日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

SpringBoot整合MyBatis超詳細教程

瀏覽:168日期:2023-03-11 14:03:15
1.整合MyBatis操作

前面一篇提到了SpringBoot整合基礎的數據源JDBC、Druid操作,實際項目中更常用的還是MyBatis框架,而SpringBoot整合MyBatis進行CRUD也非常方便。

下面從配置模式、注解模式、混合模式三個方面進行說明MyBatis與SpringBoot的整合。

1.1.配置模式

MyBatis配置模式是指使用mybatis配置文件的方式與SpringBoot進行整合,相對應的就有mybatis-config.xml(用于配置駝峰命名,也可以省略這個文件)、XxxMapper.xml文件。

主要步驟為:

導入mybatis官方starter 編寫mapper接口。標準@Mapper注解 編寫sql映射文件并綁定mapper接口

在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息 (建議;配置在mybatis.configuration中,可以省略mybatis-config.xml文件)

下面是具體整合配置步驟:

①引入相關依賴pom.xml配置:

pom.xml

<dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><!--整合mybatis--><dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration> <excludes><exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId></exclude> </excludes></configuration> </plugin></plugins> </build>

②編寫對應Mapper接口:

@Mapper //這個注解表示了這個類是一個mybatis的mapper接口類@Repositorypublic interface UserMapper { //@Select('select * from user') List<User> findAllUsers(); //@Insert('insert into user(id, username, password) values (#{id}, #{username}, #{password})') void insert(User user); //@Update('update user set username = #{username}, password = #{password} where id = #{id}') void update(User user); //@Delete('delete from user where id = #{id}') void deleteById(Integer id);}

③在resources下創建對應的mapper文件,對應domain類,數據庫表單如下:

User類:

@Datapublic class User { private Integer id; private String username; private String password;}

數據庫user表:

SpringBoot整合MyBatis超詳細教程

UserMapper.xml文件:

<?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'><!--namespace表示當前mapper的唯一標識:一般使用domain的全路徑名+Mapper來命名--><mapper namespace='com.fengye.springboot_mybatis.mapper.UserMapper'> <!--id:方法表示,一般配置對應的方法;resultType:表示該方法有返回,返回需要封裝到對應實體的類型--> <select resultType='com.fengye.springboot_mybatis.entity.User'>select * from user </select> <insert parameterType='com.fengye.springboot_mybatis.entity.User'>insert into user(id, username, password) values (#{id}, #{username}, #{password}) </insert> <update parameterType='com.fengye.springboot_mybatis.entity.User'>update user set username = #{username}, password = #{password} where id = #{id} </update> <delete parameterType='Integer'>delete from user where id = #{id} </delete></mapper>

④對應配置application.yml文件:

application.yml

server: port: 8083spring: datasource: username: root password: admin #假如時區報錯,增加時區配置serverTimezone=UTC url: jdbc:mysql://localhost:3306/mybatis02_0322?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Drivermybatis: #config-location: classpath:mybatis/mybatis-config.xml 使用了configuration注解則無需再指定mybatis-config.xml文件 mapper-locations: classpath:mybatis/mapper/*.xml configuration: #指定mybatis全局配置文件中的相關配置項 map-underscore-to-camel-case: true1.2.注解模式

注解模式使用

主要步驟:

導入mybatis官方依賴 注解方式編寫mapper接口 在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息

可以看到注解模式比配置模式少了編寫Mapper.xml文件,簡化了簡單SQL語句的xml文件編寫。

下面是具體整合步驟:

①創建測試表單city,對應domain類:

建表sql:

CREATE TABLE city( id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(30), state VARCHAR(30), country VARCHAR(30));

City類:

@Datapublic class City { private Long id; private String name; private String state; private String country;}

②導入pom.xml與配置模式相同,編寫注解式CityMapper接口:

@Mapper@Repositorypublic interface CityMapper { @Select('select * from city where id = #{id}') public City getCityById(Long id); /** * 使用@Options來增加除Insert語句中其它可選參數,比如插入獲取id主鍵的值 * @param city */ @Insert('insert into city(name, state, country) values (#{name}, #{state}, #{country})') @Options(useGeneratedKeys = true, keyProperty = 'id') public void insert(City city); @Update('update city set name = #{name}, state = #{state}, country = #{country} where id = #{id}') public void update(City city); @Delete('delete from city where id = #{id}') public void deleteById(Long id);}

