成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

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

SpringBoot整合MyBatis超詳細教程

瀏覽:145日期: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
相關文章:
成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
乱一区二区av| 午夜精品成人在线视频| 国产亚洲精品bt天堂精选| 天堂va蜜桃一区二区三区 | 在线观看日韩av电影| 日韩免费电影一区| 亚洲观看高清完整版在线观看| 狠狠噜噜久久| 久久久精品日韩欧美| 粉嫩aⅴ一区二区三区四区五区| 久久综合图片| 日韩一区二区三区观看| 人人超碰91尤物精品国产| 夜夜精品视频| 国产精品久久久久久久久晋中| 成人黄色大片在线观看| 欧美男女性生活在线直播观看| 视频在线在亚洲| 国产精品五区| 亚洲色图制服丝袜| 午夜亚洲福利| 精品美女一区二区| 国产成人一区二区精品非洲| 欧美视频完全免费看| 日韩精彩视频在线观看| 久久精品一区| 亚洲综合av网| 成人一区二区三区视频在线观看| 欧美熟乱第一页| 日本不卡高清视频| 久久综合婷婷| 蜜桃久久久久久| 欧洲精品一区二区| 热久久免费视频| 久久久青草婷婷精品综合日韩| 亚洲综合免费观看高清完整版| 亚洲国产一区二区三区高清| 成人欧美一区二区三区视频网页| 国产成人在线色| 欧美一区二区免费视频| 国产伦精品一区二区三区免费迷| 欧美吻胸吃奶大尺度电影| 久久精品国产999大香线蕉| 欧洲亚洲精品在线| 激情欧美一区二区| 欧美精品日韩精品| 韩国一区二区三区| 制服丝袜av成人在线看| 国产剧情av麻豆香蕉精品| 欧美精品在线观看播放| 国产精品88av| 欧美一区二区三区思思人| 国产 欧美在线| 欧美成人猛片aaaaaaa| 99久久亚洲一区二区三区青草| 亚洲精品在线电影| 午夜欧美精品| 亚洲乱码国产乱码精品精可以看| 国产欧美韩日| 亚洲国产精品一区二区www在线| 久久国产精品亚洲77777| 日韩精品电影一区亚洲| 国产亚洲欧美一区二区| 亚洲综合图片区| 久久久久久久久一区二区| 日本v片在线高清不卡在线观看| 91国偷自产一区二区三区成为亚洲经典 | 亚洲视频小说图片| 日韩亚洲视频| 天天综合色天天综合| 欧美日韩视频在线观看一区二区三区| 国产乱码精品一区二区三区av| 日韩视频在线你懂得| 成人国产精品免费观看动漫 | 国产精品一区2区| 欧美岛国在线观看| 亚洲午夜精品久久久久久浪潮 | 一区二区三区在线观看欧美| 久久久久欧美精品| 国产一区二区三区在线观看精品 | 亚洲国产三级在线| 欧美日韩国产免费| 91免费版在线| 午夜国产不卡在线观看视频| 欧美日韩国产美女| 欧美日韩一区二区视频在线观看| 性久久久久久久| 欧美xxxxxxxx| 亚洲欧美日韩精品综合在线观看| 顶级嫩模精品视频在线看| 一区二区三区美女| 欧美xxxx在线观看| 亚洲一区二区在线看| 国产激情精品久久久第一区二区| 亚洲欧美在线视频观看| 欧美视频一区在线观看| 狠狠色噜噜狠狠狠狠色吗综合| 精品中文字幕一区二区小辣椒| 国产精品久久久久久久裸模| 色欧美乱欧美15图片| 欧美国产视频在线观看| 六月丁香婷婷久久| 国产精品久久久久久久久搜平片| 欧美日韩视频在线第一区| 怡红院精品视频在线观看极品| 久久99深爱久久99精品| 国产精品卡一卡二卡三| 久久精品成人| av亚洲产国偷v产偷v自拍| 亚洲一二三区在线观看| 在线国产电影不卡| www.av亚洲| 日韩精品欧美成人高清一区二区| 日韩一区二区三区免费观看| 韩日视频一区| 免费观看在线综合色| 日韩欧美第一区| 国产在线不卡| 国产成人精品亚洲日本在线桃色 | 久久综合久久综合亚洲| 亚洲精选成人| 国模套图日韩精品一区二区| 国产性做久久久久久| 国产欧美日韩在线播放 | 一本一本大道香蕉久在线精品| 99re66热这里只有精品3直播 | 国产高清不卡一区二区| 欧美专区一区二区三区| 男人的天堂亚洲一区| 美女脱光内衣内裤视频久久影院| 国产精品午夜在线| 国内精品国语自产拍在线观看| 久久这里只有精品6| 一本色道久久99精品综合| 国产精品成人在线观看| 日韩精品久久理论片| 色噜噜狠狠成人网p站| 欧美阿v一级看视频| 国产成人一区在线| 一个色妞综合视频在线观看| 久久久久99精品国产片| aa国产精品| 欧美成人综合| 日韩成人免费电影| 亚洲精品视频免费观看| 欧美另类久久久品| 色婷婷精品大视频在线蜜桃视频| 92国产精品观看| 国产福利精品一区二区| 亚洲激情自拍视频| 国产精品不卡在线| 色婷婷综合激情| 国产农村妇女精品一区二区 | av一区二区三区| 日韩精品一级二级| 日本一二三不卡| 欧美日韩一区中文字幕| 激情久久五月| 韩国欧美国产一区| 日韩黄色一级片| 欧美韩国日本不卡| 4438亚洲最大| 免费精品视频| 午夜精品久久久久99热蜜桃导演| 五月婷婷激情综合| 亚洲精品伦理在线| 精品裸体舞一区二区三区| 久久综合伊人| 一区一区视频| 国产精品亚洲午夜一区二区三区| 一区二区三区免费看视频| 精品日韩av一区二区| 欧美一区二区网站| 久久天天综合| 日本一区二区三区高清不卡| 在线不卡a资源高清| 久久久久久精| 国产亚洲一级| 91丨九色丨蝌蚪富婆spa| 91尤物视频在线观看| 国产美女在线精品| 青青草一区二区三区| 亚洲图片欧美综合| 国产精品久久久久久久蜜臀| 日韩一区二区在线观看视频播放| 久久综合狠狠| 欧美最猛性xxxxx直播| 亚洲一区国产一区| 激情欧美丁香| 91亚洲国产成人精品一区二三| 国产一区二区三区四| 五月天丁香久久| 一区二区三区免费| 国产精品久久影院| 欧美日韩久久不卡| 色先锋aa成人| 国产精品免费一区二区三区在线观看 | 国产欧美一二三区| 精品一二三四区| 午夜久久久影院|