Skip to content

Commit 1cf9b2a

Browse files
committed
Spring Boot 2.x基础教程:使用MyBatis访问MySQL
1 parent 77a4c13 commit 1cf9b2a

File tree

10 files changed

+188
-5
lines changed

10 files changed

+188
-5
lines changed

2.1.x/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
- [Spring Boot 2.x基础教程:默认数据源Hikari的配置详解](http://blog.didispace.com/spring-boot-learning-21-3-2/)
7575
- [Spring Boot 2.x基础教程:使用国产数据库连接池Druid](http://blog.didispace.com/spring-boot-learning-21-3-3/)
7676
- [Spring Boot 2.x基础教程:使用Spring Data JPA访问MySQL](http://blog.didispace.com/spring-boot-learning-21-3-4/)
77+
- [Spring Boot 2.x基础教程:使用MyBatis访问MySQL](http://blog.didispace.com/spring-boot-learning-21-3-5/)
7778

78-
- [Spring Boot 2.x基础教程:使用MyBatis]
7979
- [Spring Boot 2.x基础教程:多数据源配置]
8080
- [Spring Boot 2.x基础教程:数据库管理Flyway]
8181

2.1.x/README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
- [Spring Boot 2.x基础教程:默认数据源Hikari的配置详解](http://blog.didispace.com/spring-boot-learning-21-3-2/)
6868
- [Spring Boot 2.x基础教程:使用国产数据库连接池Druid](http://blog.didispace.com/spring-boot-learning-21-3-3/)
6969
- [Spring Boot 2.x基础教程:使用Spring Data JPA访问MySQL](http://blog.didispace.com/spring-boot-learning-21-3-4/)
70+
- [Spring Boot 2.x基础教程:使用MyBatis访问MySQL](http://blog.didispace.com/spring-boot-learning-21-3-5/)
7071

71-
- [Spring Boot 2.x基础教程:使用MyBatis]
7272
- [Spring Boot 2.x基础教程:多数据源配置]
7373
- [Spring Boot 2.x基础教程:数据库管理Flyway]
7474

2.1.x/chapter3-5/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

2.1.x/chapter3-5/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter3-5</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.mybatis.spring.boot</groupId>
29+
<artifactId>mybatis-spring-boot-starter</artifactId>
30+
<version>2.1.1</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+
<scope>provided</scope>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.chapter35;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@SpringBootApplication
9+
public class Chapter35Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Chapter35Application.class, args);
13+
}
14+
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.didispace.chapter35;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
7+
@Data
8+
@NoArgsConstructor
9+
public class User {
10+
11+
private Long id;
12+
13+
private String name;
14+
private Integer age;
15+
16+
public User(String name, Integer age) {
17+
this.name = name;
18+
this.age = age;
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.didispace.chapter35;
2+
3+
import org.apache.ibatis.annotations.Insert;
4+
import org.apache.ibatis.annotations.Mapper;
5+
import org.apache.ibatis.annotations.Param;
6+
import org.apache.ibatis.annotations.Select;
7+
8+
/**
9+
* Created by 程序猿DD/翟永超 on 2020/2/15.
10+
* <p>
11+
* Blog: http://blog.didispace.com/
12+
* Github: https://github.com/dyc87112/
13+
*/
14+
@Mapper
15+
public interface UserMapper {
16+
17+
@Select("SELECT * FROM USER WHERE NAME = #{name}")
18+
User findByName(@Param("name") String name);
19+
20+
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
21+
int insert(@Param("name") String name, @Param("age") Integer age);
22+
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/test
2+
spring.datasource.username=root
3+
spring.datasource.password=12345678
4+
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.didispace.chapter35;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.annotation.Rollback;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import javax.sql.DataSource;
14+
import java.util.List;
15+
16+
@Slf4j
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest
19+
public class Chapter35ApplicationTests {
20+
21+
@Autowired
22+
private UserMapper userMapper;
23+
24+
@Test
25+
@Rollback
26+
public void test() throws Exception {
27+
userMapper.insert("AAA", 20);
28+
User u = userMapper.findByName("AAA");
29+
Assert.assertEquals(20, u.getAge().intValue());
30+
}
31+
32+
}

2.1.x/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
<module>chapter3-2</module>
2626
<module>chapter3-3</module>
2727
<module>chapter3-4</module>
28+
<module>chapter3-5</module> <!-- 使用 MyBatis -->
2829

2930
<!-- Web开发 -->
30-
<module>chapter4-1</module> <!-- 使用 Thymeleaf开发Web页面 -->
31-
<module>chapter4-2</module> <!-- 使用 ECharts 绘制折线图 -->
31+
<module>chapter4-1</module> <!-- 使用 Thymeleaf开发Web页面 -->
32+
<module>chapter4-2</module> <!-- 使用 ECharts 绘制折线图 -->
3233

3334
</modules>
3435

35-
3636
</project>

0 commit comments

Comments
 (0)