欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot集成MyBatis訪問數(shù)據(jù)庫的方法

 更新時間:2017年04月17日 10:06:05   投稿:mrr  
這篇文章主要介紹了Spring Boot集成MyBatis訪問數(shù)據(jù)庫的方法,需要的朋友可以參考下

基于spring boot開發(fā)的微服務(wù)應(yīng)用,與MyBatis如何集成?

集成方法

可行的方法有:

1.基于XML或者Java Config,構(gòu)建必需的對象,配置MyBatis。

2.使用MyBatis官方提供的組件,實現(xiàn)MyBatis的集成。

方法一

建議參考如下文章,完成集成的驗證。

MyBatis學(xué)習(xí) 之 一、MyBatis簡介與配置MyBatis+Spring+MySql

基于Spring + Spring MVC + Mybatis 高性能web構(gòu)建

spring與mybatis三種整合方法

MyBatis學(xué)習(xí)總結(jié)(八)——Mybatis3.x與Spring4.x整合

由于不是本文的重點,因此不附上樣例。

方法二

有如下步驟:

  • 修改pom.xml,增加軟件依賴
<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>1.2.0</version>
</dependency>
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.21</version>
</dependency>
  • 修改application.yml,增加數(shù)據(jù)源的定義

spring:

    datasource:
        url: jdbc:mysql://localhost:3306/test
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver

  • 修改application.yml,增加MyBatis的配置

mybatis:

    type-aliases-package: com.example.domain.model
    type-handlers-package: com.example.typehandler
    configuration:
        map-underscore-to-camel-case: true
        default-fetch-size: 100
        default-statement-timeout: 30

日志的配置

通過觀察日志,可有效的分析MyBatis生成的SQL,檢查SQL配置的正確性。

修改application.yml,增加如下配置

logging:
    level:
        net:
            jackieathome:
                db:
                    mapper: DEBUG

其中net.jackieathome.db.mapper下定義了訪問數(shù)據(jù)庫的mapper接口。

輸出的日志樣例如下

2017-04-16 11:32:23.266 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.createUser      : ==>  Preparing: insert into `user`(id, name, password) values(?, ?, ?)
2017-04-16 11:32:23.293 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.createUser      : ==> Parameters: id1492313542(String), null, null
2017-04-16 11:32:23.366 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.createUser      : <==    Updates: 1
2017-04-16 11:32:23.372 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.findUserById    : ==>  Preparing: select * from `user` where id = ?
2017-04-16 11:32:23.373 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.findUserById    : ==> Parameters: id1492313542(String)
2017-04-16 11:32:23.417 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.findUserById    : <==      Total: 1

事務(wù)的使用

依據(jù)MyBatis的官方文檔,允許用戶將事務(wù)交給Spring來管理,使用編程和注解來控制事務(wù)。這里以注解方式來舉例說明使用方法,樣例代碼如下:

1.mapper的定義,如下

package net.jackieathome.db.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import net.jackieathome.bean.User;
@Mapper
public interface UserMapper {
 // 創(chuàng)建用戶
 void createUser(User user);
 // 查找用戶
 User findUserById(@Param("id") String id);
}

2.數(shù)據(jù)庫訪問的中間層代碼,對上述mapper進行了封裝。

使用@Transactional標(biāo)記該類,表明該類的公有方法全部都啟用了事務(wù)的支持。關(guān)于@Transactional的使用,可以參考相關(guān)的官方文檔。

package net.jackieathome.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import net.jackieathome.bean.User;
import net.jackieathome.db.mapper.UserMapper;
@Component
@Transactional
public class UserDao {
 @Autowired
 private UserMapper userMapper;
 /**
 * 重復(fù)插入相同的用戶數(shù)據(jù),確認事務(wù)是否生效
 */
 public List<String> createBatch() {
 long time = System.currentTimeMillis() / 1000;
 User user = null;
 List<String> ids = new ArrayList<>();
 String id = "id" + time;
 String name = "name" + time;
 String password = "password" + time;
 user = new User();
 user.setId(id);
 user.setName(name);
 user.setPassword(password);
 userMapper.createUser(user);
 ids.add(id);
 user = new User();
 user.setId(id);
 user.setName(name);
 user.setPassword(password);
 userMapper.createUser(user);
 ids.add(id);
 return ids;
 }
}

3.業(yè)務(wù)層實現(xiàn)

package net.jackieathome.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import net.jackieathome.bean.User;
import net.jackieathome.dao.UserDao;
import net.jackieathome.db.mapper.UserMapper;
@RestController
public class UserController {
 @Autowired
 private UserMapper userMapper; 
 @Autowired
 private UserDao userDao;
 @RequestMapping(method = RequestMethod.GET, value = "/user/create/batch")
 public List<User> createBatch() {
 try
 {
 userDao.createBatch();
 }
 catch (Exception e)
 {
 }
 return userMapper.loadAllUsers();
 }
}

從實際測試看,上述事務(wù)的實現(xiàn)有效,可保證當(dāng)數(shù)據(jù)出現(xiàn)主鍵沖突時,事務(wù)中的插入操作可全部撤銷,不會出現(xiàn)部分?jǐn)?shù)據(jù)插入成功、部分失敗的現(xiàn)象。

注意事項:

由于注解事務(wù)的實現(xiàn)依賴Spring AOP,因此只有當(dāng)注入行為存在時,注解事務(wù)的控制才會生效。

1.假如在上述UserController類中定義createBatch方法,并且使用注解@Transactional標(biāo)記,經(jīng)驗證可確認此時注解事務(wù)是無效的。  

2.假如在上述UserDao中定義了多個公有方法,存在相互調(diào)用的行為,基于相同的原因,這些方法相互調(diào)用時注解事務(wù)并不會生效。如果確實需要保證事務(wù)可用,可以考慮調(diào)整類的設(shè)計或者使用編程的方式來控制事務(wù)。

以上所述是小編給大家介紹的Spring Boot集成MyBatis訪問數(shù)據(jù)庫的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論