springboot2.x整合tkmapper的示例代碼
springboot整合tkmapper
1.導(dǎo)入pom依賴
1.1 導(dǎo)入springboot的parent依賴
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.9.RELEASE</version>
</parent>
1.2 導(dǎo)入具體依賴
<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配置類
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訪問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)訪問根路徑是:/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)類上添加掃描注解
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í)體類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è)試類,進(jìn)行單表的CRUD操作
@SpringBootTest
@RunWith(SpringRunner.class)
public class AppTest {
@Autowired
private ProductMapper productMapper;
@Test//查詢所有
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 多條件查詢和分頁(yè)查詢
@SpringBootTest
@RunWith(SpringRunner.class)
public class QueryTest {
@Autowired
private ProductMapper productMapper;
@Test //根據(jù)多條件動(dòng)態(tài)查詢
public void queryByParam(){
//多條件查詢
Example example = new Example(TProduct.class);
// //添加第1個(gè)條件 name:模糊查詢
// example.and().andLike("name","%洗澡8%");
//
// //添加第2個(gè)條件 :價(jià)格在100以內(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ì)象:就是用于拼接查詢條件,每次執(zhí)行example.and()或者example.or()將都會(huì)創(chuàng)建一個(gè)新的查詢條件的拼接對(duì)象(意味著多一組())
and.andLike("name","%洗澡8%").orEqualTo("state",1);
//再創(chuàng)建一組新的區(qū)間查詢條件,這個(gè)條件它要加(),所以你要重新通過 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è)查詢
public void queryByPage(){
//不帶條件的分頁(yè)查詢
//如果要進(jìn)行分頁(yè)查詢,只需在調(diào)用查詢的方法前,設(shè)置分頁(yè)參數(shù)即可
//特點(diǎn)注意:當(dāng)前設(shè)置的分頁(yè)參數(shù),只適用于離它最近的這條查詢
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í)體類的主鍵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函數(shù)式編程之通過行為參數(shù)化傳遞代碼
行為參數(shù)化就是可以幫助你處理頻繁變更的需求的一種軟件開發(fā)模式,這篇文章將給大家詳細(xì)的介紹一下Java函數(shù)式編程之行為參數(shù)化傳遞代碼,感興趣的同學(xué)可以參考閱讀下2023-08-08
springboot整合netty實(shí)現(xiàn)心跳檢測(cè)和自動(dòng)重連
本文主要介紹了Spring Boot中整合Netty實(shí)現(xiàn)心跳檢測(cè)和自動(dòng)重連,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
@PathVariable和@RequestParam傳參為空問題及解決
這篇文章主要介紹了@PathVariable和@RequestParam傳參為空問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
最新jsonwebtoken-jwt 0.12.3 基本使用小結(jié)
這篇文章主要介紹了最新jsonwebtoken-jwt 0.12.3 基本使用小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
詳解微信開發(fā)之Author網(wǎng)頁(yè)授權(quán)
微信開發(fā)中,經(jīng)常有這樣的需求:獲得用戶頭像、綁定微信號(hào)給用戶發(fā)信息,那么實(shí)現(xiàn)這些的前提就是授權(quán)!本文對(duì)此進(jìn)行系統(tǒng)介紹,需要的朋友一起來(lái)看下吧2016-12-12
SpringBoot項(xiàng)目中公共字段填充的實(shí)現(xiàn)
本文主要介紹了SpringBoot項(xiàng)目中公共字段填充的實(shí)現(xiàn),利用SpringBoot的Aop思想和自定義注解和反射機(jī)制的方法來(lái)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
struts2中simple主題下<s:fieldError>標(biāo)簽?zāi)J(rèn)樣式的移除方法
這篇文章主要給大家介紹了關(guān)于struts2中simple主題下<s:fieldError>標(biāo)簽?zāi)J(rèn)樣式的移除方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn)
mongodb4.0也出來(lái)一段時(shí)間了,這個(gè)版本最為大眾期待的特性就是支持了多文檔事務(wù)。這篇文章主要介紹了SpringDataMongoDB多文檔事務(wù)的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2018-11-11

