源碼
請前往文章末端查看
開發環境:
- 開發工具:Intellij IDEA 2017.1.3
- JDK : 1.8.0_101
- spring boot 版本 : 1.5.8.RELEASE
- maven : 3.3.9
拓展:
- springboot 整合 Mybatis 事務管理
開始
1.新建一個springboot項目:
添加依賴
2.看一下項目結構
3.完整依賴
<?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.winterchen</groupId>
<artifactId>springboot-mybatis-demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-mybatis-demo2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
4.配置文件
因爲習慣性的喜歡使用yml作爲配置文件,所以將application.properties替換爲application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mytest
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
- 1
- 2
- 3
- 4
- 5
- 6
簡單且簡潔的完成了基本配置,下面看看我們是如何在這個基礎下輕鬆使用Mybatis訪問數據庫的
使用Mybatis
- 在Mysql數據庫中創建數據表:
CREATE DATABASE mytest;
USE mytest;
CREATE TABLE t_user(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL ,
password VARCHAR(255) NOT NULL ,
phone VARCHAR(255) NOT NULL
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 創建映射對象User
package com.winterchen.domain;
/**
* User實體映射類
* Created by Administrator on 2017/11/24.
*/
public class User {
private Integer id;
private String name;
private String password;
private String phone;
//省略 get 和 set ...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 創建User映射的操作UserMapper,爲了後續單元測試驗證,實現插入和查詢操作
package com.winterchen.mapper;
import com.winterchen.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* User映射類
* Created by Administrator on 2017/11/24.
*/
@Mapper
public interface UserMapper {
@Select("SELECT * FROM T_USER WHERE PHONE = #{phone}")
User findUserByPhone(@Param("phone") String phone);
@Insert("INSERT INTO T_USER(NAME, PASSWORD, PHONE) VALUES(#{name}, #{password}, #{phone})")
int insert(@Param("name") String name, @Param("password") String password, @Param("phone") String phone);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
如果想了解更多Mybatis註解的詳細:springboot中使用Mybatis註解配置詳解
- 創建springboot 主類:
package com.winterchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootMybatisDemo2Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDemo2Application.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 創建測試單元:
- 測試邏輯:插入一條name爲”weinterchen”的User,然後根據user的phone進行查詢,並判斷user的name是否爲”winterchen”。
package com.winterchen;
import com.winterchen.domain.User;
import com.winterchen.mapper.UserMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisDemo2ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test(){
userMapper.insert("winterchen", "123456", "12345678910");
User u = userMapper.findUserByPhone("12345678910");
Assert.assertEquals("winterchen", u.getName());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 測試結果
說明已經成功了
事務管理(重要)
我們在開發企業應用時,對於業務人員的一個操作實際是對數據讀寫的多步操作的結合。由於數據操作在順序執行的過程中,任何一步操作都有可能發生異常,異常會導致後續操作無法完成,此時由於業務邏輯並未正確的完成,之前成功操作數據的並不可靠,需要在這種情況下進行回退。
爲了測試的成功,請把測試的內容進行替換,因爲之前測試的時候已經將數據生成了,重複的數據會對測試的結果有影響
@Test
@Transactional
public void test(){
userMapper.insert("張三", "123456", "18600000000");
int a = 1/0;
userMapper.insert("李四", "123456", "13500000000");
User u = userMapper.findUserByPhone("12345678910");
Assert.assertEquals("winterchen", u.getName());
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
只需要在需要事務管理的方法上添加 @Transactional
註解即可,然後我們啓動測試,會發現異常之後,數據庫中沒有產生數據。