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