常见问题
MyBatis出现The server time zone value如何解决
- 将url后面加上 ?serverTimezone=UTC就可以了
jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
插入操作时候出现id不能为空的情况
- 有可能是你的创建表的时候没有设置表可以自增
- 有可能是你的属性变量用的是int而不是Integer
处理多对一关系时候的一些问题
Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘list’ in ‘class com.situ.mybatis.entity.Department’
- 这是由于resultMap中的属性和你当前类的属性不一致导致的反射异常,可以好好检查一下自己的reusultMap中的属性和自己类的属性是不一样的
学习笔记
解决字段名和属性名不一致的情况(驼峰规则)
- 为字段起一个别名,保持和属性名一致
- 设置全局配置,将_自动映射为驼峰
<settings>
<!-- mapUnderscoreToCamelCase
将sql中的字段中的下划线自动映射为驼峰 例如 emp_name -> empName-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
- 通过mapper配置文件中的resultmap来设置
<resultMap id="empResultMap" type="Emp">
<!--id 表示的是主键 result就是普通的-->
<!--property对应是属性 column是字段 -->
<id property="eid" column="eid"></id>
<!--这里就是将字段中的emp_name映射到empName-->
<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>
</resultMap>
<select id="getAllEmp2" resultMap="empResultMap">
select * from t_emp;
</select>
如何解决多对一的映射关系
通过级联属性赋值解决
<!--注意这里对的属性一定要和类中属性一一对应--->
<resultMap id="empAndDeptResultMapOne" type="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>
<!-- Emp getEmpAndDept(@Param("eid") Integer eid);-->
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">
select * from t_emp left join t_dept on t_emp.did=t_dept.did where t_emp.eid =#{eid};
</select>
通过association解决
- 其实就是把他当做类来映射然后在处理一下类里面的映射关系
<!-- association:处理多对一的映射关系
javaType:该属性的类型
-->
<resultMap id="empAndDeptResultMapTwo" type="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="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
</association>
</resultMap>
通过分布查询解决(推荐)
-
分布查询的优点就是可以进行延迟加载
-
下面这个例子就是说我分2步查询得到结果
-
第一步就是先通过员工的id查询该员工所在信息
-
第二步在通过该员工的部门编号查询他的部门信息
-
这是第一个EmpMaper的配置文件
<resultMap id="empAndDeptByStepResultMap" type="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.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did"
fetchType="eager">
</association>
</resultMap>
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
select * from t_emp where eid = #{eid};
</select>
- 这是第二个DeptMapper的配置文件
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from t_dept where did = #{did};
</select>
延迟加载
- lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
- aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个
属性会按需加载 - 此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和
collection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType=“lazy(延迟加
载)|eager(立即加载)”
<resultMap id="empAndDeptByStepResultMap" type="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.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did"
fetchType="eager"><!---这里就是设置立即加载的-->
</association>
</resultMap>
通过conllection解决一对多的映射关系
- conllection 解决一对多 里面的ofType:表示该所对应的集合中存储数据的类型
<resultMap id="getDeptAndEmpResultMapp" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps" ofType="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="empName"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
</collection>
</resultMap>
<!-- Dept getDeptAndEmp(@Param("did") Integer did);-->
<select id="getDeptAndEmp" resultMap="getDeptAndEmpResultMapp">
select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did};
</select>
- 通过分布查询
关键字 | 作用 |
---|---|
collection | 表示这个操作返回的是一个集合 |
select | 下一步操作所在的地方的全类名 |
column | 表示根据那个字段来连接 |
- 第一步
<resultMap id="getDeptAndEmpByStepResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps" select="com.atguigu.mybatis.mapper.EmpMapper.getDetAndEmpByStepTwo" column="did">
</collection>
</resultMap>
<!-- Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
<select id="getDeptAndEmpByStepOne" resultMap="getDeptAndEmpByStepResultMap">
select * from t_dept where did = #{did};
</select>
- 第二步
<select id = "getDetAndEmpByStepTwo" resultType="Emp">
select * from t_emp where did = #{did};
</select>
动态SQL
if关键字
- 按照常规的想法第一个if里面是不用加and的并且where后面也是不用1=1这个恒成立的条件的,但是这样会出现一个问题。就是如果第一个if条件不成立那么后面的if条件成立的话,就会出现and在where后面,从而出现语法错误。但是如果加上了这个1=1的恒成立条件那么后面的if都用上and的可以了
<select id="getEmpByCondition" resultType="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 = #{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标签,那么如果后面有一个以上的条件成立就会自动生成where,反之如果没有一个条件成立就不会生成where
- where标签会自动去掉内容前面的and或者or去掉 不能去掉后面的and或or
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<where>
<if test="empName != null and empName != ''">
emp_name = #{empName}
</if>
<if test="age != null">
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标签中前面添加指定内容 |
suffix | 在trim标签中后面添加指定内容 |
prefixOverrides | 标签中前面删除指定内容 |
suffixOverrides | 语句后面删除指定内容 |
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<trim prefix="where" suffixOverrides="and|or" prefixOverrides="and|or">
<if test="empName != null and empName != ''">
emp_name = #{empName} and
</if>
<if test="age != null">
age = #{age} and
</if>
<if test="sex != null and sex != ''">
sex = #{sex} and
</if>
<if test="email != null and email != ''">
email = #{email} and
</if>
</trim>
</select>
choose when otherwise
- 这一段的内容相当于java中的witch语句 只要有一个满足就执行when中里面的其他的when不在执行 如果都不满足就执行otherwise里面的内容
<!-- List<Emp> getEmpByChoose(Emp emp);-->
<select id="getEmpByChoose" resultType="Emp">
select * from t_emp
<where>
<choose>
<when test="empName!=null and empName!=''">
emp_name = #{empName};
</when>
<when test="age!=null and age!=''">
emp_name = #{empName};
</when>
<when test="sex!=null and sex!=''">
emp_name = #{empName};
</when>
<when test="email!=null and email!=''">
emp_name = #{empName};
</when>
<otherwise>did = 1</otherwise>
</choose>
</where>
</select>
froeach标签
标签属性 | 功能 |
---|---|
collection | 传进来的集合 |
item | 用于遍历集合里面的每一个元素 |
separator | 用于添加遍历每个元素之间的分隔符 |
open | 在foreach标签前添加指定内容 |
close | 在foreach标签后添加指定内容 |
- foreach用来删除的两种方式
<!-- int deleteMoreByArray(@Param("eids") Integer[] eids);-->
<delete id="deleteMoreByArray">
<!-- 这个eid是数据库里面的eid -->
delete from t_emp where eid in
<!--separator表示用什么分割 -->
<foreach collection="eids" item="eid" separator="," open="(" close=")"> <!--eid表示数组里面的每一个元素 -->
#{eid}
</foreach>
</delete>
<delete id="deleteMoreByArrayTwo">
delete from t_emp where
<foreach collection="eids" item="eid" separator="or">
eid = #{eid}
</foreach>
</delete>
foreach用来添加
<insert id="insertMoreByList">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null, #{emp.empName}, #{emp.age}, #{emp.sex}, #{emp.email}, null)
</foreach>
</insert>
动态sql
如何创建sql片段
<sql id="empColumns">eid, emp_name, age, sex, email</sql>
如何引用sql片段
select <include refid="empColumns"></include> from t_emp
Mybatis只逆向工程
需要配置一个文件为generatorConfig.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- targetRuntime: 执行生成的逆向工程的版本
MyBatis3Simple: 生成基本的CRUD(清新简洁版)
MyBatis3: 生成带条件的CRUD(奢华尊享版) -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 数据库的连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"
userId="root" password="root">
</jdbcConnection>
<!-- javaBean的生成策略-->
<javaModelGenerator targetPackage="com.atguigu.mybatis.pojo"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- SQL映射文件的生成策略 -->
<sqlMapGenerator targetPackage="com.atguigu.mybatis.mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Mapper接口的生成策略 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.atguigu.mybatis.mapper"
targetProject=".\src\main\java">
<property name="enableSubPackages"
value="true" />
</javaClientGenerator>
<!-- 逆向分析的表 -->
<!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
<!-- domainObjectName属性指定生成出来的实体类的类名 -->
<table tableName="t_emp" domainObjectName="Emp"/>
<table tableName="t_dept" domainObjectName="Dept"/>
</context>
</generatorConfiguration>
逆向工程中需要注意的地方
对于逆向工程的更新和添加操作分别有普通版本和选择版本 对于普通版本如果更新的属性中有null那么该属性对于的字段也会被更新成null, 但是如果是选择性版本,则该字段不会被更新!
分页插件
依赖配置 pom.xml中
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
在mybati-config中配置分页插件
<plugins> <!--设置分页插件-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
属性 | 功能 |
---|---|
index | 当前页的索引 |
pageSize | 每页显示的条数 |
pageNum | 当前页的页码 |