③編寫Service層、Controller層:

Service相關:

public interface CityService { City findCityById(Long id); void insert(City city); void update(City city); void deleteById(Long id);}@Servicepublic class CityServiceImpl implements CityService { @Autowired private CityMapper cityMapper; @Override public City findCityById(Long id) {return cityMapper.getCityById(id); } @Override public void insert(City city) {cityMapper.insert(city); } @Override public void update(City city) {cityMapper.update(city); } @Override public void deleteById(Long id) {cityMapper.deleteById(id); }}

Controller相關:

@RestController@RequestMapping('/city/api')public class CityController { @Autowired private CityService cityService; @RequestMapping('/findCityById/{id}') public City findCityById(@PathVariable('id') Long id){return cityService.findCityById(id); } @PostMapping('/insert') public String insert(City city){cityService.insert(city);return 'insert ok'; } @PostMapping('/update') public String update(City city){cityService.update(city);return 'update ok'; } @GetMapping('/delete/{id}') public String delete(@PathVariable('id') Long id){cityService.deleteById(id);return 'delete ok'; }}

④對應使用Postman接口進行測試:

簡單模擬接口POST/GET請求即可:

SpringBoot整合MyBatis超詳細教程

1.3.混合模式

在實際項目開發中涉及很多復雜業務及連表查詢SQL,可以配合使用注解與配置模式,達到最佳實踐的目的。

實際項目操作步驟:

引入mybatis-starter 配置application.yaml中,指定mapper-location位置即可 編寫Mapper接口并標注@Mapper注解 簡單方法直接注解方式 復雜方法編寫mapper.xml進行綁定映射 主啟動類上使用@MapperScan('com.fengye.springboot_mybatis.mapper') 簡化Mapper接口,包下所有接口就可以不用標注@Mapper注解

具體配置如下:

@SpringBootApplication//主啟動類上標注,在XxxMapper中可以省略@Mapper注解@MapperScan('com.fengye.springboot_mybatis.mapper')public class SpringbootMybatisApplication { public static void main(String[] args) {SpringApplication.run(SpringbootMybatisApplication.class, args); }}@Repositorypublic interface CityMapper { @Select('select * from city where id = #{id}') public City getCityById(Long id); /** * 使用@Options來增加除Insert語句中其它可選參數,比如插入獲取id主鍵的值 * @param city */ @Insert('insert into city(name, state, country) values (#{name}, #{state}, #{country})') @Options(useGeneratedKeys = true, keyProperty = 'id') public void insert(City city); @Update('update city set name = #{name}, state = #{state}, country = #{country} where id = #{id}') public void update(City city); @Delete('delete from city where id = #{id}') public void deleteById(Long id);}

本博客參考寫作文檔:

SpringBoot2核心技術與響應式編程

博客涉及代碼示例均已上傳至github地址:

SpringBootStudy

