引入依赖mybatis-spring-boot-starter:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
mybatis配置绑定类 MybatisProperties.class
@ConfigurationProperties(prefix="mybatis")
指定mybatis全局配置文件和sql映射文件位置
我们甚至可以不用去指定全局配置文件,而使用mybatis.configuration去代替它。
开启驼峰命名规则的2种方式:
如数据库里这个字段是这样的user_id,实体类里是这样的userId。mybatis默认不开启驼峰命名规则。即默认情况下无法匹配。
1.在主配置文件mybatis-config.xml开启驼峰规则
2.在配置文件中mybatis.configuration开启,注意下图是有问题的,要去掉config-location配置,因为它和configuration不能同时存在,这样才能生效。这也就是我们上面说的可以不写全局配置文件
使用mybatis还有一个重中之重,@Mapper。
告诉mybatis这是一个Mapper接口,来操作数据库
@Mapper
public interface AccountMapper{
public Account getAcct(Long id);
}
1》单个参数,mybatis不做任何处理,你这里#{id}可以是id,也可以是别的。
public Employee getEmpById(Integet id)
<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where id=#{id}
</select>
2》俩个参数mybatis就会帮我们做处理了,自动帮我们封装为map集合。默认是param1,param2
推荐使用命名参数,@Param。明确指定封装封装参数时map的key。多个参数会被mybatis封装成一个map。
public Employee getEmpByIdAndLastName(@Param("id") Integer id,@Param("lastName")String lastName)
key:使用@Param指定的值
value:参数值
#{指定的key}取出对应的参数值
3》参数是一个map集合
public Employee getEmpByMap(Map<String,Object> map);
如果多个参数不是业务业务模型中的数据,没有对应的pojo,为了方面,可以传入map。
通过#{key},取出map对应的值
4》POJO如果多个参数正好是我们业务逻辑的数据模型,我们可以直接传入POJO;
#{属性名}:取出传入的pojo的属性值
5》如果多个参数不是业务模型的数据,但经常使用。推荐编写一个TO(Transfer Object)数据传输对象。
resultMap:自定义结果集
<id/>:指定主键列的封装规则
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmp">
<id column="id" property="id"/>
</resultMap>
<result/>定义普通列封装规则
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmp">
<result column="last_name" property="lastName"/>
</resultMap>
要是javaBean这样的呢,属性里有一个是对象。如下图:
public class Employee{
private Integer id;
private String lastName;
private String email;
private String gender;
private Department dept;
}
public class Department{
private Integer id;
private String departmentName;
}
多表联查的情况下,resultMap该怎么写呢
resultMap第一种写法 级联属性封装结果
<!--级联属性封装结果-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>
</resultMap>
resultMap第二种写法 使用association指定联合的javaBean对象
<!--使用association指定联合的javaBean对象-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<association property="dept" javaType="com.atguigu.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association>
</resultMap>
使用association还可以做到分步查询
原理,执行俩条sql
select * from tbl_employee where id=1;
select * from tbl_dept where id=1;
select:表名当前属性是调用select的方法查出的结果
column:指定将哪一列的值传给这个方法
<!--使用select指定的方法查出对象,并封装给property指定的属性-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<association property="dept"
select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById">
<id column="d_id" />
</association>
</resultMap>