项目地址:https://github.com/Gang-bb/Gangbb-Vue
历史遗留TODO:无
本章将留下TODO:
- 第三章
- mybatis缓存暂时没用到,后面整合redis后用redis做缓存(整合Redis完成)。
- 重写mybatis配置文件扫描mapper(日志整合章节完成)
- 整合暂时没有用到druid数据源,后续会用到
1. 添加依赖
整体思路:
-
父项目:引入
pagehelper-spring-boot-starter
(后面分页实现使用的分页插件),并规定版本号。pagehelper-spring-boot-starter
内含依赖:(主要是有整合mybatis的驱动mybatis-spring-boot-starter
,引入它就可以不用再声明引入mybatis驱动),如下图:
-
Gangbb-common
:引入pagehelper-spring-boot-starter
不用规定版本号,在此引入是因为分页工具以后在该Module中封装。 -
Gangbb-admin
:引入mysql-connector-java
mysql驱动,再引入Gangbb-common
就相当于有了它所有依赖(包括分页的扩展和mybatis驱动)
1.1 Gangbb-Vue的pom.xml
<properties>
<pagehelper.boot.version>1.3.0</pagehelper.boot.version>
</properties>
<!--统一项目整体依赖版本-->
<dependencyManagement>
<dependencies>
<!-- pagehelper 分页插件 begin-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.boot.version}</version>
</dependency>
<!-- pagehelper 分页插件 end-->
</dependencies>
</dependencyManagement>
1.2 Gangbb-common的pom.xml
<dependencies>
<!-- pagehelper 分页插件 begin-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<!-- pagehelper 分页插件 end-->
</dependencies>
1.3 Gangbb-admin的pom.xml
<dependencies>
<!--mysql驱动 begin-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mysql驱动 end-->
<!--项目common通用工具类 begin(下一章该文件删除该依赖)-->
<dependency>
<groupId>com.gangbb</groupId>
<artifactId>Gangbb-common</artifactId>
</dependency>
<!--项目common通用工具类 end-->
</dependencies>
2. 准备数据库表
建库:
CREATE DATABASE `gangbb` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';
建测试表:
CREATE TABLE `test-mybatis` (
`id` int(11) NOT NULL COMMENT '主键id',
`name` varchar(50) DEFAULT NULL COMMENT '姓名',
`gender` varchar(50) DEFAULT NULL COMMENT '性别',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
数据自行添加!
3. 编写配置文件
application.yml
spring:
datasource:
name: gangbb_datasource
url: jdbc:mysql://127.0.0.1:3306/gangbb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
devtools:
# 热部署生效
add-properties: true
# 设置重启的目录
restart:
additional-paths: src/main/java
mybatis:
configuration:
# 实体类驼峰命名转成数据库中的下划线命名
map-underscore-to-camel-case: true
# 指定 MyBatis 所用日志的具体实现
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
# 全局映射器启用缓存
cache-enabled: true
# 允许 JDBC 支持自动生成主键
use-generated-keys: true
# 配置默认的执行器
default-executor-type: reuse
# 声明 XxxMapper.xml文件路径
mapper-locations: mapper/*.xml
# 扫描实体类路径
type-aliases-package: com.gangbb.test.model.entity
default-executor-type
详解:https://blog.csdn.net/fengshuiyue/article/details/89222654
use-generated-keys
详解:https://blog.csdn.net/u012060033/article/details/79948353就是
cache-enabled
详解:https://blog.csdn.net/canot/article/details/51491732缓存暂时没用到,后面整合redis后用redis做缓存。
4. 创建各层测试
新建各层文件:
一、TestMybatis
实体类
public class TestMybatis implements Serializable {
private static final long serialVersionUID = -15945377879783826L;
/**
* 主键id
*/
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 性别
*/
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
二、dao层TestMybatisMapper
@Mapper
public interface TestMybatisMapper {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
TestMybatis queryById(Integer id);
}
三、TestMybatisService
public interface TestMybatisService {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
TestMybatis queryById(Integer id);
}
四、TestMybatisServiceImpl
@Service("testMybatisService")
public class TestMybatisServiceImpl implements TestMybatisService {
@Resource
private TestMybatisMapper testMybatisMapper;
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public TestMybatis queryById(Integer id) {
return this.testMybatisMapper.queryById(id);
}
}
五、TestMybatisMapper.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.gangbb.test.model.dao.TestMybatisMapper">
<resultMap type="com.gangbb.test.model.entity.TestMybatis" id="TestMybatisMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="gender" column="gender" jdbcType="VARCHAR"/>
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="TestMybatisMap">
select
id, name, gender
from `test-mybatis`
where id = #{id}
</select>
</mapper>
六、TestMybatisController
@RestController
@RequestMapping("testMybatis")
public class TestMybatisController {
/**
* 服务对象
*/
@Resource
private TestMybatisService testMybatisService;
/**
* 通过主键查询单条数据
*
* @return 单条数据
*/
@GetMapping("/selectOne")
public TestMybatis selectOne() {
return this.testMybatisService.queryById(1);
}
}
测试结果: