转载来自:http://blog.youkuaiyun.com/u013360850/article/details/53121967
1. 动态查询语句
<select id="findUserInfo" parameterType="UserInfo" resultType="UserInfo">
select * from userinfo
<where>
<if test="id!=0">
and id = #{id}
</if>
<if test="name!=null and name!=''">
and name like #{name}
</if>
<if test="address!=null and address!=''">
and address like #{address}
</if>
</where>
</select>
SqlSession session = MyBatisUtil.openSession()
UserInfoDao dao = session.getMapper(UserInfoDao.class)
UserInfo u = new UserInfo()
u.setId(4)
u.setName("%a%")
u.setAddress("%c%")
session.commit()
session.close()
此处的Like语句如果用#
,需要在传入的参数中设置%
,如果用的是$
,则需要在SQL语句中写'%#{param}%'
SQL:and name like #{name}
Java:u.setName("%a%");
SQL:name like
Java:u.setName("a");
2. 传入多个参数
<select id="findUserInfoByIdArray" resultType="UserInfo">
select * from userinfo where id in
<foreach collection="array" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
SqlSession session = MyBatisUtil.openSession()
UserInfoDao dao = session.getMapper(UserInfoDao.class)
List<Integer> ids = new ArrayList<Integer>()
ids.add(1)
ids.add(2)
ids.add(3)
List<UserInfo> list = dao.findUserInfoByIds(ids)
session.close()
3. SQL语句复用
<sql id="ids">
where id in
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</sql>
<select id="findUserInfoByIds" resultType="UserInfo">
select * from userinfo
<include refid="ids" />
</select>
4. 批量操作
<insert id="insertBatch" parameterType="java.util.List">
insert into userinfo(id,name,address)
values
<foreach collection="list" item="data" separator=",">
(
</foreach>
</insert>
SqlSession session = MyBatisUtil.openSession();
UserInfoDao dao = session.getMapper(UserInfoDao.class);
List<UserInfo> list = new ArrayList<UserInfo>();
UserInfo u1 = new UserInfo(5,"c5","dalian");
UserInfo u2 = new UserInfo(6,"c6","dalian");
UserInfo u3 = new UserInfo(7,"c7","dalian");
UserInfo u4 = new UserInfo(8,"c8","dalian");
UserInfo u5 = new UserInfo(9,"c9","dalian");
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
list.add(u5);
dao.insertBath(list);
session.commit();
session.close();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
5. 动态的拼接更新语句
<update id="updateUserInfo" parameterType="UserInfo">
update userinfo
<set>
<if test="name!=null and name!=''">
name=
</if>
<if test="address!=null and name!=''">
address=
</if>
</set>
where id=
</update>
SqlSession session = MyBatisUtil.openSession()
UserInfoDao dao = session.getMapper(UserInfoDao.class)
//此处不能所有值都为空,否则SQL语句没有更新条件会有异常
UserInfo u = new UserInfo(4, "啦啦啦", null)
dao.updateUserInfo(u)
session.commit()
session.close()
以下内容 转自Clement-Xu的csdn博客:http://blog.youkuaiyun.com/clementad/article/details/55099432
有时候需要简单地把一个Map中所有的key和value获取出来,拼到sql语句中。MyBatis提供的一种方法是遍历Map中的entrySet,然后把key扔进index里面,value扔进item中。具体的一个使用的例子如下:
- <insert id="operationName" parameterType="map">
- INSERT INTO table_name(hot_word, cnt)
- VALUES
- <foreach item="value" index="key" collection="mapData.entrySet()" open="(" separator="),(" close=")">
- #{key}, #{value}
- </foreach>
- ON DUPLICATE KEY UPDATE
- cnt=VALUES(cnt)
- </insert>