Skip to content

Commit 88d1eaa

Browse files
committed
✨ spring-boot-demo-orm-mybatis 完成
1 parent 05a8e24 commit 88d1eaa

File tree

13 files changed

+676
-0
lines changed

13 files changed

+676
-0
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<module>spring-boot-demo-template-enjoy</module>
2323
<module>spring-boot-demo-orm-jdbctemplate</module>
2424
<module>spring-boot-demo-orm-jpa</module>
25+
<module>spring-boot-demo-orm-mybatis</module>
2526
<module>spring-boot-demo-email</module>
2627
<module>spring-boot-demo-upload</module>
2728
<module>spring-boot-demo-war</module>
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
+256
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
# spring-boot-demo-orm-mybatis
2+
3+
> 此 demo 演示了 Spring Boot 如何与原生的 mybatis 整合,使用了 mybatis 官方提供的脚手架 `mybatis-spring-boot-starter `可以很容易的和 Spring Boot 整合。
4+
5+
## pom.xml
6+
7+
```xml
8+
<?xml version="1.0" encoding="UTF-8"?>
9+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>spring-boot-demo-orm-mybatis</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<packaging>jar</packaging>
16+
17+
<name>spring-boot-demo-orm-mybatis</name>
18+
<description>Demo project for Spring Boot</description>
19+
20+
<parent>
21+
<groupId>com.xkcoding</groupId>
22+
<artifactId>spring-boot-demo</artifactId>
23+
<version>1.0.0-SNAPSHOT</version>
24+
</parent>
25+
26+
<properties>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
29+
<java.version>1.8</java.version>
30+
<mybatis.version>1.3.2</mybatis.version>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.mybatis.spring.boot</groupId>
36+
<artifactId>mybatis-spring-boot-starter</artifactId>
37+
<version>${mybatis.version}</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>mysql</groupId>
42+
<artifactId>mysql-connector-java</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.projectlombok</groupId>
47+
<artifactId>lombok</artifactId>
48+
<optional>true</optional>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>cn.hutool</groupId>
53+
<artifactId>hutool-all</artifactId>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>com.google.guava</groupId>
58+
<artifactId>guava</artifactId>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-test</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
</dependencies>
67+
68+
<build>
69+
<finalName>spring-boot-demo-orm-mybatis</finalName>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-maven-plugin</artifactId>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
</project>
79+
```
80+
81+
## SpringBootDemoOrmMybatisApplication.java
82+
83+
```java
84+
/**
85+
* <p>
86+
* 启动类
87+
* </p>
88+
*
89+
* @package: com.xkcoding.orm.mybatis
90+
* @description: 启动类
91+
* @author: yangkai.shen
92+
* @date: Created in 2018/11/8 10:52
93+
* @copyright: Copyright (c) 2018
94+
* @version: V1.0
95+
* @modified: yangkai.shen
96+
*/
97+
@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.mapper"})
98+
@SpringBootApplication
99+
public class SpringBootDemoOrmMybatisApplication {
100+
101+
public static void main(String[] args) {
102+
SpringApplication.run(SpringBootDemoOrmMybatisApplication.class, args);
103+
}
104+
}
105+
```
106+
107+
## UserMapper.java
108+
109+
```java
110+
/**
111+
* <p>
112+
* User Mapper
113+
* </p>
114+
*
115+
* @package: com.xkcoding.orm.mybatis.mapper
116+
* @description: User Mapper
117+
* @author: yangkai.shen
118+
* @date: Created in 2018/11/8 10:54
119+
* @copyright: Copyright (c) 2018
120+
* @version: V1.0
121+
* @modified: yangkai.shen
122+
*/
123+
@Mapper
124+
@Component
125+
public interface UserMapper {
126+
127+
/**
128+
* 查询所有用户
129+
*
130+
* @return 用户列表
131+
*/
132+
@Select("SELECT * FROM orm_user")
133+
List<User> selectAllUser();
134+
135+
/**
136+
* 根据id查询用户
137+
*
138+
* @param id 主键id
139+
* @return 当前id的用户,不存在则是 {@code null}
140+
*/
141+
@Select("SELECT * FROM orm_user WHERE id = #{id}")
142+
User selectUserById(@Param("id") Long id);
143+
144+
/**
145+
* 保存用户
146+
*
147+
* @param user 用户
148+
* @return 成功 - {@code 1} 失败 - {@code 0}
149+
*/
150+
int saveUser(@Param("user") User user);
151+
152+
/**
153+
* 删除用户
154+
*
155+
* @param id 主键id
156+
* @return 成功 - {@code 1} 失败 - {@code 0}
157+
*/
158+
int deleteById(@Param("id") Long id);
159+
160+
}
161+
```
162+
163+
## UserMapper.xml
164+
165+
```xml
166+
<?xml version="1.0" encoding="UTF-8"?>
167+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
168+
<mapper namespace="com.xkcoding.orm.mybatis.mapper.UserMapper">
169+
170+
<insert id="saveUser">
171+
INSERT INTO `orm_user` (`name`,
172+
`password`,
173+
`salt`,
174+
`email`,
175+
`phone_number`,
176+
`status`,
177+
`create_time`,
178+
`last_login_time`,
179+
`last_update_time`)
180+
VALUES (#{user.name},
181+
#{user.password},
182+
#{user.salt},
183+
#{user.email},
184+
#{user.phoneNumber},
185+
#{user.status},
186+
#{user.createTime},
187+
#{user.lastLoginTime},
188+
#{user.lastUpdateTime})
189+
</insert>
190+
191+
<delete id="deleteById">
192+
DELETE
193+
FROM `orm_user`
194+
WHERE `id` = #{id}
195+
</delete>
196+
</mapper>
197+
```
198+
199+
## UserMapperTest.java
200+
201+
```java
202+
/**
203+
* <p>
204+
* UserMapper 测试类
205+
* </p>
206+
*
207+
* @package: com.xkcoding.orm.mybatis.mapper
208+
* @description: UserMapper 测试类
209+
* @author: yangkai.shen
210+
* @date: Created in 2018/11/8 11:25
211+
* @copyright: Copyright (c) 2018
212+
* @version: V1.0
213+
* @modified: yangkai.shen
214+
*/
215+
@Slf4j
216+
public class UserMapperTest extends SpringBootDemoOrmMybatisApplicationTests {
217+
@Autowired
218+
private UserMapper userMapper;
219+
220+
@Test
221+
public void selectAllUser() {
222+
List<User> userList = userMapper.selectAllUser();
223+
Assert.assertTrue(CollUtil.isNotEmpty(userList));
224+
log.debug("【userList】= {}", userList);
225+
}
226+
227+
@Test
228+
public void selectUserById() {
229+
User user = userMapper.selectUserById(1L);
230+
Assert.assertNotNull(user);
231+
log.debug("【user】= {}", user);
232+
}
233+
234+
@Test
235+
public void saveUser() {
236+
String salt = IdUtil.fastSimpleUUID();
237+
User user = User.builder().name("testSave3").password(SecureUtil.md5("123456" + salt)).salt(salt).email("[email protected]").phoneNumber("17300000003").status(1).lastLoginTime(new DateTime()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build();
238+
int i = userMapper.saveUser(user);
239+
Assert.assertEquals(1, i);
240+
}
241+
242+
@Test
243+
public void deleteById() {
244+
int i = userMapper.deleteById(1L);
245+
Assert.assertEquals(1, i);
246+
}
247+
}
248+
```
249+
250+
## 参考
251+
252+
- Mybatis官方文档:http://www.mybatis.org/mybatis-3/zh/index.html
253+
254+
- Mybatis官方脚手架文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
255+
256+
- Mybatis整合Spring Boot官方demo:https://github.com/mybatis/spring-boot-starter/tree/master/mybatis-spring-boot-samples

