Skip to content

Commit 1ac7489

Browse files
committed
Spring Boot 2.x基础教程:使用MyBatis的XML配置方式
1 parent b5b48e2 commit 1ac7489

File tree

8 files changed

+188
-0
lines changed

8 files changed

+188
-0
lines changed

2.1.x/chapter3-6/.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-6/pom.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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-6</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.mybatis.spring.boot</groupId>
24+
<artifactId>mybatis-spring-boot-starter</artifactId>
25+
<version>2.1.1</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>mysql</groupId>
30+
<artifactId>mysql-connector-java</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.projectlombok</groupId>
35+
<artifactId>lombok</artifactId>
36+
<scope>provided</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-maven-plugin</artifactId>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.chapter36;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@MapperScan("com.didispace.chapter36.mapper")
8+
@SpringBootApplication
9+
public class Chapter36Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Chapter36Application.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.chapter36.entity;
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.didispace.chapter36.mapper;
2+
3+
import com.didispace.chapter36.entity.User;
4+
import org.apache.ibatis.annotations.Param;
5+
6+
/**
7+
* Created by 程序猿DD/翟永超 on 2020/2/28.
8+
* <p>
9+
* Blog: http://blog.didispace.com/
10+
* Github: https://github.com/dyc87112/
11+
*/
12+
public interface UserMapper {
13+
14+
User findByName(@Param("name") String name);
15+
16+
int insert(@Param("name") String name, @Param("age") Integer age);
17+
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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
5+
6+
mybatis.mapper-locations=classpath:mapper/*.xml
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE mapper
3+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5+
<mapper namespace="com.didispace.chapter36.mapper.UserMapper">
6+
<select id="findByName" resultType="com.didispace.chapter36.entity.User">
7+
SELECT * FROM USER WHERE NAME = #{name}
8+
</select>
9+
10+
<insert id="insert">
11+
INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})
12+
</insert>
13+
</mapper>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.didispace.chapter36;
2+
3+
import com.didispace.chapter36.entity.User;
4+
import com.didispace.chapter36.mapper.UserMapper;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.annotation.Rollback;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.springframework.transaction.annotation.Transactional;
14+
15+
@Slf4j
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest
18+
@Transactional
19+
public class Chapter36ApplicationTests {
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+
}

0 commit comments

Comments
 (0)