MyBatis使用详解
This commit is contained in:
62
Chapter3-2-8/pom.xml
Normal file
62
Chapter3-2-8/pom.xml
Normal file
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>Chapter3-2-8</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter3-2-8</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.21</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
13
Chapter3-2-8/src/main/java/com/didispace/Application.java
Normal file
13
Chapter3-2-8/src/main/java/com/didispace/Application.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
47
Chapter3-2-8/src/main/java/com/didispace/domain/User.java
Normal file
47
Chapter3-2-8/src/main/java/com/didispace/domain/User.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.didispace.domain;
|
||||
|
||||
public class User {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(Long id, String name, Integer age) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public User(String name, Integer age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.didispace.domain;
|
||||
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
@Select("SELECT * FROM user WHERE name = #{name}")
|
||||
User findByName(@Param("name") String name);
|
||||
|
||||
@Results({
|
||||
@Result(property = "name", column = "name"),
|
||||
@Result(property = "age", column = "age")
|
||||
})
|
||||
@Select("SELECT name, age FROM user")
|
||||
List<User> findAll();
|
||||
|
||||
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
|
||||
int insert(@Param("name") String name, @Param("age") Integer age);
|
||||
|
||||
@Update("UPDATE user SET age=#{age} WHERE name=#{name}")
|
||||
void update(User user);
|
||||
|
||||
@Delete("DELETE FROM user WHERE id =#{id}")
|
||||
void delete(Long id);
|
||||
|
||||
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
|
||||
int insertByUser(User user);
|
||||
|
||||
@Insert("INSERT INTO user(name, age) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")
|
||||
int insertByMap(Map<String, Object> map);
|
||||
|
||||
}
|
||||
4
Chapter3-2-8/src/main/resources/application.properties
Normal file
4
Chapter3-2-8/src/main/resources/application.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/test
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.domain.User;
|
||||
import com.didispace.domain.UserMapper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@Transactional
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
@Rollback
|
||||
public void testUserMapper() throws Exception {
|
||||
// insert一条数据,并select出来验证
|
||||
userMapper.insert("AAA", 20);
|
||||
User u = userMapper.findByName("AAA");
|
||||
Assert.assertEquals(20, u.getAge().intValue());
|
||||
// update一条数据,并select出来验证
|
||||
u.setAge(30);
|
||||
userMapper.update(u);
|
||||
u = userMapper.findByName("AAA");
|
||||
Assert.assertEquals(30, u.getAge().intValue());
|
||||
// 删除这条数据,并select验证
|
||||
userMapper.delete(u.getId());
|
||||
u = userMapper.findByName("AAA");
|
||||
Assert.assertEquals(null, u);
|
||||
|
||||
u = new User("BBB", 30);
|
||||
userMapper.insertByUser(u);
|
||||
Assert.assertEquals(30, userMapper.findByName("BBB").getAge().intValue());
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", "CCC");
|
||||
map.put("age", 40);
|
||||
userMapper.insertByMap(map);
|
||||
Assert.assertEquals(40, userMapper.findByName("CCC").getAge().intValue());
|
||||
|
||||
List<User> userList = userMapper.findAll();
|
||||
for(User user : userList) {
|
||||
Assert.assertEquals(null, user.getId());
|
||||
Assert.assertNotEquals(null, user.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user