到此這篇關于SpringBoot整合MyBatis超詳細教程的文章就介紹到這了,更多相關SpringBoot整合MyBatis內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲精品一二三**| 伊人久久成人| 亚洲精品在线二区| 日韩中出av| 亚洲精品伊人| 欧美亚洲二区| 国产经典一区| 高潮一区二区| 黄色成人在线网址| 日韩一区二区三区免费播放| 999国产精品| 人人精品人人爱| 日韩精品欧美大片| 精品视频在线你懂得| 欧美精品日日操| 国产亚洲精品v| 久久国产麻豆精品| 中文字幕在线看片| 亚洲中午字幕| 国产精品1区| 亚洲先锋成人| 亚洲精品福利| 日韩av二区| 欧美精品九九| 午夜久久av| 福利一区视频| 久久高清国产| 免费在线亚洲欧美| 激情欧美日韩一区| 7m精品国产导航在线| 黄色欧美在线| 久久最新视频| 国产精品久久久久久久免费观看| 午夜免费一区| 麻豆精品在线| 一区二区三区四区日韩| 国产精品成人一区二区不卡| 三级一区在线视频先锋| 国产经典一区| 日韩高清成人| 日韩高清在线一区| 久久三级视频| 久久国内精品| 国产精品婷婷| 成人精品久久| 欧美日韩一区二区三区不卡视频| 日韩欧美一区二区三区在线观看 | 国产精品一国产精品k频道56| 麻豆国产欧美日韩综合精品二区| 日韩国产在线| 日本欧美在线| 国产综合色产| 里番精品3d一二三区| 亚洲免费高清| 高清一区二区三区av| 亚洲精品乱码| 亚洲特级毛片| 国产一区二区三区四区大秀| 亚洲视频电影在线| re久久精品视频| 成人日韩av| 久久精品99国产精品日本| 欧美日韩国产探花| 日韩国产一区二区三区| 日韩中出av| 9色国产精品| 久久蜜桃精品| 国产a亚洲精品| 国产欧美69| 视频一区视频二区中文字幕| 精品免费av在线| 欧美激情aⅴ一区二区三区| 亚洲18在线| 国产精品女主播一区二区三区| 国产精品13p| 国产精品麻豆成人av电影艾秋| 亚洲欧美日本日韩| 蜜桃一区二区三区| 成人日韩在线| 国产精品一区二区三区四区在线观看 | 色偷偷偷在线视频播放| 欧美精品中文| 亚洲精品日韩久久| 久久国产精品毛片| 久久久国产精品一区二区中文| 精品久久不卡| 欧美xxxx性| 国内一区二区三区| 久久精品午夜| 国产精选在线| 国产精品字幕| 成人午夜网址| 国产suv精品一区二区四区视频| 久久精品理论片| 国产精品久久久久蜜臀| av综合电影网站| 亚洲天堂资源| 91精品国产乱码久久久久久久| 久久久久久久久久久9不雅视频| 成人久久一区| 亚洲精品国产偷自在线观看| 黄色不卡一区| 亚洲激情中文在线| 热久久久久久久| 亚洲精品系列| 国产日韩欧美高清免费| 日本少妇一区二区| 国产精品久久久网站| 精品99在线| 国产91精品对白在线播放| 91精品1区| 亚洲免费毛片| 国产精品一区二区美女视频免费看| 国产欧美一级| 国产成人精品一区二区三区视频| 国产成人调教视频在线观看| 日韩毛片视频| 欧美日韩国产精品一区二区亚洲| 黑丝一区二区| 日韩精品一区二区三区中文字幕| 国产欧美日韩精品一区二区免费| 日韩精品三级| 久久久久久久久成人| 粉嫩av一区二区三区四区五区| 波多野结衣久久精品| 一区在线免费观看| 亚洲精品欧美| 国产在线看片免费视频在线观看| 欧美理论视频| 日本三级亚洲精品| 亚洲风情在线资源| 日韩中文字幕1| 国产劲爆久久| 欧美福利专区| 青青草精品视频| 老牛影视精品| 国产一级一区二区| 国产亚洲高清在线观看| 国产色播av在线| 亚洲一区导航| 国产一区精品福利| 蜜臀av国产精品久久久久| 美腿丝袜在线亚洲一区| 久久久蜜桃一区二区人| 亚洲日本国产| 日韩精品91| 7777精品| 久久久久国产精品一区三寸| 日韩精品a在线观看91| 免费福利视频一区二区三区| 亚洲欧洲av| 日韩免费高清| 亚洲1区在线观看| 99精品一区| 国产精品v日韩精品v欧美精品网站| 久久久久久免费视频| 亚洲不卡视频| av在线日韩| 欧美激情aⅴ一区二区三区| 欧洲激情综合| 久久不见久久见中文字幕免费 | 日本不卡免费高清视频在线| 日本伊人久久| 色爱综合av| 国产日韩亚洲| 国产综合亚洲精品一区二| 国产精品美女久久久久久不卡| 99国产精品视频免费观看一公开 | 色88888久久久久久影院| 日韩激情av在线| 精品一区亚洲| 免费一级欧美片在线观看网站| 亚洲激情不卡| 日韩国产一区| 国产精品久一| 鲁大师影院一区二区三区| 夜鲁夜鲁夜鲁视频在线播放| 日本少妇一区二区| 久久av一区| 久久要要av| 成人综合一区| 国产精品久久久久久久久久白浆 | 亚洲色诱最新| 日本国产精品| 高清在线一区| 麻豆精品视频在线| 国产欧美自拍| 青青国产精品| 国产精品日韩久久久| 精品一区欧美| 久久精品电影| 欧美激情另类| 欧美国产亚洲精品| 国产欧美一区二区三区精品观看 | 国产亚洲精品精品国产亚洲综合| 亚洲视频国产| 热久久国产精品| 免费久久99精品国产自在现线| 国产一区日韩| 成人午夜亚洲|