springboot---mybits整合

配置

POM文件

<parent>
<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath />
</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>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>RELEASE</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>

同一環境1.5.7.RELEASE版本的Spring-boot會拋Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver異常,1.5.6.RELEASE以及1.5.5.RELEASE版本親測沒問題

application.properties配置文件

#數據庫
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=741852
spring.datasource.driver-class-name=com.mysql.jdbc.Driver#mybatis&&通用Mapper
mybatis.type-aliases-package=com.karle.bean
mybatis.mapper-locations=classpath:mapper/*.xml
mapper.mappers=com.karle.tk.TkMapper
mapper.identity=MYSQL#分頁插件
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql#log
logging.file=logger.log
logging.level.*=debug

映射實體(省略字段get、set)

import java.util.Date;import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Table(name = "user")
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;/*** 名稱*/@Column(name = "name")private String name;/*** 年齡*/@Column(name = "age")private Integer age;/*** 身份編號*/@Column(name = "card_no")private Integer cardNo;/*** 生日*/@Column(name = "birthday")private Date birthday;
}

本地通用Mapper接口(繼承通用Mapper接口)

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;public interface TkMapper<T> extends Mapper<T>, MySqlMapper<T> {}

Mapper接口:基本的增、刪、改、查方法
MySqlMapper:針對MySQL的額外補充接口,支持批量插入

業務接口(繼承“本地通用Mapper接口”)

import org.apache.ibatis.annotations.Param;import com.karle.bean.User;
import com.karle.tk.TkMapper;public interface UserMapper extends TkMapper<User> {public User selectByCardNo(@Param("cardNo") int cardNo);}

Spring-boot啟動類,@MapperScan僅掃描業務接口包,不能掃描本地通用Mapper接口包,否則報java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class異常

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan(basePackages = { "com.karle.mapper" })
public class SpringBootMyBatisApplication {public static void main(String[] args) {SpringApplication.run(SpringBootMyBatisApplication.class, args);}}

通過使用@MapperScan可以指定要掃描的Mapper類的包的路徑,比如:

@SpringBootApplication  
@MapperScan("com.kfit.*.mapper")  
public class App {  public static void main(String[] args) {  SpringApplication.run(App.class, args);  }  
}  

或者:

@SpringBootApplication  
@MapperScan("com.kfit.mapper")  
public class App {  public static void main(String[] args) {  SpringApplication.run(App.class, args);  }  
}  

可以根據包的結構指定不同的表達式。

使用@MapperScan注解多個包

可以使用如下的方式指定多個包:

@SpringBootApplication  
@MapperScan({"com.kfit.demo","com.kfit.user"})  
public class App {  public static void main(String[] args) {  SpringApplication.run(App.class, args);  }  
}  

如果mapper類沒有在Spring Boot主程序可以掃描的包或者子包下面,可以使用如下方式進行配置:

@SpringBootApplication  
@MapperScan({"com.kfit.*.mapper","org.kfit.*.mapper"})  
public class App {  public static void main(String[] args) {  SpringApplication.run(App.class, args);  }  
}  

單元測試

