springboot2.x整合tkmapper的示例代碼
springboot整合tkmapper
1.導(dǎo)入pom依賴(lài)
1.1 導(dǎo)入springboot的parent依賴(lài)
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.9.RELEASE</version>
</parent>
1.2 導(dǎo)入具體依賴(lài)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- druid連接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- tkmapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- pagehelper分頁(yè)插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
</dependencies>
2. 添加tkmapper數(shù)據(jù)庫(kù)連接配置
創(chuàng)建application.yml配置類(lèi)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 連接池指定 springboot2.02版本默認(rèn)使用HikariCP 此處要替換成Druid
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///pethome?serverTimezone=Asia/Shanghai&characterEncoding=utf-8
username: root
password: qwe123
druid:
initial-size: 5 # 初始化時(shí)建立物理連接的個(gè)數(shù)
min-idle: 5 # 最小連接池連接數(shù)量,最小空閑數(shù)量
max-active: 20 # 最大連接池連接數(shù)量,最大活躍連接數(shù)
max-wait: 60000 # 配置獲取連接等待超時(shí)的時(shí)間
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
stat-view-servlet:
allow: 0.0.0.0 # 允許哪些IP訪問(wèn)druid監(jiān)控界面,多個(gè)IP以逗號(hào)分隔
login-username: admin # 設(shè)置登錄帳號(hào)
login-password: 123456 # 設(shè)置登錄密碼
reset-enable: false # 是否允許重置數(shù)據(jù)
# url-pattern: /database/* # 默認(rèn)訪問(wèn)根路徑是:/druid/;也可以自定義設(shè)置
# mybatis配置
mybatis:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 設(shè)置控制臺(tái)輸入執(zhí)行的sql語(yǔ)句
type-aliases-package: org.example.model
# tkmapper配置
mapper:
not-empty: false
identity: mysql #指定tkmapper加載的數(shù)據(jù)庫(kù)
3. 在啟動(dòng)類(lèi)上添加掃描注解
MainApp.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "org.example.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
4.tkmapper的使用
4.1 創(chuàng)建mapper.java
public interface ProductMapper extends Mapper<TProduct> {
}
4.2 創(chuàng)建表對(duì)應(yīng)的實(shí)體類(lèi)TProduct
@Data
public class TProduct {
@Id //指定主鍵的注解
private Long id;
private String name;
private String resources;
private Double saleprice;
private java.util.Date offsaletime;
private java.util.Date onsaletime;
private Long state;
private String costprice;
private java.util.Date createtime;
private Long salecount;
}
4.3 添加測(cè)試類(lèi),進(jìn)行單表的CRUD操作
@SpringBootTest
@RunWith(SpringRunner.class)
public class AppTest {
@Autowired
private ProductMapper productMapper;
@Test//查詢(xún)所有
public void findAll(){
List<TProduct> tProducts = productMapper.selectAll();
for (TProduct tProduct : tProducts) {
System.out.println(tProduct);
}
}
@Test
public void insert(){
TProduct product = new TProduct();
product.setName("我是測(cè)試的");
product.setCreatetime(new Date());
product.setState(1L);
productMapper.insert(product);
}
@Test
public void updateById(){
TProduct product = new TProduct();
product.setId(174L);
product.setName("我是測(cè)試");
//如果修改時(shí),只想改變更新的name值,其他值不改
//下面這個(gè)方法,是無(wú)論修改的值是否為空,將全部修改
// productMapper.updateByPrimaryKey(product);
//下面的方法,只改非空的字段.
//注意:tkmapper中,凡是方法名以Selective結(jié)尾的,就是在拼接動(dòng)態(tài)sql
//即,不更新非空的字段
product.setCreatetime(new Date());
productMapper.updateByPrimaryKeySelective(product);
}
@Test//刪除操作
public void delete(){
productMapper.deleteByPrimaryKey(174L);
}
4.4 多條件查詢(xún)和分頁(yè)查詢(xún)
@SpringBootTest
@RunWith(SpringRunner.class)
public class QueryTest {
@Autowired
private ProductMapper productMapper;
@Test //根據(jù)多條件動(dòng)態(tài)查詢(xún)
public void queryByParam(){
//多條件查詢(xún)
Example example = new Example(TProduct.class);
// //添加第1個(gè)條件 name:模糊查詢(xún)
// example.and().andLike("name","%洗澡8%");
//
// //添加第2個(gè)條件 :價(jià)格在100以?xún)?nèi)
// example.and()
// .andGreaterThanOrEqualTo("saleprice",0).andLessThanOrEqualTo("saleprice",100);
//
// //添加第3個(gè)條件:狀態(tài) state =1
// example.and().andEqualTo("state",1);
//優(yōu)化Sql中的括號(hào) : 當(dāng)多個(gè)條件如果是 平級(jí),則不用example.and()去追加條件
Example.Criteria and = example.and();//Criteria對(duì)象:就是用于拼接查詢(xún)條件,每次執(zhí)行example.and()或者example.or()將都會(huì)創(chuàng)建一個(gè)新的查詢(xún)條件的拼接對(duì)象(意味著多一組())
and.andLike("name","%洗澡8%").orEqualTo("state",1);
//再創(chuàng)建一組新的區(qū)間查詢(xún)條件,這個(gè)條件它要加(),所以你要重新通過(guò) example對(duì)象獲取
example.and().andGreaterThanOrEqualTo("saleprice",0).andLessThanOrEqualTo("saleprice",100);
List<TProduct> tProducts = productMapper.selectByExample(example);
for (TProduct tProduct : tProducts) {
System.out.println(tProduct);
}
}
@Test //分頁(yè)查詢(xún)
public void queryByPage(){
//不帶條件的分頁(yè)查詢(xún)
//如果要進(jìn)行分頁(yè)查詢(xún),只需在調(diào)用查詢(xún)的方法前,設(shè)置分頁(yè)參數(shù)即可
//特點(diǎn)注意:當(dāng)前設(shè)置的分頁(yè)參數(shù),只適用于離它最近的這條查詢(xún)
PageHelper.startPage(1,3);
//List<TProduct> tProducts = productMapper.selectAll();
PageInfo<TProduct> pageInfo = new PageInfo<>(productMapper.selectAll());
/*
pageInfo中的常用的方法:
總記錄數(shù):pageInfo.getTotal()
總頁(yè)數(shù):pageInfo.getPages()
每頁(yè)的數(shù)據(jù)列表:pageInfo.getList()
*/
System.out.println(pageInfo);
}
4.5 添加數(shù)據(jù)后,立馬得到添加數(shù)據(jù)的主鍵
當(dāng)前這個(gè)主鍵是由數(shù)據(jù)庫(kù)進(jìn)行【自增長(zhǎng)】設(shè)置的
在實(shí)體類(lèi)的主鍵ID上添加如下配置
public class TProduct {
@Id //指定主鍵的注解
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
在需要獲取的地方,直接調(diào)用get方法即可
@Test //添加新數(shù)據(jù)后,獲取 自增長(zhǎng)主鍵
public void insertAndGetId(){
TProduct product = new TProduct();
product.setName("我是測(cè)試的");
product.setCreatetime(new Date());
product.setState(1L);
productMapper.insert(product);
System.out.println(product.getId());
}
到此這篇關(guān)于springboot2.x整合tkmapper的文章就介紹到這了,更多相關(guān)springboot2.x整合tkmapper內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java編程之AC自動(dòng)機(jī)工作原理與實(shí)現(xiàn)代碼
這篇文章主要介紹了java編程之AC自動(dòng)機(jī)的有關(guān)內(nèi)容,涉及其應(yīng)用場(chǎng)景,運(yùn)行原理,運(yùn)行過(guò)程,構(gòu)造方法及Java中的實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
基于Ajax用戶名驗(yàn)證、服務(wù)條款加載、驗(yàn)證碼生成的實(shí)現(xiàn)方法
本篇文章對(duì)Ajax用戶名驗(yàn)證、服務(wù)條款加載、驗(yàn)證碼生成的實(shí)現(xiàn)方法,進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
Springboot實(shí)現(xiàn)高吞吐量異步處理詳解(適用于高并發(fā)場(chǎng)景)
這篇文章主要介紹了Springboot實(shí)現(xiàn)高吞吐量異步處理詳解(適用于高并發(fā)場(chǎng)景),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
基于Java編寫(xiě)一個(gè)簡(jiǎn)單的風(fēng)控組件
這篇文章主要為大家詳細(xì)介紹了如何基于Java編寫(xiě)一個(gè)簡(jiǎn)單的風(fēng)控組件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-12-12
SpringCloud Zuul過(guò)濾器實(shí)現(xiàn)登陸鑒權(quán)代碼實(shí)例
這篇文章主要介紹了SpringCloud Zuul過(guò)濾器實(shí)現(xiàn)登陸鑒權(quán)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
淺談java中對(duì)集合對(duì)象list的幾種循環(huán)訪問(wèn)
下面小編就為大家?guī)?lái)一篇java中對(duì)集合對(duì)象list的幾種循環(huán)訪問(wèn)詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
springboot的類(lèi)加載器(org.springframework.boot.loader)過(guò)程詳解
這篇文章主要介紹了springboot的類(lèi)加載器(org.springframework.boot.loader),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
springboot實(shí)現(xiàn)攔截器之驗(yàn)證登錄示例
本篇文章主要介紹了springboot實(shí)現(xiàn)攔截器之驗(yàn)證登錄示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Spring中事務(wù)用法示例及實(shí)現(xiàn)原理詳解
這篇文章主要給大家介紹了關(guān)于Spring中事務(wù)用法示例及實(shí)現(xiàn)原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11

