使用 if where foreach标签对映射配置文件中sql语句进行动态配置

1、首先在dao接口中设置两个查询方法

 package sun.dao;

 import sun.domain.QueryObj;
import sun.domain.User; import java.util.List; public interface UserDao { /**
* 根据已有条件进行查询(if where)
*/
List<User> findUserByCondition(User user); /**
* 根据集合中的id进行查询(if where foreach)
*/
List<User> findUserByList(QueryObj qobj);
}

2、配置映射配置文件(使用if where foreach三种标签)

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="sun.dao.UserDao">
<resultMap id="userMap" type="sun.domain.User">
<!--主键字段对应-->
<id property="user_id" column="id"></id>
<!--非主键关系对应-->
<result property="user_name" column="username"></result>
<result property="user_birthday" column="birthday"></result>
<result property="user_address" column="address"></result>
<result property="user_sex" column="sex"></result>
</resultMap> <!--公共sql语句抽取-->
<sql id="defaultSelect">
select * from user
</sql> <!--根据条件进行查询-->
<select id="findUserByCondition" resultMap="userMap" parameterType="user">
<include refid="defaultSelect"></include>
<where>
<if test="user_name!=null">
and username=#{user_name}
</if>
<if test="user_sex!=null">
and sex=#{user_sex}
</if>
</where>
</select>
<!--根据id列表进行查询-->
<select id="findUserByList" parameterType="queryobj" resultMap="userMap">
<include refid="defaultSelect"></include>
<where>
<if test="ids!=null and ids.size()>0">
<foreach collection="ids" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
</mapper>

在映射配置文件中可以使用sql标签对常用的sql语句进行抽取,在操作标签内如果需要使用该sql语句可使用include标签进行导入即可。

(注意:使用sql语句进行抽取时,sql语句后面不要添加分号,否则会导致在操作标签内引用后拼接sql字符串时造成错误)

如果在该段代码中有关于parameterType未使用全限定类名的疑惑请参考Mybatis项目构建和CURD操作博客最下面的properties标签和typeAliases标签的使用

3、测试类中进行测试

 package sun.test;

 import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sun.dao.UserDao;
import sun.domain.QueryObj;
import sun.domain.User; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; public class MybatisTest { private InputStream in;
private SqlSession sqlSession;
private UserDao userDao; @Before
public void init() throws IOException {
// 读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建SqlSessionFactory
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
// 使用工厂生产sqlsession对象
sqlSession = factory.openSession();
// 使用sqlsession创建UserDao接口代理对象
userDao = sqlSession.getMapper(UserDao.class);
} @After
public void destory() throws IOException {
sqlSession.commit();
sqlSession.close();
in.close();
} @Test
public void conditionTest(){
User user = new User();
user.setUser_name("kelvin");
user.setUser_sex("女");
List<User> users = userDao.findUserByCondition(user);
for (User user1 : users) {
System.out.println(user1);
}
} @Test
public void ids(){
QueryObj queryObj = new QueryObj();
ArrayList<Integer> ids = new ArrayList<Integer>();
ids.add(51);
ids.add(52);
ids.add(53);
queryObj.setIds(ids);
List<User> users = userDao.findUserByList(queryObj);
for (User user : users) {
System.out.println(user);
} }
}

测试结果如下:

最新文章

  1. iOS开发:http中的get和post请求
  2. angular的路由
  3. ios cell展示可滑动的图片
  4. innobackupex使用实践
  5. python写入中文到文件乱码的问题
  6. Unity中的单实例
  7. 【转】读取android根目录下的文件或文件夹
  8. VS2013 C++代码运行问题
  9. 迷茫&lt;第二篇:回到老家湖南长沙&gt;
  10. Django学习手册 - 权限管理(二)
  11. Luogu5058 ZJOI2004嗅探器(割点)
  12. dedecms如何去除后台登陆验证码
  13. Mac下RabbitMQ安装和在Java client端的使用
  14. auto function -&gt; return type 当不能从{}内推断类型时
  15. Python 2.7.6 安装lxml模块[ubuntu14.04 LTS]
  16. C#托管代码是什么?非托管代码是什么?
  17. R中执行if else报错:unexpected &#39;else&#39; in &quot;else&quot;
  18. Resetting a lost Admin password
  19. loadrunner socket协议问题归纳(2)
  20. EC20的短消息

热门文章

  1. 【译】gRPC-Web for .NET now available
  2. JNDI和连接池的配置
  3. 简谈DFS
  4. 在 Go 语言中,我为什么使用接口
  5. 【POJ2976】Dropping tests - 01分数规划
  6. 【NOI2014】动物园 - KMP
  7. Windows server 2008R2 中sql server的搭建
  8. Dubbo系列之 (五)服务订阅(2)
  9. elaticsearch
  10. wsgi的environ变量