目录
数据库:
实体类:
package com.qcby.pojo;
public class Emp {
private Integer eid;
private String empName;
private Integer age;
private String sex;
private String email;
public Emp() {
}
public Emp(Integer eid, String empName, Integer age, String sex, String email) {
this.eid = eid;
this.empName = empName;
this.age = age;
this.sex = sex;
this.email = email;
}
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Emp{" +
"eid=" + eid +
", empName='" + empName + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
'}';
}
}
package com.qcby.pojo;
public class Dept {
private Integer did;
private String deptName;
public Dept() {
}
public Dept(Integer did, String deptName) {
this.did = did;
this.deptName = deptName;
}
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Dept{" +
"did=" + did +
", deptName='" + deptName + '\'' +
'}';
}
}
自定义映射resultMap
resultMap处理字段和属性的映射关系
若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性 名符合Java的规则(使用驼峰)
方式:
1.可以通过为字段起别名的方式,保证和实体类中的属性名保持一致
/**
* 查询所有的员工信息
*/
List<Emp> getAllEmp();
<select id="getAllEmp" resultType="com.qcby.pojo.Emp">
select eid,emp_name empName,age,sex,email from t_emp
</select>
2.可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中数据时,自动将_类型的字段名转换为驼峰
例如:字段名user_name,设置了mapUnderscoreToCamelCase,此时字段名就会转换为userName
<!--设置mybatis的全局配置-->
<settings>
<!--将_自动映射为驼峰,emp_name:empName-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
3.若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射
<!--
resultMap:设置自定义映射
id:表示自定义映射的唯一标识
type:查询的数据要映射的实体类的类型
子标签:
id:设置主键的映射关系
result:设置普通字段的映射关系
association:设置多对一的映射关系
collection:设置一对多的映射关系
属性:
property:设置映射关系中实体类中的属性名(type属性中的)
column:设置映射关系中表中的字段名(sql语句中的)
-->
<resultMap id="empResultMap" type="com.qcby.pojo.Emp">
<id property="eid" column="eid"></id>
<id property="empName" column="emp_name"></id>
<id property="age" column="age"></id>
<id property="sex" column="sex"></id>
<id property="email" column="email"></id>
</resultMap>
<select id="getAllEmp" resultMap="empResultMap">
select * from t_emp
</select>
多对一映射处理
查询员工信息以及员工所对应的部门信息
方式:
1.级联方式处理映射关系
/**
* 查询员工以及员工所对应的部门信息
*/
Emp getEmpAndDept(@Param("eid") Integer eid);
<resultMap id="empAndDeptResultMap" type="com.qcby.pojo.Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<result property="dept.did" column="did"></result>
<result property="dept.deptName" column="dept_name"></result>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMap">
select * from t_emp left join t_dept on t_emp.did = t_dept.did
where t_emp.eid = #{eid}
</select>
2.使用association处理映射关系
<!--
association:处理多对一的映射关系
property:需要处理多对的映射关系的属性名
javaType:该属性的类型
-->
<resultMap id="empAndDeptResultMap" type="com.qcby.pojo.Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<association property="dept" javaType="com.qcby.pojo.Dept">
<id property="did" column="did"></id>
<id property="deptName" column="dept_name"></id>
</association>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMap">
select * from t_emp left join t_dept on t_emp.did = t_dept.did
where t_emp.eid = #{eid}
</select>
3.分步查询
(1)查询员工信息
(2)根据员工所对应的部门id查询部门信息
Emp:
/**
* 通过分步查询员工以及员工所对应的部门信息
*/
//第一步:查询员工信息
Emp getEmpAndDeptByStepOne(@Param("eid")Integer eid);
<resultMap id="getEmpAndDeptByStepResultMap" type="com.qcby.pojo.Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
<association property="dept"
select="com.qcby.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did">
</association>
</resultMap>
<select id="getEmpAndDeptByStepOne" resultMap="getEmpAndDeptByStepResultMap">
select * from t_emp where eid = #{eid}
</select>
Dept:
//分步查询第二步:通过did查询员工所对应的部门
Dept getEmpAndDeptByStepTwo(@Param("did")Integer did);
<select id="getEmpAndDeptByStepTwo" resultType="com.qcby.pojo.Dept">
select * from t_dept where did = #{did}
</select>
一对多映射处理
方式:
1.collection
/**
* 获取部门以及部门中所有的员工信息
*/
Dept getDeptAndEmp(@Param("did") Integer did);
<!--
collection:处理一对多的映射关系
ofType:表示该属性所对应的集合中存储数据的类型
-->
<resultMap id="deptAndEmpResultMap" type="com.qcby.pojo.Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps" ofType="com.qcby.pojo.Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
</collection>
</resultMap>
<select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
select * from t_dept left join t_emp on t_dept.did = t_emp.did
where t_dept.did = #{did}
</select>
2.分步查询
分步查询一般第一步使用resultMap,第二步使用resultType
Dept:
/**
* 分步查询部门以及部门中所有的员工信息
*/
//分步查询第一步:查询部门信息
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
<resultMap id="deptAndEmpByStepResultMap" type="com.qcby.pojo.Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps"
select="com.qcby.mapper.EmpMapper.getDeptAndEmpByStepTwo"
column="did">
</collection>
</resultMap>
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
select * from t_dept where did = #{did}
</select>
Emp:
//分步查询第二步:根据did查询员工信息
List<Emp> getDeptAndEmpByStepTwo(@Param("did")Integer did);
<select id="getDeptAndEmpByStepTwo" resultType="com.qcby.pojo.Emp">
select * from t_emp where did = #{did}
</select>
分步查询
优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息
lazyLoadingEnabled:延迟加载的全局开关,当开启时,所有关联对象都会延迟加载
aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载 ,此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql
可通过association和 collection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType="lazy(延迟加载)|eager(立即加载)"
<!--设置mybatis的全局配置-->
<settings>
<!--将_自动映射为驼峰,emp_name:empName-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
动态SQL
Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题
if
if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之标签中的内容不会执行
/**
* 多条件查询
*/
List<Emp> getEmpByCondition(Emp emp);
<select id="getEmpByCondition" resultType="com.qcby.pojo.Emp">
select * from t_emp where 1 = 1
<if test="empName != null and empName != ''">
and emp_name = #{empName}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="email != null and email != ''">
and email = #{email}
</if>
</select>
where
where和if一般结合使用:
a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and或or去掉
注意:where标签不能去掉条件最后多余的and或or
<select id="getEmpByCondition" resultType="com.qcby.pojo.Emp">
select * from t_emp
<where>
<if test="empName != null and empName != ''">
emp_name = #{empName}
</if>
<if test="age != null and age != ''">
and age = #{age}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
<if test="email != null and email != ''">
and email = #{email}
</if>
</where>
</select>
trim
trim用于去掉或添加标签中的内容
常用属性:
- prefix:在trim标签中的内容的前面添加某些内容
- prefixOverrides:在trim标签中的内容的前面去掉某些内容
- suffix:在trim标签中的内容的后面添加某些内容
- suffixOverrides:在trim标签中的内容的后面去掉某些内容
<select id="getEmpByCondition" resultType="com.qcby.pojo.Emp">
select * from t_emp
<trim prefix="where" suffixOverrides="and">
<if test="empName != null and empName != ''">
emp_name = #{empName} and
</if>
<if test="age != null and age != ''">
age = #{age} and
</if>
<if test="sex != null and sex != ''">
sex = #{sex} and
</if>
<if test="email != null and email != ''">
email = #{email}
</if>
</trim>
</select>
choose、when、otherwise
choose、when、otherwise相当于if...else if..else
<select id="getEmpByCondition" resultType="com.qcby.pojo.Emp">
select * from t_emp
<where>
<choose>
<when test="empName != null and empName != ''">
emp_name = #{empName}
</when>
<when test="age != null and age != ''">
age = #{age}
</when>
<when test="sex != null and sex != ''">
sex = #{sex}
</when>
<when test="email != null and email != ''">
email = #{email}
</when>
<otherwise>
did = 1
</otherwise>
</choose>
</where>
</select>
foreach
属性:
- collection:设置要循环的数组或集合
- item:表示集合或数组中的每一个数据
- separator:设置循环体之间的分隔符
- open:设置foreach标签中的内容的开始符
- close:设置foreach标签中的内容的结束符
/**
* 通过数组批量删除
*/
Integer deleteMoreByArray(@Param("eids") Integer[] eids);
<delete id="deleteMoreByArray">
delete from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
</delete>
<delete id="deleteMoreByArray">
delete from t_emp where
<foreach collection="eids" item="eid" separator="or">
eid = #{eid}
</foreach>
</delete>
/**
* 批量插入
*/
Integer insertMoreByList(@Param("emps") List<Emp> emps);
<insert id="insertMoreByList">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.empName},#{emp.age},#{emp.age},#{emp.email},null)
</foreach>
</insert>
SQL片段
sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入
<sql id="empColumns">eid,emp_name,age,sex,email</sql>
<select id="getEmpByCondition1" resultType="com.qcby.pojo.Emp">
select <include refid="empColumns"></include> from t_emp
<where>
<choose>
<when test="empName != null and empName != ''">
emp_name = #{empName}
</when>
<when test="age != null and age != ''">
age = #{age}
</when>
<when test="sex != null and sex != ''">
sex = #{sex}
</when>
<when test="email != null and email != ''">
email = #{email}
</when>
<otherwise>
did = 1
</otherwise>
</choose>
</where>
</select>