springboot 2.x整合mybatis實現(xiàn)增刪查和批量處理方式
springboot 2.x整合mybatis實現(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驅動包-->
<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ù)據(jù)源(默認數(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 #驅動(會自動檢測配置,可以注釋掉) spring.datasource.driver-class-name=com.mysql.jdbc.Driver #設置需要被掃描的包 #mybatis.type-aliases-package=java.com.example.demo #打印sql語句(一般用于本地開發(fā)測試) mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
3.Application.class添加掃描
(路徑為自己項目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對應類屬性名,keyColumn對應數(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實現(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實現(xiàn)一個最基本的增刪改查功能,整合的方式有兩種一種是注解形式的,也就是沒有Mapper.xml文件,還有一種是XML形式的,我推薦的是使用注解形式,為什么呢?因為更加的簡介,減少不必要的錯誤。
1.環(huán)境配置
對于環(huán)境配置我是用了一張表來展示,版本之間差異不大,你可以基于其他版本進行測試。

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

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

第三步:導入相關依賴

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

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

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

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

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

第九步:編寫controller層

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

第十一步:測試
在瀏覽器輸入相應的路徑即可。OK。大功告成。只要你按照上面的步驟一步一步來,就一定OK。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java hashCode原理以及與equals()區(qū)別聯(lián)系詳解
在 Java 應用程序執(zhí)行期間,在同一對象上多次調用 hashCode 方法時,必須一致地返回相同的整數(shù),前提是對象上 equals 比較中所用的信息沒有被修改。從某一應用程序的一次執(zhí)行到同一應用程序的另一次執(zhí)行,該整數(shù)無需保持一致2022-11-11
任何Bean通過實現(xiàn)ProxyableBeanAccessor接口即可獲得動態(tài)靈活的獲取代理對象或原生對象的能力(最新推
這篇文章主要介紹了任何Bean通過實現(xiàn)ProxyableBeanAccessor接口即可獲得動態(tài)靈活的獲取代理對象或原生對象的能力,通過示例代碼看到,借助ProxyableBeanAccessor接口默認實現(xiàn)的getReal、getProxy、selfAs方法,很靈活的按需獲取代理或非代理對象,需要的朋友可以參考下2024-02-02
Java實現(xiàn)文件上傳到服務器本地并通過url訪問的方法步驟
最近項目中使用到了文件上傳到服務器的功能,下面這篇文章主要給大家介紹了關于Java實現(xiàn)文件上傳到服務器本地并通過url訪問的方法步驟,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04
Spring 處理 HTTP 請求參數(shù)注解的操作方法
這篇文章主要介紹了Spring 處理 HTTP 請求參數(shù)注解的操作方法,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友參考下吧2024-04-04