import java.util.ArrayList;
import java.util.Date;
import java.util.List;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;import com.github.pagehelper.PageHelper;
import com.karle.bean.User;
import com.karle.mapper.UserMapper;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootMyBatisApplicationTests {@Autowiredprivate UserMapper mapper;// 插入一條新記錄@Testpublic void insertOne() {User newUser = new User();int cardNo = (int) (Math.random() * 10000000);newUser.setAge(24);newUser.setBirthday(new Date());newUser.setName(cardNo + "用戶");newUser.setCardNo(cardNo);mapper.insertSelective(newUser);System.out.println("插入成功");}// 批量插入記錄@Testpublic void insertMore() {List<User> recordList = new ArrayList<User>();for (int i = 0; i < 2; i++) {User newUser = new User();int cardNo = (int) (Math.random() * 10000000);newUser.setAge(26);newUser.setBirthday(new Date());newUser.setName(cardNo + "批量插入用戶");newUser.setCardNo(cardNo);recordList.add(newUser);}mapper.insertList(recordList);System.out.println("批量插入成功");}// 根據唯一編號查詢用戶(通用Mapper查詢)@Testpublic void selectByCardNo() {User paramBean = new User();paramBean.setCardNo(6647403);User dbUser = mapper.selectOne(paramBean);if (dbUser != null) {System.out.println("數據庫用戶(通用Mapper查詢):" + dbUser.getName());return;}System.out.println("查無此用戶");}// 根據唯一編號查詢用戶(XML查詢)@Testpublic void selectByCardNoByXml() {User dbUser = mapper.selectByCardNo(6105967);if (dbUser != null) {System.out.println("數據庫用戶(XML查詢):" + dbUser.getName());return;}System.out.println("查無此用戶");}// 根據年齡查詢一組用戶@Testpublic void selectByAge() {User paramBean = new User();paramBean.setAge(24);List<User> dbUserList = mapper.select(paramBean);System.out.println("總共查詢數:" + dbUserList.size());}// 分頁查詢用戶@Testpublic void selectByPage() {PageHelper.offsetPage(1, 5);List<User> dbUserList = mapper.select(null);for (User item : dbUserList) {System.out.println("分頁用戶:" + item.getName());}}// 更新用戶信息@Testpublic void updateOneInfo() {User paramBean = new User();paramBean.setId(1);paramBean.setAge(26);mapper.updateByPrimaryKeySelective(paramBean);System.out.println("更新成功");}}

事務的使用

spring Boot 使用事務非常簡單,首先使用注解 @EnableTransactionManagement開啟事務支持后,然后在訪問數據庫的Service方法上添加注解 @Transactional 便可。

@EnableTransactionManagement放在啟動類上

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/250124.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250124.shtml
英文地址,請注明出處:http://en.pswp.cn/news/250124.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用airdrop進行文件共享

使用airdrop進行文件共享 學習了&#xff1a; https://support.apple.com/zh-cn/HT203106 https://zh.wikihow.com/%E5%9C%A8Mac%E4%B8%8A%E7%94%A8%E8%BF%91%E6%9C%BA%E6%8D%B7%E4%BC%A0%EF%BC%88Airdrop%EF%BC%89%E5%85%B1%E4%BA%AB%E6%96%87%E4%BB%B6 轉載于:https://www.cn…

【鏈表】逆序打印鏈表

1 public class Main {2 3 // 逆序打印鏈表4 public void reversePrint(Node node) {5 if (node null){6 return;7 }8 reversePrint(node.next);9 System.out.println(node.data); 10 } 11 12 public Node crea…

[pytorch、學習] - 5.8 網絡中的網絡(NiN)

參考 5.8 網絡中的網絡&#xff08;NiN&#xff09; 前幾節介紹的LeNet、AlexNet和VGG在設計上的共同之處是&#xff1a;先以由卷積層構成的模塊充分抽取空間特征&#xff0c;再以由全連接層構成的模塊來輸出分類結果。其中&#xff0c;AlexNet和VGG對LeNet的改進主要在于如何…

springboot---集成mybits方法

SpringBoot集成mybatis配置 一個有趣的現象&#xff1a;傳統企業大都喜歡使用hibernate,互聯網行業通常使用mybatis&#xff1b;之所以出現這個問題感覺與對應的業務有關&#xff0c;比方說&#xff0c;互聯網的業務更加的復雜&#xff0c;更加需要進行靈活性的處理&#xff0c…

jQuery源碼解讀

參考 &#xff1a; https://www.cnblogs.com/yuqingfamily/p/5785593.html 轉載于:https://www.cnblogs.com/wfblog/p/9172622.html

info.plist文件里面添加描述 - 配置定位,相冊等

<key>NSAppleMusicUsageDescription</key> <string>App需要您的同意,才能訪問媒體資料庫</string> <key>NSBluetoothPeripheralUsageDescription</key> <string>App需要您的同意,才能訪問藍牙</string> <key>NSCalendar…

[pytorch、學習] - 5.9 含并行連結的網絡(GoogLeNet)

參考 5.9 含并行連結的網絡&#xff08;GoogLeNet&#xff09; 在2014年的ImageNet圖像識別挑戰賽中&#xff0c;一個名叫GoogLeNet的網絡結構大放異彩。它雖然在名字上向LeNet致敬&#xff0c;但在網絡結構上已經很難看到LeNet的影子。GoogLeNet吸收了NiN中網絡串聯網絡的思…

mybits注解詳解

一、mybatis 簡單注解 關鍵注解詞 &#xff1a; Insert &#xff1a; 插入sql , 和xml insert sql語法完全一樣 Select &#xff1a; 查詢sql, 和xml select sql語法完全一樣 Update &#xff1a; 更新sql, 和xml update sql語法完全一樣 Delete &#xff1a; 刪除sql, 和xml d…

使用python裝飾器計算函數運行時間的實例

使用python裝飾器計算函數運行時間的實例 裝飾器在python里面有很重要的作用&#xff0c; 如果能夠熟練使用&#xff0c;將會大大的提高工作效率 今天就來見識一下 python 裝飾器&#xff0c;到底是怎么工作的。 本文主要是利用python裝飾器計算函數運行時間 一些需要精確的計算…

SQLServer用存儲過程實現插入更新數據

實現 1&#xff09;有同樣的數據&#xff0c;直接返回&#xff08;返回值&#xff1a;0&#xff09;。 2&#xff09;有主鍵同樣。可是數據不同的數據。進行更新處理&#xff08;返回值&#xff1a;2&#xff09;&#xff1b; 3&#xff09;沒有數據&#xff0c;進行插入數據處…

[pytorch、學習] - 9.1 圖像增廣

參考 9.1 圖像增廣 在5.6節(深度卷積神經網絡)里我們提過,大規模數據集是成功應用神經網絡的前提。圖像增廣(image augmentation)技術通過對訓練圖像做一系列隨機改變,來產生相似但又不相同的訓練樣本,從而擴大訓練數據集的規模。圖像增廣的另一種解釋是,隨機改變訓練樣本可以…

mysql綠色版安裝

導讀&#xff1a;MySQL是一款關系型數據庫產品&#xff0c;官網給出了兩種安裝包格式&#xff1a;MSI和ZIP。MSI格式是圖形界面安裝方式&#xff0c;基本只需下一步即可&#xff0c;這篇文章主要介紹ZIP格式的安裝過程。ZIP Archive版是免安裝的。只要解壓就行了。 一、首先下…

在微信瀏覽器字體被調大導致頁面錯亂的解決辦法

iOS的解決方案是覆蓋掉微信的樣式&#xff1a; body { /* IOS禁止微信調整字體大小 */-webkit-text-size-adjust: 100% !important; } 安卓的解決方案是通過 WeixinJSBridge 對象將網頁的字體大小設置為默認大小&#xff0c;并且重寫設置字體大小的方法&#xff0c;讓用戶不能在…

[pytorch、學習] - 9.2 微調

參考 9.2 微調 在前面得一些章節中,我們介紹了如何在只有6萬張圖像的Fashion-MNIST訓練數據集上訓練模型。我們還描述了學術界當下使用最廣泛規模圖像數據集ImageNet,它有超過1000萬的圖像和1000類的物體。然而,我們平常接觸到數據集的規模通常在這兩者之間。 假設我們想從圖…

Springboot默認加載application.yml原理

Springboot默認加載application.yml原理以及擴展 SpringApplication.run(…)默認會加載classpath下的application.yml或application.properties配置文件。公司要求搭建的框架默認加載一套默認的配置文件demo.properties&#xff0c;讓開發人員實現“零”配置開發&#xff0c;但…

java 集合(Set接口)

Set接口&#xff1a;無序集合&#xff0c;不允許有重復值&#xff0c;允許有null值 存入與取出的順序有可能不一致 HashSet:具有set集合的基本特性&#xff0c;不允許重復值&#xff0c;允許null值 底層實現是哈希表結構 初始容量為16 保存自定義對象時&#xff0c;保證數據的唯…

關于mac機抓包的幾點基礎知識

1. 我使用的抓包工具為WireShark&#xff0c;以下操作按我當前的版本(Version 2.6.1)做的&#xff0c;以前的版本或者以后的版本可能有稍微的區別。 2. 將mac設置為熱點&#xff1a;打開系統偏好設置&#xff0c;點擊共享&#xff1a; 然后點擊WIFI選項&#xff0c;設置WIFI名…

SpringBoot啟動如何加載application.yml配置文件

一、前言 在spring時代配置文件的加載都是通過web.xml配置加載的(Servlet3.0之前)&#xff0c;可能配置方式有所不同&#xff0c;但是大多數都是通過指定路徑的文件名的形式去告訴spring該加載哪個文件&#xff1b; <context-param><param-name>contextConfigLocat…

[github] - git使用小結(分支拉取、版本回退)

1. 首次(fork項目之后) $ git clone [master] $ git branch -a $ git checkout -b [自己的分支名] [遠程倉庫的分支名]克隆的是主干網絡 2. 再次拉取代碼 $ git pull [master下選擇分支名] [分支名] $ git push origin HEAD:[分支名]拉取首先得進入主倉(不是自己的遠程倉)然后…

MYSQL 查看最大連接數和修改最大連接數

MySQL查看最大連接數和修改最大連接數 1、查看最大連接數show variables like %max_connections%;2、修改最大連接數set GLOBAL max_connections 200; 以下的文章主要是向大家介紹的是MySQL最大連接數的修改&#xff0c;我們大家都知道MySQL最大連接數的默認值是100, 這個數值…