if标签

  • 在UserMapper中加入
//多条件查询 通过用户对象中的条件查询用户列表
public List<User> selectUserListByUser(User u);

  • 在UserMapper.xml中加入
<!-- //多条件查询 通过用户对象中的条件查询用户列表
public List<User> selectUserListByUser(User u); -->
<select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊查询用户名 查询用户c_id国际Id -->
select *
from user
where
<!-- where 可以去掉开头的and 符号,and 不能放到末尾 -->
<if test="u_sex!=null and u_sex!=''">
u_sex=#{u_sex}
</if>
<if test="u_username!=null and u_username!=''">
and u_username like "%"#{u_username}"%"
</if>
<if test="u_cid!=null">
and u_cid=#{u_cid}
</if>
</select>

这里有几个需要注意的地方

  • 使用模糊查询的时候需要使用的格式 “%”#{u_username}"%"
  • 在if标签内部写需要进行拼接的SQL语句
  • 上面 if中的test教师u_sex不为空并且不为空串

where标签

where 标签可以去掉 前AND

  • 将上面的代码改下
  <!-- //多条件查询 通过用户对象中的条件查询用户列表
public List<User> selectUserListByUser(User u); -->
<select id="selectUserListByUser" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊查询用户名 查询用户c_id国际Id -->
select *
from user
<where>
<!-- where 可以去掉开头的and 符号,and 不能放到末尾 -->
<if test="u_sex!=null and u_sex!=''">
u_sex=#{u_sex}
</if>
<if test="u_username!=null and u_username!=''">
and u_username like "%"#{u_username}"%"
</if>
<if test="u_cid!=null">
and u_cid=#{u_cid}
</if>
</where>
</select>

trim标签

  • 使用trim标签就是用来替代where标签的
<select id="selectUserListByUserTrim" parameterType="User" resultType="User">
<!-- 查询用户性别 模糊查询用户名 查询用户c_id国际Id -->
select *
from user
<trim prefix="where" suffixOverrides="and">
<!-- where 可以去掉开头的and 符号,and 不能放到末尾 -->
<if test="u_sex!=null and u_sex!=''">
u_sex=#{u_sex} and
</if>
<if test="u_username!=null and u_username!=''">
u_username like "%"#{u_username}"%" and
</if>
<if test="u_cid!=null">
u_cid=#{u_cid}
</if>
</trim >
</select>

这里需要注意的地方

  • where标签默认使用的就是 prefixOverrides=“and” 也就是去掉前AND
  • 这里使用 prefix=“where” 意思是添加前缀 where 所以用trim就可以代替where
  • suffixOverrides=“and” 意思是去掉 后AND
  • 在UserMapper中
//多条件查询 通过用户对象中的条件查询用户列表Trim
public List<User> selectUserListByUserTrim(User u);

  • 在MapperTest中
@Test
public void Test_selectUserListByUserTrim() throws IOException {
String resource="sqlMapConfig.xml";
//读取配置文件
InputStream in = Resources.getResourceAsStream(resource);
//创建sqlSessionfactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
//生产一个sqlSession
SqlSession session = ssf.openSession();

UserMapper mapper = session.getMapper(UserMapper.class);

User u=new User();
u.setU_sex("1");
u.setU_username("王");
u.setU_cid(1);

List<User> list = mapper.selectUserListByUserTrim(u);
for (User user : list) {
System.out.println(user);
}
}

set标签

  • 在UserMapper中加入
//更新用户表
public void updateSetUser(User u);

  • 在UserMapper.xml中加入

<!-- //更新用户表
public void updateSetUser(User u); -->
<update id="updateSetUser" parameterType="User">
<!-- 用户名 用户 密码 性别 用id来限制 -->
update user
<set>
<if test="u_username!=null and u_username!=''">
u_username=#{u_username},
</if>
<if test="u_password!=null and u_password!=''">
u_password=#{u_password},
</if>
<if test="u_sex!=null and u_sex!=''">
u_sex=#{u_sex}
</if>
</set>
where u_id=#{u_id}
</update>

  • 在MapperTest中

