1、定义EmployeeMapperDynamicSQL接口
package com.lhk.mybatis.dao;
import com.lhk.mybatis.bean.Employee;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface EmployeeMapperDynamicSQL {
public List<Employee> getEmpByConditionIf(Employee employee);
public List<Employee> getEmpByConditionTrim(Employee employee);
public List<Employee> getEmpByConditionChoose(Employee employee);
public void updateEmp(Employee employee);
public void insertEmp(Employee employee);
public List<Employee> getEmpByConditionForeach(@Param("ids") List<Integer> id);
public void addEmp(@Param("emps") List<Employee> employee);
}
2、定义EmployeeMapperDynamicSQL.xml配置文件
<?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="com.lhk.mybatis.dao.EmployeeMapperDynamicSQL">
<resultMap id="Simple" type="com.lhk.mybatis.bean.Employee">
<result column="last_name" property="lastName"></result>
</resultMap>
<select id="getEmpByConditionIf" resultMap="Simple">
select * from tb1_employee
-- where
<where>
<if test="id!=null">
id=#{id}
</if>
<if test="lastName!=null and lastName!=''">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=''">
and email=#{email}
</if>
<if test="gender==0 and gender==1">
and gender=#{gender}
</if>
</where>
</select>
<select id="getEmpByConditionTrim" resultMap="Simple">
select * from tb1_employee
-- where
<trim prefix="where" prefixOverrides="" suffix="" suffixOverrides="and">
<if test="id!=null">
id=#{id} and
</if>
<if test="lastName!=null and lastName!=''">
last_name like #{lastName} and
</if>
<if test="email!=null and email.trim()!=''">
email=#{email} and
</if>
<if test="gender==0 and gender==1">
gender=#{gender}
</if>
</trim>
</select>
<select id="getEmpByConditionChoose" resultMap="Simple">
select * from tb1_employee
<where>
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name like #{lastName}
</when>
<when test="email!=null">
email=#{email}
</when>
<otherwise>
gender=0
</otherwise>
</choose>
</where>
</select>
<update id="updateEmp">
update tb1_employee
<set>
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender!=null">
gender=#{gender}
</if>
id=#{id},
</set>
where id=#{id}
</update>
<insert id="insertEmp">
insert into tb1_employee(
<if test="id!=null and id!=''">
id,
</if>
<if test="lastName!=null and lastName!=''">
last_name,
</if>
<if test="email!=null and email!=''">
email,
</if>
<if test="gender!=null and gender!=''">
gender
</if>
)
<trim suffixOverrides=",">
values(
<if test="id!=null and id!=''">
#{id},
</if>
<if test="lastName!=null and lastName!=''">
#{lastName},
</if>
<if test="email!=null and email!=''">
#{email},
</if>
<if test="gender!=null and gender!=''">
#{gender}
</if>
)
</trim>
</insert>
<select id="getEmpByConditionForeach" resultMap="Simple">
select * from tb1_employee where id in
<foreach collection="ids" item="item_id" separator="," open="(" close=")">
#{item_id}
</foreach>
</select>
<insert id="addEmp">
insert into tb1_employee(last_name,email,gender,d_id)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
</foreach>
</insert>
</mapper>
3、定义MybatisDynamicSQLTest测试类
package com.lhk.mybatis.test;
import com.lhk.mybatis.bean.Department;
import com.lhk.mybatis.bean.Employee;
import com.lhk.mybatis.dao.EmployeeMapperDynamicSQL;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MybatisDynamicSQLTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "conf/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testgetEmpByConditionIf() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);
Employee employee = new Employee(2,"%e%","Jerry@qq.com","1");
List<Employee> list = mapper.getEmpByConditionIf(employee);
for (Employee emp : list) {
System.out.println(emp);
}
}finally {
sqlSession.close();
}
}
@Test
public void testGetEmpByConditionTrim() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);
Employee employee = new Employee(null, "%e%", "Jerry@qq.com", "1");
List<Employee> list = mapper.getEmpByConditionTrim(employee);
for (Employee emp : list) {
System.out.println(emp);
}
}finally {
sqlSession.close();
}
}
@Test
public void testGetEmpByConditionChoose() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);
Employee employee = new Employee(null, null, "Jerry@qq.com", "1");
List<Employee> list = mapper.getEmpByConditionChoose(employee);
for (Employee emp : list) {
System.out.println(emp);
}
}finally {
sqlSession.close();
}
}
@Test
public void testUpdateEmp() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);
Employee employee = new Employee(null, null, null, null);
mapper.updateEmp(employee);
sqlSession.commit();
}finally {
sqlSession.close();
}
}
@Test
public void testInsertEmp() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);
Employee employee = new Employee(null, "lll", null, "0");
mapper.insertEmp(employee);
sqlSession.commit();
}finally {
sqlSession.close();
}
}
@Test
public void testGetEmpByConditionForeach() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);
List<Employee> list = mapper.getEmpByConditionForeach(Arrays.asList(2,6));
for (Employee emp : list) {
System.out.println(emp);
}
}finally {
sqlSession.close();
}
}
@Test
public void testBatchSave() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);
List<Employee> emps = new ArrayList<>();
emps.add(new Employee(null,"Smith","Smith@qq.com","1",new Department(1)));
emps.add(new Employee(null,"Tom","Tom@qq.com","0",new Department(2)));
mapper.addEmp(emps);
sqlSession.commit();
}finally {
sqlSession.close();
}
}
}