Mybatis-2-XML 映射器
1 映射CUD增删改
(1)insert语句
参数说明:
①id:对应mapper接口中的方法名,必须的,接口的方法名
②parameterType:对应传入的参数类型(类型的全类名),代表接口的入参类型,可写不可写,一般建议不写(写多了容易出错),让mybatis自己推断
③useGeneratedKeys:告诉mybatis,执行后需要返回自行生成的主键值
④keyProperty:指定mybatis返回的主键值,交给实体类的哪个属性进行保存
注意:insert,update,delete操作是没有返回值类型属性-resultType,只会返回影响行数
<insert id="insertEdocEntry" parameterType="com.yuyi.mybatis.beans.EdocEntry"
useGeneratedKeys="true" keyProperty="id">
insert into table_name(colName1,..,colNameN) values(#{v1},...,#{vN})
</insert
(2)update语句
<update id="updateEdocEntry">
update table_name set colName1 = #{v1},..., colNameN = #{vN} where ...
</update>
(3)delete语句
<delete id="deleteEdocEntry">
delete from table_name where ...
</delete>
2 映射select查询
(1)select语句:单个参数
<select id="selectEdocEntryById" resultType="com.yuyi.mybatis.beans.EdocEntry">
select id,cid,title,summary, uploadUser,createDate
from edoc_entry
where id = #{id}
</select>
(2)select语句:双个参数,使用注解进行别名处理
EdocEntry selectEdocEntryByAuthorAndTitle(@Param("author") String author, @Param("title")String title);
<select id="selectEdocEntryByAuthorAndTitle" resultType="com.kgc.mybatis.beans.EdocEntry">
select id, cid, title, summary, upload_user, create_date
from edoc_entry
where upload_user = #{author} and title = #{title}
</select>
(3)select:实体参数,使用注解处理,并采用 . 去得实体属性
EdocEntry selectEdocEntryByEdoc(@Param("edoc") EdocEntry edocEntry);
<select id="selectEdocEntryByEdoc" resultType="com.kgc.mybatis.beans.EdocEntry">
select id, cid, title, summary, upload_user, create_date
from edoc_entry
where upload_user = #{edoc.uploadUser} and title = #{edoc.title}
</select>
(4)传递map参数,使用注解别名处理,
EdocEntry selectEdocEntryByMap(Map<String, String> paramMap);
<select id="selectEdocEntryByMap" resultType="com.kgc.mybatis.beans.EdocEntry">
select id, cid, title, summary, upload_user, create_date
from edoc_entry
where upload_user = #{author} and title = #{title}
</select>
(5)${}符号传参实现动态查表
EdocEntry selectEdocEntryByIdAndTableName(@Param("id") Integer id, @Param("tableName") String tableName);
<select id="selectEdocEntryByIdAndTableName" resultType="com.kgc.mybatis.beans.EdocEntry">
select id, cid, title, summary, upload_user, create_date
from ${tableName}
where id = #{id}
</select>
(6)返回类型自动封装成List返回
List<EdocEntry> selectEdocEntryListByCid(Integer cid);
<select id="selectEdocEntryListByCid" resultType="com.kgc.mybatis.beans.EdocEntry">
select id, cid, title, summary, upload_user, create_date
from edoc_entry
where cid = #{cid}
</select>
(7)使用concat拼接模糊查询参数
List<EdocEntry> selectEdocEntryListBySummary(String summary);
<select id="selectEdocEntryListBySummary" resultType="com.kgc.mybatis.beans.EdocEntry">
select id, cid, title, summary, upload_user, create_date
from edoc_entry
where summary like concat('%', #{summary}, '%')
</select>
(8)模糊查找并且分页返回
List<EdocEntry> selectEdocEntryListBySummaryUsePage(@Param("summary") String summary, @Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize);
<select id="selectEdocEntryListBySummaryUsePage" resultType="com.kgc.mybatis.beans.EdocEntry">
<bind name="pageIndex" value="(pageNo - 1) * pageSize"/>
select id, cid, title, summary, upload_user, create_date
from edoc_entry
where summary like concat('%', #{summary}, '%')
limit #{pageIndex}, #{pageSize}
</select>
总结:
Mybatis入参处理:
1) 单个参数;可以随便定义获取参数明,一般建议跟方法形参参数名一直
2) 多个参数:默认情况,不是形参参数名,而是内置参数:0,1,...或者param1
3) 多个参数:可以自定义参数别名,使用@Param(“自定义参数名”)注解,后去参数就只能是自定你工艺参数名
4)实体参数:将入参直接封装到实体对象中,获取参数就直接使用实体的属性名(#{属性名}),如果给实体起了别名,哦那就必须是(#{别名.属性名})
5) map参数:直接将map集合作为入参,获取参数直接使用map的key(#{map的key})
Mybatis的参数获取法师:#{参数}或者${参数}
1) #{}方式:可以获取不同参数,比如,自定义参数,实体参数等,会进行预编译处理,参数使用占位符 ?代替,安全性高
2) ${}方式:正常情况下,跟#{}使用上没啥区别,会直接进行字符串拼接,有安全隐患,比如sql注入
建议:能使用#{}的时候,优先使用,除非#{}解决不了(动态自动,动态表名,动态排序,参数计算等)
Q.E.D.