springboot 2.x整合mybatis實(shí)現(xiàn)增刪查和批量處理方式
springboot 2.x整合mybatis實(shí)現(xiàn)增刪查和批量處理
話不多說,直接上代碼:
1.添加依賴
<!--mybatis數(shù)據(jù)庫整合-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySQL的JDBC驅(qū)動包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--引入第三方數(shù)據(jù)源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
2.添加配置文件
#--------------------- # mybatis配置 #--------------------- #設(shè)置數(shù)據(jù)源(默認(rèn)數(shù)據(jù)源是com.zaxxer.hikari.HikariDataSource) spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #數(shù)據(jù)庫登錄賬號 spring.datasource.username=root #數(shù)據(jù)庫登錄密碼 spring.datasource.password=123456 #數(shù)據(jù)庫連接 spring.datasource.url=jdbc:mysql://localhost:3306/customer?useUnicode=true&characterEncoding=utf-8 #驅(qū)動(會自動檢測配置,可以注釋掉) spring.datasource.driver-class-name=com.mysql.jdbc.Driver #設(shè)置需要被掃描的包 #mybatis.type-aliases-package=java.com.example.demo #打印sql語句(一般用于本地開發(fā)測試) mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
3.Application.class添加掃描
(路徑為自己項(xiàng)目package的路徑)
@SpringBootApplication
@ServletComponentScan
@MapperScan("com.example.mapper") //mybatis包掃描
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.創(chuàng)建Mapper
@Mapper
public interface DemoMapper {
//增
@Insert("INSERT INTO demo(name,age)VALUES(#{name},#{age})")
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id") //獲取插入后自動生成的主鍵,keyProperty對應(yīng)類屬性名,keyColumn對應(yīng)數(shù)據(jù)庫字段名
int add(Demo demo);
//刪
@Delete("DELETE FROM demo WHERE id=#{id}")
boolean deleteById(int id);
//查
@Select("SELECT * FROM demo WHERE id=#{id}")
Demo findById(int id);
@Select("SELECT * FROM demo WHERE login=#{login} AND password=#{password}")
Demo findByObject(@Param("login") String login,@Param("password") String password);
//改,注意:需要指定參數(shù)映射@Param,如不指定,可按下面的方法執(zhí)行
@Update("UPDATE demo SET name=#{name} WHERE id =#{id}")
boolean updateById(@Param("name") String name,@Param("id") int id);
@Update("UPDATE demo SET name=#{name} WHERE id =#{id}")
boolean update_2ById(Demo demo);
//批量增
@InsertProvider(type = LoginProvider.class,method = "insert")
int insert(@Param("demoList")List<Demo> demoList);
//批量刪
@DeleteProvider(type = LoginProvider.class,method = "delete")
boolean delete(@Param("demoList")List<Demo> demoList);
//批量查
@Select("SELECT * FROM demo")
List<Demo> find();
@SelectProvider(type = LoginProvider.class,method = "find2")
List<Demo>find2(@Param("demoList")List<Demo> demoList);
//批量改
@UpdateProvider(type = LoginProvider.class,method = "update")
boolean update(@Param("demoList")List<Demo> demoList);
}
5.創(chuàng)建provider實(shí)現(xiàn)類
為注解@UpdateProvider、@InsertProvider、@DeleteProvider、@SelectProvider返回可執(zhí)行SQL語句,需注意:要添加@Param注解,指定映射參數(shù)
public class LoginProvider {
public String insert(@Param("demoList")List<Demo> demoList){
StringBuilder builder=new StringBuilder();
builder.append("INSERT INTO demo(name,age)VALUES");
String message="(''{0}'',{1})";
int i=1;
for (Demo demo : demoList) {
String s = MessageFormat.format(message, demo.getName(), demo.getAge());
builder.append(s);
if (i==demoList.size()){break;}
builder.append(",");
i++;
}
return builder.toString();
}
public String delete(@Param("demoList")List<Demo> demoList){
StringBuilder builder=new StringBuilder();
builder.append("DELETE FROM demo WHERE id IN (");
int i=1;
for (Demo demo : demoList) {
builder.append(demo.getId());
if (i==demoList.size()){break;}
builder.append(",");
i++;
}
builder.append(")");
return builder.toString();
}
public String find2(@Param("demoList")List<Demo> demoList){
StringBuilder builder=new StringBuilder();
builder.append("SELECT * FROM demo WHERE id IN (");
int i=1;
for (Demo demo : demoList) {
builder.append(demo.getId());
if (i==demoList.size()){break;}
builder.append(",");
i++;
}
builder.append(")");
return builder.toString();
}
public String update(@Param("demoList")List<Demo> demoList){
StringBuilder builder=new StringBuilder();
builder.append("INSERT INTO demo(id,name,age)VALUES");
String message="({0},''{1}'',{2})";
int i=1;
for (Demo demo : demoList) {
String s = MessageFormat.format(message, demo.getId(), demo.getName(), demo.getAge());
builder.append(s);
if (i==demoList.size()){break;}
builder.append(",");
i++;
}
builder.append("on duplicate key update id=VALUES(id),age=values(age),name=VALUES(name)");
return builder.toString();
}
}
Springboot整合mybatis(注解而且能看明白版本)
Springboot整合Mybatis實(shí)現(xiàn)一個最基本的增刪改查功能,整合的方式有兩種一種是注解形式的,也就是沒有Mapper.xml文件,還有一種是XML形式的,我推薦的是使用注解形式,為什么呢?因?yàn)楦拥暮喗椋瑴p少不必要的錯誤。
1.環(huán)境配置
對于環(huán)境配置我是用了一張表來展示,版本之間差異不大,你可以基于其他版本進(jìn)行測試。

