SpringBoot集成Mybatis
使用 SpringBoot 开发企业项目时,持久层数据访问是前端页面数据展示的基础,SpringBoot 支持市面上常见的关系库产品 (Oracle、Mysql、SqlServer、DB2等) 对应的相关持久层框架,当然除了对于关系库访问的支持,也支持当下众多的非关系库 (Redis、Solr、MongoDB等) 数据访问操作,这里主要介绍 SpringBoot 集成 Mybatis 并实现持久层数据基本增删改查操作。
环境整合配置
IDEA下创建 Maven 普通工程
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxxx</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springboot_mybatis</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis 集成 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- springboot 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- c3p0 数据源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml 整合配置
server:
# 设置项目启动的端口号
port: 8080
# 设置项目的访问路径(上下文路径)
servlet:
context-path: /springboot_mybatis
## 数据源配置
spring:
datasource:
type: com.mchange.v2.c3p0.ComboPooledDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
## mybatis 配置
mybatis:
#映射文件的存放路径
mapper-locations: classpath:/mappers/*.xml
type-aliases-package: com.xxxx.springboot.po
configuration:
## 下划线转驼峰配置
map-underscore-to-camel-case: true
## pageHelper
pagehelper:
helper-dialect: mysql
## 显示dao 执行sql语句
logging:
level:
com:
xxxx:
springboot:
dao: debug
源代码添加
JavaBean 对象定义
package com.xxxx.springboot.po;
public class User {
private Integer userId;
private String userName;
private String userEmail;
private String userAge;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
}
Dao层接口方法定义
com.xxxx.springboot.dao 包下创建 UserMapper.java 接口声明查询方法
package com.xxxx.springboot.dao;
import com.xxxx.springboot.po.User;
public interface UserDao {
//通过用户名查询用户对象
public User queryUserByName(String userName);
}
SQL映射文件添加
resources/mappers 目录下添加 UserMapper.xml 配置,查询 statetment
<?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.xxxx.springboot.dao.UserDao">
<!--查询操作-->
<select id="queryUserByName" parameterType="string" resultType="com.xxxx.springboot.po.User">
select * from student where user_name = #{userName}
</select>
</mapper>
添加Service层代码
package com.xxxx.springboot.service;
import com.xxxx.springboot.dao.UserDao;
import com.xxxx.springboot.po.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserService {
@Resource
private UserDao userDao;
public User queryUserByName(String name){
User user = userDao.queryUserByName(name);
return user;
}
}
添加Controller层代码
package com.xxxx.springboot.controller;
import com.xxxx.springboot.po.User;
import com.xxxx.springboot.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserService userService;
/**
* 通过用户名查询用户对象
* @param uname
* @return
*/
@GetMapping("user/{uname}")
public User queryUserByName(@PathVariable String uname){
User user = userService.queryUserByName(uname);
return user;
}
}
添加应用启动入口
package com.xxxx.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//扫描指定包下的接口类
@MapperScan("com.xxxx.springboot.dao")
public class Starter {
public static void main(String[] args){
SpringApplication.run(Starter.class);
}
}
启动测试
运行 Starter main方法,启动应用浏览器测试查询
后端日志打印效果 :