@Test
public void Test_updateSetUser() throws IOException {
String resource="sqlMapConfig.xml";
//读取配置文件
InputStream in = Resources.getResourceAsStream(resource);
//创建sqlSessionfactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
//生产一个sqlSession
SqlSession session = ssf.openSession();

UserMapper mapper = session.getMapper(UserMapper.class);

User u=new User();
u.setU_id(1);
u.setU_username("隔壁老王");
u.setU_password("aaa");
//u.setU_sex("1");

mapper.updateSetUser(u);
session.commit();
}

  • 在where使用的时候拼接SQL语句使用的是AND ,而where标签或者是trim标签的作用就是去掉前AND或者是后AND(因空串或者null 产生的多余的AND的问题)
  • 而在使用Set的时候,产生的则是 逗号 “,” 使用set标签的作用就是去掉这个逗号,使用trim同样可以代替set

foreach 遍历数组和集合

数组

  • UserMapper
//使用多个id获取用户列表 by array
public List<User> selectUserListByIds(Integer[] ids);

  • Usermapper.xml
<!-- //使用多个id获取用户列表
public List<User> selectUserListByIds(); (1,3,5)-->
<select id="selectUserListByIds" parameterType="CountryVo" resultType="User" >
select *
from user
where u_id
in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>

这里的foreach中的collection =“array” 因为我们使用的是数组

  • Mappertest
@Test
public void Test_selectUserListByIds() throws IOException {
String resource="sqlMapConfig.xml";
//读取配置文件
InputStream in = Resources.getResourceAsStream(resource);
//创建sqlSessionfactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
//生产一个sqlSession
SqlSession session = ssf.openSession();

UserMapper mapper = session.getMapper(UserMapper.class);

Integer[] ids= {1,3,5,12};
List<User> list = mapper.selectUserListByIds(ids);
for (User user : list) {
System.out.println(user);
}
}

集合

  • UserMapper
//使用多个id获取用户列表 by list
public List<User> selectUserListByList(List<Integer> idList);

  • Usermapper.xml
<!-- //使用多个id获取用户列表
public List<User> selectUserListByList(List<Integer> ids); -->
<select id="selectUserListByList" parameterType="CountryVo" resultType="User">
select *
from user
where u_id
in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>

这里的foreach中的collection =“array” 因为我们使用的是数组

  • Mappertest
@Test
public void Test_selectUserListByList() throws IOException {
String resource="sqlMapConfig.xml";
//读取配置文件
InputStream in = Resources.getResourceAsStream(resource);
//创建sqlSessionfactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
//生产一个sqlSession
SqlSession session = ssf.openSession();

UserMapper mapper = session.getMapper(UserMapper.class);

List<Integer> idList=new ArrayList<>();
idList.add(1);
idList.add(4);
idList.add(8);

List<User> list = mapper.selectUserListByList(idList);
for (User user : list) {
System.out.println(user);
}
}

包装类

  • UserMapper
  //by userVo
public List<User> selectUserListByUserVo(UserVo vo);

  • Usermapper.xml
  <!-- //by userVo
public List<User> selectUserListByUserVo(UserVo vo); -->
<select id="selectUserListByUserVo" parameterType="UserVo" resultType="User">
select *
from user
where u_id
in
<foreach collection="idList" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>

这里我们的参数类型是UserVol 和上面的饿两种不同的是这里的collection=“idList” idList 为我们遍历的集合的名

  • Mappertest
  @Test
public void Test_selectUserListByUserVo() throws IOException {
String resource="sqlMapConfig.xml";
//读取配置文件
InputStream in = Resources.getResourceAsStream(resource);
//创建sqlSessionfactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
//生产一个sqlSession
SqlSession session = ssf.openSession();

UserMapper mapper = session.getMapper(UserMapper.class);

List<Integer> idList=new ArrayList<>();
idList.add(1);
idList.add(4);
idList.add(8);


UserVo vo=new UserVo();
vo.setIdList(idList);
List<User> list = mapper.selectUserListByUserVo(vo);
for (User user : list) {
System.out.println(user);
}
}

sql标签

  • UserMapper.xml 的前面
<sql id="myselect">
select *
from user
</sql>

用来代替重复的代码

  • 使用下面代码取用
  <select id="selectUserListByUserVo" parameterType="UserVo" resultType="User">
<include refid="myselect"/>
where u_id
in
<foreach collection="idList" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>