這就是我的基本的環(huán)境。下一步我們一步一步來整合一波
2.整合Mybatis
第一步:數(shù)據(jù)庫新建Person表

這個表結(jié)構(gòu)很簡單,也就是三個字段id、name、age。并以id為主鍵且遞增。
第二步:新建Springboot項(xiàng)目
這個比較簡單,這里先給出一個最終的目錄結(jié)構(gòu):

第三步:導(dǎo)入相關(guān)依賴

OK,我們只需要加上這些依賴即可。在我們的pom文件。
第四步:更改application.yml配置文件
我們只需要把a(bǔ)pplication.properties文件改為yml格式即可。此時添加相關(guān)配置

這里的配置有點(diǎn)多,不過還是一個最基本的配置都在這。
第五步:新建dao包,在dao包下新建Person類

這個類是和我們數(shù)據(jù)庫中的Person類一一對應(yīng)的。
第六步:新建mapper包,在mapper新建PersonMapper類
在這個類中,我們實(shí)現(xiàn)基本的增刪改查功能接口:

這就是最基本的一個增刪改查操作的接口。
第七步:新建service包,在service包創(chuàng)建PersonService接口

第八步:在service包下創(chuàng)建PersonServiceImpl接口實(shí)現(xiàn)類

第九步:編寫controller層

第十步:在啟動主類添加掃描器

第十一步:測試
在瀏覽器輸入相應(yīng)的路徑即可。OK。大功告成。只要你按照上面的步驟一步一步來,就一定OK。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
創(chuàng)建動態(tài)代理對象bean,并動態(tài)注入到spring容器中的操作
這篇文章主要介紹了創(chuàng)建動態(tài)代理對象bean,并動態(tài)注入到spring容器中的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
IDEA?2022最新激活碼注冊碼超詳細(xì)教程(親測激活有效)
這篇文章主要介紹了IDEA?2022最新激活碼超詳細(xì)教程(親測激活至2099年),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
java實(shí)現(xiàn)稀疏矩陣的壓縮與解壓的方法
這篇文章主要介紹了java實(shí)現(xiàn)稀疏矩陣的壓縮與解壓 ,把該稀疏矩陣壓縮以三元組形式表示并以文件形式保存,再寫另一個程序讀取文件中的信息把壓縮后的三元組還原成原來的稀疏矩陣,需要的朋友可以參考下2022-03-03
SpringBoot四種讀取properties文件的方式(小結(jié))
這篇文章主要介紹了SpringBoot四種讀取properties文件的方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Java創(chuàng)建和啟動線程的兩種方式實(shí)例分析
這篇文章主要介紹了Java創(chuàng)建和啟動線程的兩種方式,結(jié)合實(shí)例形式分析了java多線程創(chuàng)建、使用相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-09-09

