日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring boot Mybatis 整合(注解版)

發布時間:2023/12/1 javascript 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring boot Mybatis 整合(注解版) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前寫過一篇關于springboot 與 mybatis整合的博文,使用了一段時間spring-data-jpa,發現那種方式真的是太爽了,mybatis的xml的映射配置總覺得有點麻煩。接口定義和映射離散在不同的文件中,閱讀起來不是很方便。于是,準備使用mybatis的注解方式實現映射。如果喜歡xml方式的可以看我之前的博文:Spring boot Mybatis 整合(完整版)

個人開源項目

  • springboot+mybatis+thymeleaf+docker構建的個人站點開源項目(集成了個人主頁、個人作品、個人博客)

推薦開源項目

  • 開源的springboot接口文檔組件swagger2

更多干貨

SpringBoot系列目錄

源碼

請前往文章末端查看

開發環境:


  • 開發工具: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>

4.配置文件

因為習慣性的喜歡使用yml作為配置文件,所以將application.properties替換為application.yml

spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mytestusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver

簡單且簡潔的完成了基本配置,下面看看我們是如何在這個基礎下輕松使用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;
  • 創建映射對象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 ... }
  • 創建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);}

如果想了解更多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);} }
  • 創建測試單元:
    • 測試邏輯:插入一條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 {@Autowiredprivate UserMapper userMapper;@Testpublic void test(){userMapper.insert("winterchen", "123456", "12345678910");User u = userMapper.findUserByPhone("12345678910");Assert.assertEquals("winterchen", u.getName());}}
  • 測試結果

說明已經成功了。
**如果出現mapper注入不了的情況,請檢查版本,當前博客的搭建方法只適合1.5.*版本的,如果你的版本是2.0以上的版本,請參照我的另一篇博客的mybatis的配置:springboot2.0整合mybatis **

事務管理(重要)


我們在開發企業應用時,對于業務人員的一個操作實際是對數據讀寫的多步操作的結合。由于數據操作在順序執行的過程中,任何一步操作都有可能發生異常,異常會導致后續操作無法完成,此時由于業務邏輯并未正確的完成,之前成功操作數據的并不可靠,需要在這種情況下進行回退。

為了測試的成功,請把測試的內容進行替換,因為之前測試的時候已經將數據生成了,重復的數據會對測試的結果有影響

@Test@Transactionalpublic void test(){userMapper.insert("張三", "123456", "18600000000");int a = 1/0;userMapper.insert("李四", "123456", "13500000000");User u = userMapper.findUserByPhone("12345678910");Assert.assertEquals("winterchen", u.getName());}

只需要在需要事務管理的方法上添加 @Transactional 注解即可,然后我們啟動測試,會發現異常之后,數據庫中沒有產生數據。

如果大家想對springboot事務管理有更加詳細的了解,歡迎大家查看:springboot事務管理詳解

源碼:https://github.com/WinterChenS/springboot-mybatis-demo2/

springboot技術交流群:681513531

轉載于:https://www.cnblogs.com/winterchens/p/10655558.html

總結

以上是生活随笔為你收集整理的Spring boot Mybatis 整合(注解版)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。