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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring集成Mybatis plus

發(fā)布時(shí)間:2023/12/31 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring集成Mybatis plus 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Springboot中流行的持久層框架是Hibernate和Mybatis

前者已經(jīng)成為Spring Data的規(guī)范,以極簡(jiǎn)和全自動(dòng)的對(duì)象映射著稱,后者是一個(gè)半自動(dòng)化的ORM框架,SQL獨(dú)立存放在XML文件中,提高自由度的同時(shí)也方便DBA進(jìn)行校對(duì)和后續(xù)SQL優(yōu)化

由于Mybatis plus完全基于Mybatis,且吸收了一部分HIbernate的優(yōu)點(diǎn),提供集成的CRUD,基本上現(xiàn)在開發(fā)中都使用Mybatis Plus替代原生Mybatis框架

敲黑板: MybatisPlus 不能和 Mybatis同時(shí)使用

使用流程(三步走)

  • 添加Maven依賴
  • MyBatisPlus 配置
  • 編寫3種文件(可使用插件生成,Idea Mybatis generater)
    • 實(shí)體entity
    • Dao層接口Mapper
    • Xml文件中的Sql語(yǔ)句
  • 第一步 添加Maven依賴

    <dependency><groupId>com.baomidou</groupId><artifactId>mybatisplus-spring-boot-starter</artifactId> </dependency> <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId> </dependency> 復(fù)制代碼

    第二步 在application.yml中配置Mybatis Plus

    使用properties配置文件的可以在www.toyaml.com/index.html進(jìn)行yml文件的轉(zhuǎn)換

    重點(diǎn)!!!

    第一行的xml文件掃描,此處的xml文件存放在resource下的mapper路徑下(我的路徑是resource/mapper/xx模塊/xxx.xml)
    第二行的實(shí)體掃描!!!要根據(jù)自己的entity目錄進(jìn)行配置 自己根據(jù)自己的實(shí)體類路徑修改typeAliasesPackage,或者使用我的這種模塊化工程路徑(工程根目錄/modules/xx模塊/entity/xxx.class)

    mybatis-plus: mapper-locations: classpath:mapper/**/*.xml #__對(duì)應(yīng)第三步的mapper路徑__#實(shí)體掃描,多個(gè)package用逗號(hào)或者分號(hào)分隔 typeAliasesPackage: io.renren.modules.*.entity #__對(duì)應(yīng)第三步的entity路徑__ global-config:#主鍵類型 0:"數(shù)據(jù)庫(kù)ID自增", 1:"用戶輸入ID",2:"全局唯一ID (數(shù)字類型唯一ID)", 3:"全局唯一ID UUID"; id-type: 0#字段策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷" field-strategy: 2#駝峰下劃線轉(zhuǎn)換 db-column-underline: true#刷新mapper 調(diào)試神器 refresh-mapper: true#數(shù)據(jù)庫(kù)大寫下劃線轉(zhuǎn)換#capital-mode: true#序列接口實(shí)現(xiàn)類配置#key-generator: com.baomidou.springboot.xxx#邏輯刪除配置 logic-delete-value: -1 logic-not-delete-value: 0#自定義填充策略接口實(shí)現(xiàn)#meta-object-handler: com.baomidou.springboot.xxx#自定義SQL注入器 sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector configuration: map-underscore-to-camel-case: true cache-enabled: false call-setters-on-nulls: true 復(fù)制代碼

    沒(méi)添加mysq數(shù)據(jù)源的再添加如下配置

    spring: datasource: continue-on-error: false driver-class-name: com.mysql.jdbc.Driver username: root ##根據(jù)自己MySQL的用戶名修改 url: jdbc:mysql://127.0.0.1:3306/renren_test ##根據(jù)自己MySQL的ip和端口修改 password: root ##根據(jù)自己MySQL的密碼修改復(fù)制代碼

    第三步 進(jìn)入正題

    之前的步驟都是Mybatis的配置和管理,以下才是真正的業(yè)務(wù)代碼
  • entity 文件放在entity目錄下(要與第二步的實(shí)體掃描路徑對(duì)應(yīng),忽略的回去仔細(xì)看第二步)
  • @TableName("sword") public class Sword {@TableIdprivate Long id;private String name;} 復(fù)制代碼

    之前用過(guò)JPA的可能直接運(yùn)行發(fā)現(xiàn)并沒(méi)有在數(shù)據(jù)庫(kù)中建表,因?yàn)镸ybatis不支持自動(dòng)根據(jù)實(shí)體類創(chuàng)建表,所以需要自己建表,如果要引入這個(gè)功能可以引入JPA,只利用JPA的自動(dòng)建表功能,本文為了簡(jiǎn)單明了不涉及,可以查看我另一篇文章Mybatis加入JPA的自動(dòng)建表功能

  • mapper放在Dao目錄下
  • @Repository @Mapper public interface SwordMapper {Sword selectSword(Pagination page, String id);void insertSword(@Param("name") String name); } 復(fù)制代碼
  • xml(要與第二步的mapper掃描路徑對(duì)應(yīng),忽略的回去仔細(xì)看第二步)
  • <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="io.renren.modules.test.Dao.SwordMapper"><select id="selectSword" resultType="io.renren.modules.test.entity.Sword">select * from sword limit 1</select><insert id="insertSword">insert into sword (name) values (#{name})</insert></mapper> 復(fù)制代碼

    Mybatis框架的文件引入方式:

    • entity通過(guò)第二步的配置typeAliasesPackage進(jìn)行查找,也可以通過(guò)注解方式查找
    • Mybatis的xml與mapper對(duì)應(yīng),mapper提供java層的接口,xml保存相對(duì)應(yīng)的sql語(yǔ)句
    • xml在第二步的配置中由Mybatis發(fā)現(xiàn),相對(duì)應(yīng)的Mapper文件在xml中通過(guò)namespace的方式關(guān)聯(lián)

    最后一步 測(cè)試

    在Controller文件中實(shí)現(xiàn)Restful接口 SwordController.class

    @RestController @RequestMapping public SwordController {@Autowired // 自動(dòng)注入mapperprivate SwordMapper swordMapper;@GetMapping("sword")public sword(UserEntity userEntity) {Sword sword1 = new Sword();sword1.setName("這是一把劍");Sword sword = swordMapper.selectSword("1");return sword;} } 復(fù)制代碼

    訪問(wèn)127.0.0.1:8080/demo/sword

    大功告成!

    后記

    在其他的博客中大家可能看到,有很多利用sqlsession或者SqlSessionFactoryBean來(lái)加載Mybatis配置文件,我這里又沒(méi)有sqlsession,經(jīng)常有人會(huì)被搞得一頭霧水,這里解釋一下,在原生的mybatis中,sqlsession可以由SqlSessionFactory創(chuàng)建;而在mybatis-spring中則需要通過(guò)SqlSessionFactoryBean來(lái)創(chuàng)建,并傳入datasource。 像這樣

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation"> <value>classpath:mybatis/mapper.xml</value> </property> <property name="dataSource" ref="dataSource" /> </bean> 復(fù)制代碼

    但是在Springboot中,mybatis-spring-boot支持自動(dòng)創(chuàng)建并注冊(cè)SqlSessionFactoryBean,所以sqlsession的配置并不需要。感謝Springboot.

    總結(jié)

    以上是生活随笔為你收集整理的Spring集成Mybatis plus的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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