Mapper代理的开发方式:
-
只需要编写mapper接口,Mybatis会根据映射文件自动生成动态代理实现类。
开发规范:
- mapper接口的全名对应 mapper映射文件的namespace的值相同
- mapper接口的方法名对应 mapper映射文件中的statement(select,insert,delete)的id相同;
- mapper接口的方法参数只能有一个,且类型要和mapper映射文件中statement的parameter的type相同。
- mapper接口的返回值类型要和mapper映射文件中statement的resultType值或resultMap中的type相同;
动态SQL
1. if和where
- If标签:用来判断传入参数,如果符合条件,则把if标签体内的SQL拼接上
- 用if进行判断是否为空时,不仅要判断null,也要判断空字符串''
- Where标签:会去掉条件中的第一个and符号
- 案例:模糊查询性别是user.sex(入参),名字是user.name(入参),地址是user.address(入参),判断三者是否为空或null
-
<!-- 3.if和where的使用--> <sql id="select_user_where"> <if test="user != null"> <if test="user.sex != null and user.sex != ''"> sex = #{user.sex} </if> <if test="user.username != null and user.username != ''"> and username LIKE '%${user.username}%' </if> <if test="user.address != null and user.address != ''"> and address LIKE '%${user.address}%' </if> </if> </sql> <select id="findUserList" parameterType="userQueryVO" resultType="user"> /*性别和名字*/ SELECT * FROM user <where> <include refid="select_user_where"/> </where> </select>
- 实质:判断条件,然后拼接,注意拼接规则
2.SQL片断
3.foreach 遍历
- SELECT * FROM user WHERE id in (1,2,3) 参数传入一个关于 id的list集合
<foreach collection="ids" item="id" open="id in(" close=")" separator="," >
${id}
</foreach>
拼接格式
- collection:集合,写集合属性
- item:遍历接收的变量
- open:遍历开始
- close:遍历结束
- 注意open和close的拼接,id in ( )
- separator:拼接格式,分隔符号
- ${id}:获取( )内部的参数
-
<select id="findUserByIds" parameterType="userQueryVO" resultType="user"> SELECT * FROM user <where> <if test="ids != null and ids.size > 0"> <foreach collection="ids" item="id" open="id in(" close=")" separator="," > ${id} </foreach> </if> </where> </select>
关联查询 :
-
个人总结:关联查询 肯定就不是一个简单的模型了,如下代码,在模型1中有模型2的集合,集合中有模型3,查询关联多个表,必定要采用resyltMap来进行和数据库表字段名的对应,在映射过程中集合用collection 模型用association,对应每个表(每个model)的主键要用<id column="xxx".....>来说明,其余字段用result,注意嵌套关系,model包中的VO类(来拿各种方式:model嵌套成复杂model;model继承(tostring:super.tostring)
代码案例
<!-- ==============查询用户信息及用户购买的商品信息============-->
<resultMap id="userRslMap" type="user">
<!-- 1.匹配user属性 -->
<id column="id" property="id"></id>
<result column="username" property="username"/>
<result column="password" property="password"/>
<!--2.匹配user的orderList-->
<collection property="orderList" ofType="orders">
<id column="order_id" property="id"></id>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!-- 3.匹配Orders里有orderDetails-->
<collection property="orderDetails" ofType="orderDetail">
<id column="detail_id" property="id"></id>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<!-- 4.配置定单详情的商品信息-->
<association property="items" javaType="items">
<id column="items_id" property="id"/>
<result column="name" property="name"/>
<result column="price" property="price"/>
<result column="detail" property="detail"/>
</association>
</collection>
</collection>
</resultMap>
<select id="findUserAndOrderInfo" resultMap="userRslMap">
SELECT
u.id,
u.username,
u.address,
o.id order_id,
o.number,
o.createtime,
o.note,
od.id detail_id,
od.items_id,
od.items_num,
it.name,
it.price,
it.detail
FROM
user u,
orders o,
orderdetail od,
items it
WHERE
o.user_id = u.id
AND o.id = od.orders_id
AND od.items_id = it.id
</select>