电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot整合MyBatis超詳細(xì)教程

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

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

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

1.1.配置模式

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

主要步驟為:

導(dǎo)入mybatis官方starter 編寫mapper接口。標(biāo)準(zhǔn)@Mapper注解 編寫sql映射文件并綁定mapper接口

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

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

①引入相關(guān)依賴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>

②編寫對應(yīng)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下創(chuàng)建對應(yīng)的mapper文件,對應(yīng)domain類,數(shù)據(jù)庫表單如下:

User類:

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

數(shù)據(jù)庫user表:

SpringBoot整合MyBatis超詳細(xì)教程

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表示當(dāng)前mapper的唯一標(biāo)識:一般使用domain的全路徑名+Mapper來命名--><mapper namespace='com.fengye.springboot_mybatis.mapper.UserMapper'> <!--id:方法表示,一般配置對應(yīng)的方法;resultType:表示該方法有返回,返回需要封裝到對應(yīng)實體的類型--> <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>

④對應(yīng)配置application.yml文件:

application.yml

server: port: 8083spring: datasource: username: root password: admin #假如時區(qū)報錯,增加時區(qū)配置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全局配置文件中的相關(guān)配置項 map-underscore-to-camel-case: true1.2.注解模式

注解模式使用

主要步驟:

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

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

下面是具體整合步驟:

①創(chuàng)建測試表單city,對應(yīng)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;}

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

@Mapper@Repositorypublic interface CityMapper { @Select('select * from city where id = #{id}') public City getCityById(Long id); /** * 使用@Options來增加除Insert語句中其它可選參數(shù),比如插入獲取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相關(guān):

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相關(guān):

@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'; }}

④對應(yīng)使用Postman接口進(jìn)行測試:

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

SpringBoot整合MyBatis超詳細(xì)教程

1.3.混合模式

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

實際項目操作步驟:

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

具體配置如下:

@SpringBootApplication//主啟動類上標(biāo)注,在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語句中其它可選參數(shù),比如插入獲取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核心技術(shù)與響應(yīng)式編程

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

SpringBootStudy

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: TYPE-C厂家|TYPE-C接口|TYPE-C防水母座|TYPE-C贴片-深圳步步精 | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | 盛源真空泵|空压机-浙江盛源空压机制造有限公司-【盛源官网】 | 航拍_专业的无人机航拍摄影门户社区网站_航拍网 | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 耳模扫描仪-定制耳机设计软件-DLP打印机-asiga打印机-fitshape「飞特西普」 | 圆周直径尺-小孔内视镜-纤维研磨刷-东莞市高腾达精密工具 | 热缩管切管机-超声波切带机-织带切带机-无纺布切布机-深圳市宸兴业科技有限公司 | 新密高铝耐火砖,轻质保温砖价格,浇注料厂家直销-郑州荣盛窑炉耐火材料有限公司 | 西门子伺服控制器维修-伺服驱动放大器-828D数控机床维修-上海涌迪 | 水性漆|墙面漆|木器家具漆|水漆涂料_晨阳水漆官网 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 等离子表面处理机-等离子表面活化机-真空等离子清洗机-深圳市东信高科自动化设备有限公司 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 镀锌角钢_槽钢_扁钢_圆钢_方矩管厂家_镀锌花纹板-海邦钢铁(天津)有限公司 | 收录网| 地脚螺栓_材质_标准-永年县德联地脚螺栓厂家 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 钣金加工厂家-钣金加工-佛山钣金厂-月汇好 | 隆众资讯-首页_大宗商品资讯_价格走势_市场行情 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 匀胶机旋涂仪-声扫显微镜-工业水浸超声-安赛斯(北京)科技有限公司 | 100_150_200_250_300_350_400公斤压力空气压缩机-舰艇航天配套厂家 | 涿州网站建设_网站设计_网站制作_做网站_固安良言多米网络公司 | 酒吧霸屏软件_酒吧霸屏系统,酒吧微上墙,夜场霸屏软件,酒吧点歌软件,酒吧互动游戏,酒吧大屏幕软件系统下载 | 风淋室生产厂家报价_传递窗|送风口|臭氧机|FFU-山东盛之源净化设备 | 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 | 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 | 华夏医界网_民营医疗产业信息平台_民营医院营销管理培训 | 企小优-企业数字化转型服务商_网络推广_网络推广公司 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 仪器仪表网 - 永久免费的b2b电子商务平台 | 口信网(kousing.com) - 行业资讯_行业展会_行业培训_行业资料 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | MES系统工业智能终端_生产管理看板/安灯/ESOP/静电监控_讯鹏科技 | (中山|佛山|江门)环氧地坪漆,停车场地板漆,车库地板漆,聚氨酯地板漆-中山永旺地坪漆厂家 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 无水硫酸铝,硫酸铝厂家-淄博双赢新材料科技有限公司 |