本章内容讲述如何用SpringBoot整合Mybatis,完成对数据库的操作。
首先准备一个至少有一张表的数据库,以做测试。
整个项目结构如下图所示:
一、在pom.xml文件中导入相关依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wx</groupId>
<artifactId>medical</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>medical</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- 整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- 连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、完成配置文件里对于数据库的相关配置数据:
server:
port: 8088
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名称?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 数据库密码
这里我采用的是yml后缀,因此遵从yml的书写格式,":"后记得跟一个空格。
三、创建相关实体类,给出无参、有参、set、get方法,也可以用lombok的@Data注解,该注解提供了set、get,重写了toString、equals方法,若有需要,可以用@NoArgsConstructor(无参构造方法),@AllArgsConstructor(有参构造方法):
public class UserEntity {
private Integer id;
private String userName;
private String userPassword;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Timestamp userTime;
private Integer type;
public UserEntity() {
}
public UserEntity(Integer id, String userName, String userPassword, Timestamp userTime, Integer type) {
this.id = id;
this.userName = userName;
this.userPassword = userPassword;
this.userTime = userTime;
this.type = type;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Timestamp getUserTime() {
return userTime;
}
public void setUserTime(Timestamp userTime) {
this.userTime = userTime;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", userTime=" + userTime +
", type=" + type +
'}';
}
}
四、完成mapper接口,记得为mapper接口加上@Mapper注解,这里以查询全部数据为例:
@Mapper
public interface UserMapper {
List<UserEntity> selectUserAll(UserEntity userEntity);
}
五、完成mapper.xml,这里我用的是xml,若想直接在方法上书写sql语句,可以用@Select注解,如下所示:
@Mapper
public interface UserMapper {
@Select("select * from user")
List<UserEntity> selectUserAll(UserEntity userEntity);
}
但我个人更喜欢用xml的方式,如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wx.mapper.UserMapper">
<select id="selectUserAll" resultType="com.wx.entity.UserEntity">
select * from user
</select>
</mapper>
注意:xml有两种扫描的方式,一种是在application.yml中添加对xml文件是扫描,这里不提供,若有需要请自行搜索;一种是在resource包下创建与你mapper接口相同的路径,但要提一点的是,在resource下创建多层级包不能使用".",比如"com.wx.mapper.UserMapper.xml",而是使用"/"的方式,比如:"com/wx/mapper/UserMapper.xml",或者一层一层的创建也可以。路径名相同后会讲接口和xml文件放于同一包下,这样就不用额外为xml添加扫描,如下图所示:
六、完成service以及serviceImpl,记得为impl实现类添加@Service注解:
service:
public interface IService<T> {
List<T> getAll(T t);
}
serviceImpl
@Service
public class UserServiceImpl implements IService<UserEntity> {
@Autowired
UserMapper userMapper;
@Override
public List<UserEntity> getAll(UserEntity userEntity) {
return userMapper.selectUserAll(userEntity);
}
}
七、完成Controller层的书写:
@RestController
public class HelloController {
@Autowired
IService userService;
@RequestMapping("/getUsers")
public List<UserEntity> hello() {
UserEntity userEntity = new UserEntity();
return userService.getAll(userEntity);
}
}
注意:@RestController 等同于 @Controller + @ResPonseBody,其中@Controller是讲该方法标记为控制层,被扫描到时交由Ioc容器管理,@ResPonseBody让控制层返回json数据,而不是返回页面,因此若有需要可以将@RestController分开写。
@RequestMapping("/getUsers")的作用是将该方法暴露成资源以供浏览器通过url路径访问。
八、最后,启动入口函数(为什么没有配置tomcat,因为SpringBoot内部集成了一个tomcat),在浏览器地址栏输入设定的url访问:
至此,SpringBoot整合Mybatis到此完成!