|
| 1 | +SpringBoot 整合 Mybatis 有两种常用的方式,一种就是我们常见的 xml 的方式 ,还有一种是全注解的方式。我觉得这两者没有谁比谁好,在 SQL 语句不太长的情况下,我觉得全注解的方式一定是比较清晰简洁的。但是,复杂的 SQL 确实不太适合和代码写在一起。 |
| 2 | + |
| 3 | +<!-- MarkdownTOC --> |
| 4 | + |
| 5 | +- [一 开发前的准备](#一-开发前的准备) |
| 6 | + - [1.1 环境参数](#11-环境参数) |
| 7 | + - [1.2 创建工程](#12-创建工程) |
| 8 | + - [1.3 创建数据库和 user 用户表](#13-创建数据库和-user-用户表) |
| 9 | + - [1.4 配置 pom 文件中的相关依赖](#14-配置-pom-文件中的相关依赖) |
| 10 | + - [1.5 配置 application.properties](#15-配置-applicationproperties) |
| 11 | + - [1.6 创建用户类 Bean](#16-创建用户类-bean) |
| 12 | +- [二 全注解的方式](#二-全注解的方式) |
| 13 | + - [2.1 Dao 层开发](#21-dao-层开发) |
| 14 | + - [2.2 service 层](#22-service-层) |
| 15 | + - [2.3 Controller 层](#23-controller-层) |
| 16 | + - [2.4 启动类](#24-启动类) |
| 17 | + - [2.5 简单测试](#25-简单测试) |
| 18 | +- [三 xml 的方式](#三-xml-的方式) |
| 19 | + - [3.1 Dao 层的改动](#31-dao-层的改动) |
| 20 | + - [3.2 配置文件的改动](#32-配置文件的改动) |
| 21 | + |
| 22 | +<!-- /MarkdownTOC --> |
| 23 | + |
| 24 | +下面就开始吧! |
| 25 | + |
| 26 | +## 一 开发前的准备 |
| 27 | + |
| 28 | +### 1.1 环境参数 |
| 29 | + |
| 30 | +- 开发工具:IDEA |
| 31 | +- 基础工具:Maven+JDK8 |
| 32 | +- 所用技术:SpringBoot+Mybatis |
| 33 | +- 数据库:MySQL |
| 34 | +- SpringBoot版本:2.1.0 |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +### 1.2 创建工程 |
| 39 | + |
| 40 | +创建一个基本的 SpringBoot 项目,我这里就不多说这方面问题了,具体可以参考下面这篇文章: |
| 41 | + |
| 42 | +[https://blog.csdn.net/qq_34337272/article/details/79563606](https://blog.csdn.net/qq_34337272/article/details/79563606) |
| 43 | + |
| 44 | + |
| 45 | +### 1.3 创建数据库和 user 用户表 |
| 46 | + |
| 47 | +我们的数据库很简单,只有 4 个字段:用户 id、姓名、年龄、余额,如下图所示: |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +添加了“余额money”字段是为了给大家简单的演示一下事务管理的方式。 |
| 52 | + |
| 53 | +**建表语句:** |
| 54 | + |
| 55 | +```sql |
| 56 | +CREATE TABLE `user` ( |
| 57 | + `id` int(13) NOT NULL AUTO_INCREMENT COMMENT '主键', |
| 58 | + `name` varchar(33) DEFAULT NULL COMMENT '姓名', |
| 59 | + `age` int(3) DEFAULT NULL COMMENT '年龄', |
| 60 | + `money` double DEFAULT NULL COMMENT '账户余额', |
| 61 | + PRIMARY KEY (`id`) |
| 62 | +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
| 63 | +``` |
| 64 | + |
| 65 | +### 1.4 配置 pom 文件中的相关依赖 |
| 66 | + |
| 67 | +由于要整合 springboot 和 mybatis 所以加入了artifactId 为 mybatis-spring-boot-starter 的依赖,由于使用了Mysql 数据库 所以加入了artifactId 为 mysql-connector-java 的依赖。 |
| 68 | + |
| 69 | +```xml |
| 70 | + <dependencies> |
| 71 | + <dependency> |
| 72 | + <groupId>org.springframework.boot</groupId> |
| 73 | + <artifactId>spring-boot-starter-web</artifactId> |
| 74 | + </dependency> |
| 75 | + <dependency> |
| 76 | + <groupId>org.mybatis.spring.boot</groupId> |
| 77 | + <artifactId>mybatis-spring-boot-starter</artifactId> |
| 78 | + <version>1.3.2</version> |
| 79 | + </dependency> |
| 80 | + <dependency> |
| 81 | + <groupId>mysql</groupId> |
| 82 | + <artifactId>mysql-connector-java</artifactId> |
| 83 | + <scope>runtime</scope> |
| 84 | + </dependency> |
| 85 | + <dependency> |
| 86 | + <groupId>org.springframework.boot</groupId> |
| 87 | + <artifactId>spring-boot-starter-test</artifactId> |
| 88 | + <scope>test</scope> |
| 89 | + </dependency> |
| 90 | + </dependencies> |
| 91 | +``` |
| 92 | + |
| 93 | +### 1.5 配置 application.properties |
| 94 | + |
| 95 | +由于我使用的是比较新的Mysql连接驱动,所以配置文件可能和之前有一点不同。 |
| 96 | + |
| 97 | +```properties |
| 98 | +server.port=8333 |
| 99 | +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/erp?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 |
| 100 | +spring.datasource.username=root |
| 101 | +spring.datasource.password=153963 |
| 102 | +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver |
| 103 | +``` |
| 104 | + |
| 105 | +注意:我们使用的 mysql-connector-java 8+ ,JDBC 连接到mysql-connector-java 6+以上的需要指定时区 `serverTimezone=GMT%2B8`。另外我们之前使用配置 Mysql数据连接是一般是这样指定`driver-class-name=com.mysql.jdbc.Driver`,但是现在不可以必须为 否则控制台下面的异常: |
| 106 | + |
| 107 | +```error |
| 108 | +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. |
| 109 | +``` |
| 110 | + |
| 111 | +上面异常的意思是:`com.mysql.jdbc.Driver` 被弃用了。新的驱动类是 `com.mysql.cj.jdbc.Driver`。驱动程序通过SPI自动注册,手动加载类通常是不必要。 |
| 112 | + |
| 113 | +如果你非要写把`com.mysql.jdbc.Driver` 改为`com.mysql.cj.jdbc.Driver `即可。 |
| 114 | + |
| 115 | +### 1.6 创建用户类 Bean |
| 116 | + |
| 117 | +```java |
| 118 | +public class User { |
| 119 | + private int id; |
| 120 | + private String name; |
| 121 | + private int age; |
| 122 | + private double money; |
| 123 | + ... |
| 124 | + 此处省略getter、setter以及 toString方法 |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## 二 全注解的方式 |
| 129 | + |
| 130 | +先来看一下 全注解的方式,这种方式和后面提到的 xml 的方式的区别仅仅在于 一个将 sql 语句写在 java 代码中,一个写在 xml 配置文件中。全注方式解转换成 xml 方式仅需做一点点改变即可,我在后面会提到。 |
| 131 | + |
| 132 | +**项目结构:** |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +### 2.1 Dao 层开发 |
| 137 | + |
| 138 | + |
| 139 | +UserDao.java |
| 140 | + |
| 141 | +```java |
| 142 | +@Mapper |
| 143 | +public interface UserDao { |
| 144 | + /** |
| 145 | + * 通过名字查询用户信息 |
| 146 | + */ |
| 147 | + @Select("SELECT * FROM user WHERE name = #{name}") |
| 148 | + User findUserByName(@Param("name") String name); |
| 149 | + |
| 150 | + /** |
| 151 | + * 查询所有用户信息 |
| 152 | + */ |
| 153 | + @Select("SELECT * FROM user") |
| 154 | + List<User> findAllUser(); |
| 155 | + |
| 156 | + /** |
| 157 | + * 插入用户信息 |
| 158 | + */ |
| 159 | + @Insert("INSERT INTO user(name, age,money) VALUES(#{name}, #{age}, #{money})") |
| 160 | + void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("money") Double money); |
| 161 | + |
| 162 | + /** |
| 163 | + * 根据 id 更新用户信息 |
| 164 | + */ |
| 165 | + @Update("UPDATE user SET name = #{name},age = #{age},money= #{money} WHERE id = #{id}") |
| 166 | + void updateUser(@Param("name") String name, @Param("age") Integer age, @Param("money") Double money, |
| 167 | + @Param("id") int id); |
| 168 | + |
| 169 | + /** |
| 170 | + * 根据 id 删除用户信息 |
| 171 | + */ |
| 172 | + @Delete("DELETE from user WHERE id = #{id}") |
| 173 | + void deleteUser(@Param("id") int id); |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +### 2.2 service 层 |
| 178 | + |
| 179 | +```java |
| 180 | +@Service |
| 181 | +public class UserService { |
| 182 | + @Autowired |
| 183 | + private UserDao userDao; |
| 184 | + |
| 185 | + |
| 186 | + /** |
| 187 | + * 根据名字查找用户 |
| 188 | + */ |
| 189 | + public User selectUserByName(String name) { |
| 190 | + return userDao.findUserByName(name); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * 查找所有用户 |
| 195 | + */ |
| 196 | + public List<User> selectAllUser() { |
| 197 | + return userDao.findAllUser(); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * 插入两个用户 |
| 202 | + */ |
| 203 | + public void insertService() { |
| 204 | + userDao.insertUser("SnailClimb", 22, 3000.0); |
| 205 | + userDao.insertUser("Daisy", 19, 3000.0); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * 根据id 删除用户 |
| 210 | + */ |
| 211 | + |
| 212 | + public void deleteService(int id) { |
| 213 | + userDao.deleteUser(id); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * 模拟事务。由于加上了 @Transactional注解,如果转账中途出了意外 SnailClimb 和 Daisy 的钱都不会改变。 |
| 218 | + */ |
| 219 | + @Transactional |
| 220 | + public void changemoney() { |
| 221 | + userDao.updateUser("SnailClimb", 22, 2000.0, 3); |
| 222 | + // 模拟转账过程中可能遇到的意外状况 |
| 223 | + int temp = 1 / 0; |
| 224 | + userDao.updateUser("Daisy", 19, 4000.0, 4); |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +``` |
| 229 | + |
| 230 | +### 2.3 Controller 层 |
| 231 | + |
| 232 | +```java |
| 233 | +@RestController |
| 234 | +@RequestMapping("/user") |
| 235 | +public class UserController { |
| 236 | + @Autowired |
| 237 | + private UserService userService; |
| 238 | + |
| 239 | + @RequestMapping("/query") |
| 240 | + public User testQuery() { |
| 241 | + return userService.selectUserByName("Daisy"); |
| 242 | + } |
| 243 | + |
| 244 | + @RequestMapping("/insert") |
| 245 | + public List<User> testInsert() { |
| 246 | + userService.insertService(); |
| 247 | + return userService.selectAllUser(); |
| 248 | + } |
| 249 | + |
| 250 | + |
| 251 | + @RequestMapping("/changemoney") |
| 252 | + public List<User> testchangemoney() { |
| 253 | + userService.changemoney(); |
| 254 | + return userService.selectAllUser(); |
| 255 | + } |
| 256 | + |
| 257 | + @RequestMapping("/delete") |
| 258 | + public String testDelete() { |
| 259 | + userService.deleteService(3); |
| 260 | + return "OK"; |
| 261 | + } |
| 262 | + |
| 263 | +} |
| 264 | +``` |
| 265 | + |
| 266 | +### 2.4 启动类 |
| 267 | + |
| 268 | +```java |
| 269 | +//此注解表示SpringBoot启动类 |
| 270 | +@SpringBootApplication |
| 271 | +// 此注解表示动态扫描DAO接口所在包,实际上不加下面这条语句也可以找到 |
| 272 | +@MapperScan("top.snailclimb.dao") |
| 273 | +public class MainApplication { |
| 274 | + |
| 275 | + public static void main(String[] args) { |
| 276 | + SpringApplication.run(MainApplication.class, args); |
| 277 | + } |
| 278 | + |
| 279 | +} |
| 280 | +``` |
| 281 | + |
| 282 | +### 2.5 简单测试 |
| 283 | + |
| 284 | +上述代码经过测试都没问题,这里贴一下根据姓名查询的测试的结果。 |
| 285 | + |
| 286 | + |
| 287 | + |
| 288 | +## 三 xml 的方式 |
| 289 | + |
| 290 | +**项目结构:** |
| 291 | + |
| 292 | + |
| 293 | +相比于注解的方式主要有以下几点改变,非常容易实现。 |
| 294 | + |
| 295 | +### 3.1 Dao 层的改动 |
| 296 | + |
| 297 | +我这里只演示一个根据姓名找人的方法。 |
| 298 | + |
| 299 | +**UserDao.java** |
| 300 | + |
| 301 | +```java |
| 302 | +@Mapper |
| 303 | +public interface UserDao { |
| 304 | + /** |
| 305 | + * 通过名字查询用户信息 |
| 306 | + */ |
| 307 | + User findUserByName(String name); |
| 308 | + |
| 309 | +} |
| 310 | +``` |
| 311 | + |
| 312 | +**UserMapper.xml** |
| 313 | + |
| 314 | +```xml |
| 315 | +<?xml version="1.0" encoding="UTF-8"?> |
| 316 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
| 317 | + "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 318 | + |
| 319 | +<mapper namespace="top.snailclimb.dao.UserDao"> |
| 320 | + |
| 321 | + <select id="findUserByName" parameterType="String" resultType="top.snailclimb.bean.User"> |
| 322 | + SELECT * FROM user WHERE name = #{name} |
| 323 | + </select> |
| 324 | +</mapper> |
| 325 | + |
| 326 | +``` |
| 327 | + |
| 328 | +### 3.2 配置文件的改动 |
| 329 | + |
| 330 | +配置文件中加入下面这句话: |
| 331 | + |
| 332 | +`mybatis.mapper-locations=classpath:mapper/*.xml` |
| 333 | + |
| 334 | + |
| 335 | + |
0 commit comments