MyBatis
MyBatis
是一款优秀的持久层框架,它支持自定义
SQL
、存储过程以及高级映射。
MyBatis
免除了几乎 所有的 JDBC
代码以及设置参数和获取结果集的工作。
MyBatis
可以通过简单的
XML
或注解来配置和映射原始类型、接口和 Java POJO
(
Plain Old Java Objects
,普通老式
Java
对象)为数据库中的记录。
开发步骤
1
、添加依赖
pom.xml
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
4.0.0
com.yan
1.0-SNAPSHOT
雇员管理系统
8
8
org.mybatis
3.5.11
mysql
8.0.31
javax.servlet
4.0.1
provided
javax.servlet.jsp
2.3.3
provided
2
、定义数据库表
3
、根据表结构定义对应的实体类
javax.servlet
1.2
org.projectlombok
1.18.24
provided
junit
4.13.2
test
emp
org.eclipse.jetty
9.4.11.v20180605
10
/emp
create table
if
not
exists t_dept(
id
bigint
primary key auto_increment,
dept_name
varchar
(
20
)
not
null
,
location
varchar
(
50
)
)engine=innodb default charset utf8mb4;
4
、定义映射元文件,用于指定对应关系和对应的
SQL
语句
@Data
public class
Dept
implements
Serializable
{
private
Long
id
;
private
String
deptName
;
private
String
location
;
}
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
namespace
=
"com.yan.dao.DeptMapper"
>
id
=
"baseMapper"
type
=
"com.yan.entity.Dept"
>
property
=
"id"
column
=
"id"
jdbcType
=
"BIGINT"
/>
property
=
"deptName"
column
=
"dept_name"
jdbcType
=
"VARCHAR"
/>
property
=
"location"
column
=
"location"
jdbcType
=
"VARCHAR"
/>
id
=
"columns"
>
id,dept_name,location
resultMap
=
"baseMapper"
>
select
refid
=
"columns"
/>
from t_dept where 1=1
test
=
"id!=null"
>
and id=#{id}
test
=
"deptName!=null and deptName!=''"
>
and dept_name = #{deptName}
test
=
"location!=null and location!=''"
>
and location like #{location}
select
refid
=
"columns"
/>
from t_dept where 1=1
test
=
"noId!=null"
>
and id != #{noId}
test
=
"deptName!=null"
>
and dept_name=#{deptName}
select
refid
=
"columns"
/>
from t_dept where id=#{id}
id
=
"insertDept"
parameterType
=
"DeptBean"
useGeneratedKeys
=
"true"
keyProperty
=
"id"
>
insert into t_dept(dept_name
test
=
"location!=null"
>
trim
标签的用法
如果
state
、
title
和
author
属性均为
null
,则生成的
SQL
语句语法错误
,location
) values(#{deptName}
test
=
"location!=null"
>
,#{location}
)
id
=
"deleteById"
parameterType
=
"long"
>
delete from t_dept where id=#{id}
id
=
"updateById"
parameterType
=
"DeptBean"
>
update t_dept set id=#{id}
test
=
"deptName!=null"
>
,dept_name=#{deptName}
test
=
"location!=null"
>
,location=#{location}
where id=#{id}
SELECT * FROM BLOG
WHERE
test
=
"state != null"
>
state = #{state}
test
=
"title != null"
>
AND title like #{title}
test
=
"author != null and author.name != null"
>
AND author_name like #{author.name}
SELECT * FROM BLOG
test
=
"state != null"
>
state = #{state}
test
=
"title != null"
>
AND title like #{title}
test
=
"author != null and author.name != null"
>
AND author_name like #{author.name}
where
元素只会在子元素返回任何内容的情况下才插入
WHERE
子句。如果
state
、
title
和
author
均
为
null
时,则不会生成
where
子句 where标签另外提供功能,就是子句的开头为
AND
或
OR
,
where
元素也会将它们去除。例如
state 为null
,但是
title
不为空
choose
、
when
、
otherwise
类似于
java
中的开关分支语句
set
标签用于动态生成
update
语句中的
set
部分
set
元素可以用于动态包含需要更新的列,忽略其它不更新的列
SELECT * FROM BLOG
prefix
=
"WHERE"
prefixOverrides
=
"AND |OR "
>
如果
trim
标签体不为空,则自动在
标签体内容的前部添加前缀
WHERE,
如果标签体内容以
and
或者
or
开头,则自动剔除开头的
and
或者
or
。 另外处理尾部的suffix
或者
suffixOverrides
test
=
"state != null"
>
state = #{state}
test
=
"title != null"
>
AND title like #{title}
test
=
"author != null and author.name != null"
>
AND author_name like #{author.name}
resultMap
=
"baseMapper"
>
select
refid
=
"columns"
/>
from t_dept
test
=
"id != null"
>
AND id like #{id}
test
=
"deptName != null"
>
AND dept_name like #{deptName}
AND location like '
中国
%'
set
元素会动态地在行首插入
SET
关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)
foreach
对集合进行遍历
批量删除操作
delete from t_dept WHERE id in( 1 , 2 , 3 , 4 )
批量插入操作
insert into t_dept values (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) ,
(?,?,?)
id
=
"updateAuthorIfNecessary"
>
update Author
test
=
"username != null"
>
username=#{username},
test
=
"password != null"
>
password=#{password},
test
=
"email != null"
>
email=#{email},
test
=
"bio != null"
>
bio=#{bio}
where id=#{id}
id
=
"deleteByIds"
parameterType
=
"list"
>
delete from t_dept
collection
=
"list"
item
=
"kk"
open
=
"id in("
close
=
")"
separator
=
","
nullable
=
"true"
>
${kk}
id
=
"insertCollection"
parameterType
=
"list"
>
insert into t_dept
collection
=
"list"
open
=
"values"
separator
=
","
item
=
"dd"
>
(#{dd.id},#{dd.deptName},#{dd.location})