spring-boot-demo-orm-mybatis/pom.xml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>spring-boot-demo-orm-mybatis</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-boot-demo-orm-mybatis</name>
11+
<description>Demo project for Spring Boot</description>
12+
13+
<parent>
14+
<groupId>com.xkcoding</groupId>
15+
<artifactId>spring-boot-demo</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<java.version>1.8</java.version>
23+
<mybatis.version>1.3.2</mybatis.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.mybatis.spring.boot</groupId>
29+
<artifactId>mybatis-spring-boot-starter</artifactId>
30+
<version>${mybatis.version}</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>mysql</groupId>
35+
<artifactId>mysql-connector-java</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<optional>true</optional>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>cn.hutool</groupId>
46+
<artifactId>hutool-all</artifactId>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>com.google.guava</groupId>
51+
<artifactId>guava</artifactId>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-test</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<finalName>spring-boot-demo-orm-mybatis</finalName>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-maven-plugin</artifactId>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.xkcoding.orm.mybatis;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
/**
8+
* <p>
9+
* 启动类
10+
* </p>
11+
*
12+
* @package: com.xkcoding.orm.mybatis
13+
* @description: 启动类
14+
* @author: yangkai.shen
15+
* @date: Created in 2018/11/8 10:52
16+
* @copyright: Copyright (c) 2018
17+
* @version: V1.0
18+
* @modified: yangkai.shen
19+
*/
20+
@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.mapper"})
21+
@SpringBootApplication
22+
public class SpringBootDemoOrmMybatisApplication {
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(SpringBootDemoOrmMybatisApplication.class, args);
26+
}
27+
}

0 commit comments

Comments
 (0)