plus 什么是mybais_谈谈自己用mybatis-plus中的一些经验。
軟件:idea 然后用spring boot 數據庫用的postgersql
在談mybatis-plus之前,我先說一個開發中非常好用的插件吧。用了這款插件之后,開發起來,會快很多。他就是 ----- > Mybatis plugins我這是安裝好了的,沒安裝好之前是install,直接點就可以了。
下面開始我們今天的主題:mybatis-plus
首先先導入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
然后在配置文件中配置數據源
# 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/項目名
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
#邏輯刪除的狀態
mybatis-plus:
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
數據源配置好之后新建一個包generator然后在其中新建一個類CodeGenerator(這個隨便自己)自動生成,超級方便。
然后運行輸入你的表名,就會自動生成相應的mapper層,server層,controller層等等
下面說的是個人習慣:
將mapper層中加入Mapper注解(這個注解可以不加,在啟動類中加入@MapperScan("com.example.demo.mapper")也可以),controller層加入RestController注解。然后就可以寫業務了,非常的舒服。這是一個controller的一個例子
entity層的話,可以繼承下Model類,然后重寫pkVal方法這里面的注解什么的在下面解釋下。
一。 如果你的刪除是邏輯刪除,就在你的狀態上加上@TableLogic注解,
然后在你的config中注入
@Bean
public LogicSqlInjector logicSqlInjector(){
return new LogicSqlInjector() ;
}
二。如果業務需要加上樂觀鎖,只需要在你的版本號字段上面加上@Version注解,和jpa一樣方便。
然后在你的config中注入
//樂觀鎖(目前只支持updateById和update方法)
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
三。 分頁也需要注入,不注入也可以分頁吧,不過不是數據庫分頁,你可以看生成的sql語句就知道了。
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@TableField注解 表示要和數據庫的名字一樣
@TableField( "update_time")
private LocalDateTime updateTime;
@TableId(type = IdType.AUTO) //表示mysql的自增,我用的是postgresql,所以沒有試過。
@TableField(condition = SqlCondition.LIKE)//模糊查詢,不設置的話會默認。
總結
以上是生活随笔為你收集整理的plus 什么是mybais_谈谈自己用mybatis-plus中的一些经验。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浦发梦卡之斗鱼联名卡额度是多少?怎么提额
- 下一篇: 投标报价得分计算程序_什么是投标报价?怎