CREATE DATABASE mybatis;
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(255),
gender CHAR(1),
email VARCHAR(255)
);
INSERT INTO tbl_employee VALUES('tom',0,'tom@qq.com');
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
@Data
@ToString
public class Employee {
private Integer id;
private String lastName;
private char gender;
private String email;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 注册Mapper映射文件 -->
<mappers>
<mapper resource="EmployeeMapper.xml"/>
</mappers>
</configuration>
<select id="getEmpsByConditionIf" resultType="com.wwj.mybatis.bean.Employee">
select *
from tbl_employee
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 or gender == 1">
and gender = #{gender}
</if>
</select>
EmployeeDynamicSqlMapper mapper = sqlSession.getMapper(EmployeeDynamicSqlMapper.class);
Employee employee = new Employee(1, "%c%", null, "jack@qq.com");
List<Employee> emps = mapper.getEmpsByConditionIf(employee);
for (Employee emp : emps) {
System.out.println(emp);
}
==> Preparing: select * from tbl_employee where id = ? and last_name like ? and email = ?
==> Parameters: 1(Integer), %c%(String), jack@qq.com(String)
<== Columns: id, last_name, gender, email, d_id
<== Row: 1, jack, 1, jack@qq.com, 1
<== Total: 1
Employee(id=1, lastName=null, gender=1, email=jack@qq.com)
E
mployee employee = new Employee(null, "%c%", null, "jack@qq.com");
=
=> Preparing: select *
from tbl_employee where
and last_name like ?
and email = ?
=
=> Parameters: %c%(String), jack@qq.com(String)
<select id="getEmpsByConditionIf" resultType="com.wwj.mybatis.bean.Employee">
select *
from tbl_employee
<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 or gender == 1">
and gender = #{gender}
</if>
</where>
</select>
<select id="getEmpsByConditionIf" resultType="com.wwj.mybatis.bean.Employee">
select *
from tbl_employee
<where>
<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 or gender == 1">
gender = #{gender}
</if>
</where>
</select>
Employee employee = new Employee(null, "%c%", null, null);
==> Preparing: select * from tbl_employee WHERE last_name like ? and
==> Parameters: %c%(String)
<select id="getEmpsByConditionIf" resultType="com.wwj.mybatis.bean.Employee">
select *
from tbl_employee
<trim prefix="where" 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 or gender == 1">
gender = #{gender}
</if>
</trim>
</select>
-
prefix:指定sql的前缀,该前缀会被放在trim标签拼接好的sql前面
-
suffix:指定sql的后缀,该后缀会被放在trim标签拼接好的sql后面
-
prefixOverrides:前缀覆盖,MyBatis会自动去除属性前的指定字符串
-
suffixOverrides:后缀覆盖,MyBatis会自动去除属性后的指定字符串
==> Preparing: select * from tbl_employee where last_name like ?
==> Parameters: %c%(String)
<== Columns: id, last_name, gender, email, d_id
<== Row: 1, jack, 1, jack@qq.com, 1
<== Total: 1
Employee(id=1, lastName=null, gender=1, email=jack@qq.com)
List<Employee> getEmpsByConditionChoose(Employee employee);
<select id="getEmpsByConditionChoose" resultType="com.wwj.mybatis.bean.Employee">
select *
from tbl_employee
<where>
<choose>
<when test="id != null">
id = ${id}
</when>
<when test="lastName != null">
last_name like #{lastName}
</when>
<otherwise>
</otherwise>
</choose>
</where>
</select>
Employee employee = new Employee(1, "%c%", null, null);
List<Employee> emps = mapper.getEmpsByConditionChoose(employee);
for (Employee emp : emps) {
System.out.println(emp);
}
==> Preparin
g: select * from tbl_employee WHERE id =
1
==> Parameter
s:
<== Column
s: id, last_name, gender, email, d_id
<== Ro
w:
1, jack,
1, jack@qq.
com,
1
<== Tota
l:
1
void updateEmp(Employee employee);
<update id="updateEmp">
update tbl_employee
set
<if test="lastName != null">
last_name = #{lastName},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="gender != null">
gender = #{gender}
</if>
<where>
id = #{id}
</where>
</update>
Employee employee = new Employee(1, "tom", null, null);
mapper.updateEmp(employee);
,
被直接拼在了id的前面,造成语法错误,来看看生成的sql:
==> Preparing: update tbl_employee set last_name = ?, WHERE id = ?
==> Parameters: tom(String), 1(Integer)
<update id="updateEmp">
update tbl_employee
<set>
<if test="lastName != null">
last_name = #{lastName},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="gender != null">
gender = #{gender}
</if>
</set>
<where>
id = #{id}
</where>
</update>
<update id="updateEmp">
update tbl_employee
<trim prefix="set" suffixOverrides=",">
<if test="lastName != null">
last_name = #{lastName},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="gender != null">
gender = #{gender}
</if>
</trim>
<where>
id = #{id}
</where>
</update>
07. foreach
List<Employee> getEmpsByConditionForeach(@Param("ids") List<Integer> ids);
<select id="getEmpsByConditionForeach" resultType="com.wwj.mybatis.bean.Employee">
select *
from tbl_employee
where id in(
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)
</select>
<select id="getEmpsByConditionForeach" resultType="com.wwj.mybatis.bean.Employee">
select *
from tbl_employee
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
List<Employee> emps = mapper.getEmpsByConditionForeach(Arrays.asList(1, 2, 3));
for (Employee emp : emps) {
System.out.println(emp);
}
==> Preparing: select * from tbl_employee where id in ( ? , ? , ? )
==> Parameters: 1(Integer), 2(Integer), 3(Integer)
<== Columns: id, last_name, gender, email, d_id
<== Row: 1, tom, 1, jack@qq.com, 1
<== Row: 3, tom, 1, tom@qq.com, 2
<== Total: 2
Employee(id=1, lastName=null, gender=1, email=jack@qq.com)
Employee(id=3, lastName=null, gender=1, email=tom@qq.com)
void addEmpBatch(@Param("emps") List<Employee> emps);
<insert id="addEmpBatch">
insert into tbl_employee(last_name,gender,email)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.gender},#{emp.email})
</foreach>
</insert>
List<Employee> emps = new ArrayList<>();
emps.add(new Employee(null, "jack", '1', "jack@qq.com"));
emps.add(new Employee(null, "jerry", '1', "jerry@qq.com"));
mapper.addEmpBatch(emps);
==> Preparing: insert into tbl_employee(last_name,gender,email) values (?,?,?) , (?,?,?)
==> Parameters: jack(String), 1(String), jack@qq.com(String), jerry(String), 1(String), jerry@qq.com(String)
<== Updates: 2
<insert id="addEmpBatch">
<foreach collection="emps" item="emp" separator=";">
insert into tbl_employee(last_name, gender, email)
values (#{emp.lastName}, #{emp.gender}, #{emp.email})
</foreach>
</insert>
;
分隔,但是MySQL默认是不支持这种语法的,为此,需要设置一个参数使其支持该语法:
jdbc.url=jdbc:mysql:///mybatis?allowMultiQueries=true
-
_parameter:所有参数,单个参数情况下,_parameter就是该参数;多个参数情况下,_parameter就是封装所有参数的Map集合
-
_databaseId:若配置了databaseIdProvider 标签,则该参数就是当前数据库的别名
<select id="getEmpsByConditionChoose" resultType="com.wwj.mybatis.bean.Employee">
<bind name="_lastName" value="'%'+ lastName + '%'"/>
select *
from tbl_employee
<where>
<choose>
<when test="id != null">
id = ${id}
</when>
<when test="lastName != null">
last_name like #{_lastName}
</when>
<otherwise>
</otherwise>
</choose>
</where>
</select>
%
,接下来就可以引用该变量,此时调用方法进行模糊查询就不需要自己拼接
%c%
了,而是直接传入字符串
c
即可。
<sql id="insertColumn">
last_name,gender,email
</sql>
<insert id="addEmpBatch">
insert into tbl_employee(
<include refid="insertColumn"></include>
)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.gender},#{emp.email})
</foreach>
</insert>
公众号运营至今,离不开小伙伴们的支持。为了给小伙伴们提供一个互相交流的平台,特地开通了官方交流群。关注公众号「Java后端」回复「进群」即可。
推荐阅读
1. 推荐几个好玩的 GitHub 项目
2. 推荐几个程序员常用的软件
3.
图解 Spring 解决循环依赖
本文分享自微信公众号 - Java后端(web_resource)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
相关文章
暂无评论...