Skip to content

Commit 1c355c3

Browse files
committed
MyBatis使用详解
1 parent af21e4f commit 1c355c3

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed

Chapter3-2-8/pom.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter3-2-8</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter3-2-8</name>
12+
<description>Spring Boot project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.mybatis.spring.boot</groupId>
41+
<artifactId>mybatis-spring-boot-starter</artifactId>
42+
<version>1.1.1</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>mysql</groupId>
47+
<artifactId>mysql-connector-java</artifactId>
48+
<version>5.1.21</version>
49+
</dependency>
50+
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.didispace.domain;
2+
3+
public class User {
4+
5+
private Long id;
6+
private String name;
7+
private Integer age;
8+
9+
public User() {
10+
}
11+
12+
public User(Long id, String name, Integer age) {
13+
this.id = id;
14+
this.name = name;
15+
this.age = age;
16+
}
17+
18+
public User(String name, Integer age) {
19+
this.name = name;
20+
this.age = age;
21+
}
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public Integer getAge() {
40+
return age;
41+
}
42+
43+
public void setAge(Integer age) {
44+
this.age = age;
45+
}
46+
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.didispace.domain;
2+
3+
import org.apache.ibatis.annotations.*;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
@Mapper
9+
public interface UserMapper {
10+
11+
@Select("SELECT * FROM user WHERE name = #{name}")
12+
User findByName(@Param("name") String name);
13+
14+
@Results({
15+
@Result(property = "name", column = "name"),
16+
@Result(property = "age", column = "age")
17+
})
18+
@Select("SELECT name, age FROM user")
19+
List<User> findAll();
20+
21+
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
22+
int insert(@Param("name") String name, @Param("age") Integer age);
23+
24+
@Update("UPDATE user SET age=#{age} WHERE name=#{name}")
25+
void update(User user);
26+
27+
@Delete("DELETE FROM user WHERE id =#{id}")
28+
void delete(Long id);
29+
30+
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
31+
int insertByUser(User user);
32+
33+
@Insert("INSERT INTO user(name, age) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")
34+
int insertByMap(Map<String, Object> map);
35+
36+
}
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=123456
4+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.didispace;
2+
3+
import com.didispace.domain.User;
4+
import com.didispace.domain.UserMapper;
5+
import org.junit.Assert;
6+
import org.junit.Before;
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.SpringApplicationConfiguration;
11+
import org.springframework.test.annotation.Rollback;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
import org.springframework.transaction.annotation.Transactional;
14+
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
20+
@RunWith(SpringJUnit4ClassRunner.class)
21+
@SpringApplicationConfiguration(classes = Application.class)
22+
@Transactional
23+
public class ApplicationTests {
24+
25+
@Autowired
26+
private UserMapper userMapper;
27+
28+
@Test
29+
@Rollback
30+
public void testUserMapper() throws Exception {
31+
// insert一条数据,并select出来验证
32+
userMapper.insert("AAA", 20);
33+
User u = userMapper.findByName("AAA");
34+
Assert.assertEquals(20, u.getAge().intValue());
35+
// update一条数据,并select出来验证
36+
u.setAge(30);
37+
userMapper.update(u);
38+
u = userMapper.findByName("AAA");
39+
Assert.assertEquals(30, u.getAge().intValue());
40+
// 删除这条数据,并select验证
41+
userMapper.delete(u.getId());
42+
u = userMapper.findByName("AAA");
43+
Assert.assertEquals(null, u);
44+
45+
u = new User("BBB", 30);
46+
userMapper.insertByUser(u);
47+
Assert.assertEquals(30, userMapper.findByName("BBB").getAge().intValue());
48+
49+
Map<String, Object> map = new HashMap<>();
50+
map.put("name", "CCC");
51+
map.put("age", 40);
52+
userMapper.insertByMap(map);
53+
Assert.assertEquals(40, userMapper.findByName("CCC").getAge().intValue());
54+
55+
List<User> userList = userMapper.findAll();
56+
for(User user : userList) {
57+
Assert.assertEquals(null, user.getId());
58+
Assert.assertNotEquals(null, user.getName());
59+
}
60+
61+
}
62+
63+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy