日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

plus 什么是mybais_谈谈自己用mybatis-plus中的一些经验。

發(fā)布時(shí)間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 plus 什么是mybais_谈谈自己用mybatis-plus中的一些经验。 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

軟件:idea 然后用spring boot 數(shù)據(jù)庫用的postgersql

在談mybatis-plus之前,我先說一個(gè)開發(fā)中非常好用的插件吧。用了這款插件之后,開發(fā)起來,會(huì)快很多。他就是 ----- > Mybatis plugins我這是安裝好了的,沒安裝好之前是install,直接點(diǎn)就可以了。

下面開始我們今天的主題:mybatis-plus

首先先導(dǎo)入jar包

com.baomidou

mybatis-plus-boot-starter

3.0.1

com.baomidou

mybatisplus-spring-boot-starter

1.0.5

org.apache.velocity

velocity-engine-core

2.0

然后在配置文件中配置數(shù)據(jù)源

# Tomcat

server:

tomcat:

uri-encoding: UTF-8

max-threads: 1000

min-spare-threads: 30

port: 8880

spring:

application:

name: userinfo_plus

datasource:

type: com.alibaba.druid.pool.DruidDataSource

driverClassName: org.postgresql.Driver

druid:

url: jdbc:postgresql://xx.xx.xx.xx/項(xiàng)目名

username: postgres

password: postgres

initial-size: 10

max-active: 100

min-idle: 10

max-wait: 60000

pool-prepared-statements: true

max-pool-prepared-statement-per-connection-size: 20

time-between-eviction-runs-millis: 60000

min-evictable-idle-time-millis: 300000

validation-query: SELECT 1

test-while-idle: true

test-on-borrow: false

test-on-return: false

stat-view-servlet:

enabled: true

url-pattern: /druid/*

filter:

stat:

log-slow-sql: true

slow-sql-millis: 1000

merge-sql: true

wall:

config:

multi-statement-allow: true

mybatis:

mapperLocations: classpath:mybatis/mapping/**/*.xml

config-location: classpath:mybatis/mybatis-config.xml

#邏輯刪除的狀態(tài)

mybatis-plus:

global-config:

db-config:

logic-delete-value: 1

logic-not-delete-value: 0

數(shù)據(jù)源配置好之后新建一個(gè)包generator然后在其中新建一個(gè)類CodeGenerator(這個(gè)隨便自己)自動(dòng)生成,超級(jí)方便。

然后運(yùn)行輸入你的表名,就會(huì)自動(dòng)生成相應(yīng)的mapper層,server層,controller層等等

下面說的是個(gè)人習(xí)慣:

將mapper層中加入Mapper注解(這個(gè)注解可以不加,在啟動(dòng)類中加入@MapperScan("com.example.demo.mapper")也可以),controller層加入RestController注解。然后就可以寫業(yè)務(wù)了,非常的舒服。這是一個(gè)controller的一個(gè)例子

entity層的話,可以繼承下Model類,然后重寫pkVal方法這里面的注解什么的在下面解釋下。

一。 如果你的刪除是邏輯刪除,就在你的狀態(tài)上加上@TableLogic注解,

然后在你的config中注入

@Bean

public LogicSqlInjector logicSqlInjector(){

return new LogicSqlInjector() ;

}

二。如果業(yè)務(wù)需要加上樂觀鎖,只需要在你的版本號(hào)字段上面加上@Version注解,和jpa一樣方便。

然后在你的config中注入

//樂觀鎖(目前只支持updateById和update方法)

@Bean

public OptimisticLockerInterceptor optimisticLockerInterceptor() {

return new OptimisticLockerInterceptor();

}

三。 分頁也需要注入,不注入也可以分頁吧,不過不是數(shù)據(jù)庫分頁,你可以看生成的sql語句就知道了。

@Bean

public PaginationInterceptor paginationInterceptor() {

return new PaginationInterceptor();

}

@TableField注解 表示要和數(shù)據(jù)庫的名字一樣

@TableField( "update_time")

private LocalDateTime updateTime;

@TableId(type = IdType.AUTO) //表示mysql的自增,我用的是postgresql,所以沒有試過。

@TableField(condition = SqlCondition.LIKE)//模糊查詢,不設(shè)置的話會(huì)默認(rèn)。

總結(jié)

以上是生活随笔為你收集整理的plus 什么是mybais_谈谈自己用mybatis-plus中的一些经验。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。