1.Mapper接口方法名和mapper.xml中定義的每個(gè)sql的id相同;
2.Mapper接口方法的輸入?yún)?shù)類型和mapper.xml中定義的每個(gè)sql的parameterType的類型相同;
3.Mapper接口方法的輸出參數(shù)類型和mapper.xml中定義的每個(gè)sql的resultType的類型相同;
4.Mapper.xml文件中的namespace即是mapper接口的類路徑;
第一種:通過定義select語(yǔ)句中字段名的別名,讓字段明的別名和實(shí)體類的屬性名一致
第二種:通過ResutlMap來(lái)映射字段名和實(shí)體類屬性名的一一對(duì)應(yīng)的關(guān)系。
<select id="selectlike">
? select * from foo where bar like #{value}
</select>
第 2 種:在 sql 語(yǔ)句中拼接通配符,會(huì)引起 sql 注入
<select id="selectlike">
? select * from foo where bar like "%"#{value}"%"
</select>
1)MyBatis動(dòng)態(tài)sql可以讓我們?cè)赬ml映射文件內(nèi),以標(biāo)簽的形式編寫動(dòng)態(tài)sql,完成邏輯判斷和動(dòng)態(tài)拼接sql的功能,MyBatis提供了9種動(dòng)態(tài)sql標(biāo)簽 trim | where | set | foreach | if | choose | when | otherwise | bind。
2)其執(zhí)行原理為,使用OGNL從sql參數(shù)對(duì)象中計(jì)算表達(dá)式的值,根據(jù)表達(dá)式的值動(dòng)態(tài)拼接sql,以此來(lái)完成動(dòng)態(tài)sql的功能。
Dao接口的工作原理是JDK動(dòng)態(tài)代理,Mybatis運(yùn)行時(shí)會(huì)使用JDK動(dòng)態(tài)代理為Dao接口生成代理proxy對(duì)象,代理對(duì)象proxy會(huì)攔 截接口方法,轉(zhuǎn)而執(zhí)行MappedStatement所代表的sql,然后將sql執(zhí)行結(jié)果返回。
Dao接口里的方法,是不能重載的,因?yàn)槭侨廾?方法名的保存和尋找策略。
MyBatis實(shí)現(xiàn)一對(duì)一、一對(duì)多關(guān)聯(lián)查詢一般有兩種方式:
方式一:sqlMapper配置文件
一對(duì)一:在resultMap標(biāo)簽中使用 association 標(biāo)簽
一對(duì)多:在resultMap 標(biāo)簽中使用collection 標(biāo)簽
方式二:注解
一對(duì)一:在@Results 注解中的@Result注解中使用@One注解
一對(duì)多:在@Results 注解中的@Result 注解中使用@Many注解
useGeneratedKeys為true,持自動(dòng)生成主鍵,keyProperty和keyColumn分別代表數(shù)據(jù)庫(kù)記錄主鍵字段和java對(duì)象成員屬性名
dao層 修飾符 為 void,不需要返回任何
<insert id="insertEntity" parameterType="**" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
//dao層 修飾符 為 void,不需要返回任何
void batchInsert(List<TAlarmChannelEntity> list);
<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" >
INSERT INTO t_alarm_channel
(channel, silent_cycle, aggregation_mode,threshold,effective_start_time,effective_end_time,inform_obj,selected,create_time)
VALUES
<foreach collection="list" index="index" item="bo" separator=",">
(
#{bo.channel}, #{bo.silentCycle}, #{bo.aggregationMode},#{bo.threshold},#{bo.effectiveStartTime},#{bo.effectiveEndTime},#{bo.informObj},#{bo.selected},now()
)
</foreach>
</insert>
ResultMap是??指定返回值與對(duì)象及與對(duì)象內(nèi)部屬性名和數(shù)據(jù)庫(kù)列名的綁定
ResultType是直接返回值與別名庫(kù)當(dāng)中的java對(duì)象的映射
1,第一種:
public UserselectUser(String name,String area); 對(duì)應(yīng)的 xml,#{0}代表接收的是 dao 層中的第一個(gè)參數(shù),#{1}代表 dao 層中第二 參數(shù),更多參數(shù)一致往后加即可。
<select id="selectUser"resultMap="BaseResultMap">
? select * from user_user_t where user_name = #{0} and user_area=#{1}
</select>
2,第二種:使用@param注解:
public interface UserMapper {
? ? User selectUser(@param("username") String username,
? ? ? ? ? ? ? ? ? ? @param("hashedpassword") sString hashedpassword);
}
然后,就可以在 xml 像下面這樣使用(推薦封裝為一個(gè) map,作為單個(gè)參數(shù)傳遞給mapper):
<select id="selectuser" resulttype="user">
? select id, username, hashedpassword
? from some_table
? where username = #{username} and hashedpassword = #{hashedpassword}
</select>
3、第三種:多個(gè)參數(shù)封裝成 map
try {
? ? //映射文件的命名空間.SQL 片段的 ID,就可以調(diào)用對(duì)應(yīng)的映射文件中的SQL
? ? //由于我們的參數(shù)超過了兩個(gè),而方法中只有一個(gè) Object 參數(shù)收集,因此我們使用 ? ? Map 集合來(lái)裝載我們的參數(shù)
? ? Map < String, Object > map = new HashMap();
? ? map.put("start", start);
? ? map.put("end", end);
? ? return sqlSession.selectList("StudentID.pagination", map);
} catch (Exception e) {
? ? e.printStackTrace();
? ? sqlSession.rollback();
? ? throw e;
} finally {
? ? MybatisUtil.closeSqlSession();
}