SpringBoot整合MyBatis超详细教程
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接口类 @repository public 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类:
@data public class user { private integer id; private string username; private string password; }
数据库user表:
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 id="findallusers" resulttype="com.fengye.springboot_mybatis.entity.user"> select * from user </select> <insert id="insert" parametertype="com.fengye.springboot_mybatis.entity.user"> insert into user(id, username, password) values (#{id}, #{username}, #{password}) </insert> <update id="update" parametertype="com.fengye.springboot_mybatis.entity.user"> update user set username = #{username}, password = #{password} where id = #{id} </update> <delete id="deletebyid" parametertype="integer"> delete from user where id = #{id} </delete> </mapper>
④对应配置application.yml文件:
application.yml
server: port: 8083 spring: 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.driver mybatis: #config-location: classpath:mybatis/mybatis-config.xml 使用了configuration注解则无需再指定mybatis-config.xml文件 mapper-locations: classpath:mybatis/mapper/*.xml configuration: #指定mybatis全局配置文件中的相关配置项 map-underscore-to-camel-case: true
1.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类:
@data public class city { private long id; private string name; private string state; private string country; }
②导入pom.xml与配置模式相同,编写注解式citymapper接口:
@mapper @repository public 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); } @service public 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请求即可:
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); } } @repository public 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); }
本博客参考写作文档:
博客涉及代码示例均已上传至github地址:
到此这篇关于springboot整合mybatis超详细教程的文章就介绍到这了,更多相关springboot整合mybatis内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
Photoshop绘制超质感的相机镜头的详细方法(图文教程)
-
一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程
-
axios封装,使用拦截器统一处理接口,超详细的教程(推荐)
-
Spring + Spring Boot + MyBatis + MongoDB的整合教程
-
SpringBoot + Mybatis 增删改查实例教程详细解读
-
使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文)
-
SpringBoot整合MybatisPlus的简单教程实现(简单整合)
-
spring+springmvc+mybatis+maven入门实战(超详细教程)
-
springboot+springmvc+mybatis项目整合
-
IDEA搭建Springboot+SpringMVC+Mybatis+Mysql(详细、易懂)