My Batis 的官方定义:MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis可以对配置和原生Map使用简单的XML或注解,将接口和Java的POJO映射成数据库中的记录。可以看出,MyBatis是基于一种SQL到POJO的模型,它需要我们提供SQL、映射关系(XML或者注解,目前以XML 为主)和POJO 。与此同时,它还能支持动态SQL,以适应需求的变化,当前MyBatis渐渐成为市场的主流持久框架。MyBatis的配置文件包括两个大的部分,一是基础配置文件,一个是映射文件。在MyBatis中也可以使用注解来实现映射,只是由于功能和可读性的限制,在实际的项目使用得比较少,所以不介绍使用注解配置SQL的方式。
6.2.1 工程文件中添加依赖包:pom.xml
<!-- 导入mybatis依赖包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
mybatis-spring-boot-starter依赖:
- 自动检测现有的DataSource
- 创建并注册SqlSessionFactory的实例,该实例使用SqlSessionFactoryBean将该DataSource作为输入进行传递
- 创建并注册从SqlSessionFactory中获取的SqlSessionTemplate的实例。
- 自动扫描您的mappers,将它们链接到SqlSessionTemplate并将其注册到Spring上下文,以便将它们注入到您的bean中。
即当我们引入mybatis-spring-boot-starter之后,只需要在application.properties
定义一个DataSource,它会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。同时会自动扫描我们指定的mappers,注册到Spring上下文中。
6.2.2 配置数据源
application.properties配置MySQL数据源。
server.servlet.context-path=/gavin
server.port=9001
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://rm-wz98XXXXuo.mysql.rds.aliyuncs.com:3306/usermng?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=gavin
spring.datasource.password=gavinpw
注意:现在有些文章中可能存在记述错误的地方。
spring.datasource.driverClassName
如果设定为:com.mysql.jdbc.Driver
,会引起编译错误,给出的提示中已经说明了修改方法,将ClassName变更为:com.mysql.cj.jdbc.Driver
。
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
全局配置文件 application.properties中spring.datasource下只配置了数据库地址、连接驱动、账号、密码,默认使用的是HikariDataSource数据源(com.zaxxer.hikari.HikariDataSource
)。
6.2.3 使用mybatis-generator-gui自动生成代码
详细使用参考【11.1 使用mybatis-generator-gui自动生成mybatis关联代码】
也可以下载直接编译好的可执行的文件:
1. 生成用户信息代码
package com.gavinbj.confmng.persistence.entity;
import java.io.Serializable;
import java.util.Date;
/**
* UserInfo
* @author gavinbj
*/
public class UserInfo implements Serializable {
private String userId;
/**
* 用户姓名
*/
private String userName;
/**
* 个人介绍
*/
private String introduce;
/**
* 移动电话
*/
private String mobilephone;
/**
* 邮箱
*/
private String email;
/**
* 生日
*/
private Date birthday;
/**
* 性别
*/
private String gender;
private static final long serialVersionUID = 1L;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public String getMobilephone() {
return mobilephone;
}
public void setMobilephone(String mobilephone) {
this.mobilephone = mobilephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
UserInfo other = (UserInfo) that;
return (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
&& (this.getUserName() == null ? other.getUserName() == null : this.getUserName().equals(other.getUserName()))
&& (this.getIntroduce() == null ? other.getIntroduce() == null : this.getIntroduce().equals(other.getIntroduce()))
&& (this.getMobilephone() == null ? other.getMobilephone() == null : this.getMobilephone().equals(other.getMobilephone()))
&& (this.getEmail() == null ? other.getEmail() == null : this.getEmail().equals(other.getEmail()))
&& (this.getBirthday() == null ? other.getBirthday() == null : this.getBirthday().equals(other.getBirthday()))
&& (this.getGender() == null ? other.getGender() == null : this.getGender().equals(other.getGender()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
result = prime * result + ((getUserName