【java框架】SpringBoot(7) -- SpringBoot整合MyBatis
2021-05-30 02:02
标签:domain img pac hid width dbf connector 响应式 open 前面一篇提到了SpringBoot整合基础的数据源JDBC、Druid操作,实际项目中更常用的还是MyBatis框架,而SpringBoot整合MyBatis进行CRUD也非常方便。 下面从配置模式、注解模式、混合模式三个方面进行说明MyBatis与SpringBoot的整合。 MyBatis配置模式是指使用mybatis配置文件的方式与SpringBoot进行整合,相对应的就有mybatis-config.xml(用于配置驼峰命名,也可以省略这个文件)、XxxMapper.xml文件。 主要步骤为: 下面是具体整合配置步骤: ①引入相关依赖pom.xml配置: pom.xml ②编写对应Mapper接口: ③在resources下创建对应的mapper文件,对应domain类,数据库表单如下: User类: 数据库user表: UserMapper.xml文件: ④对应配置application.yml文件: application.yml 注解模式使用 主要步骤: 可以看到注解模式比配置模式少了编写Mapper.xml文件,简化了简单SQL语句的xml文件编写。 下面是具体整合步骤: ①创建测试表单city,对应domain类: 建表sql: City类: ②导入pom.xml与配置模式相同,编写注解式CityMapper接口: ③编写Service层、Controller层: Service相关: Controller相关: ④对应使用Postman接口进行测试: 简单模拟接口POST/GET请求即可: 在实际项目开发中涉及很多复杂业务及连表查询SQL,可以配合使用注解与配置模式,达到最佳实践的目的。 实际项目操作步骤: 具体配置如下: 本博客参考写作文档: SpringBoot2核心技术与响应式编程 博客涉及代码示例均已上传至github地址: SpringBootStudy 【java框架】SpringBoot(7) -- SpringBoot整合MyBatis 标签:domain img pac hid width dbf connector 响应式 open 原文地址:https://www.cnblogs.com/yif0118/p/14758181.html1.整合MyBatis操作
1.1.配置模式
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>org.mybatis.spring.bootgroupId>
artifactId>mybatis-spring-boot-starterartifactId>
version>2.1.4version>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
scope>runtimescope>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-devtoolsartifactId>
scope>runtimescope>
optional>trueoptional>
dependency>
dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
optional>trueoptional>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-testartifactId>
scope>testscope>
dependency>
dependencies>
build>
plugins>
plugin>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-maven-pluginartifactId>
configuration>
excludes>
exclude>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
@Mapper //这个注解表示了这个类是一个mybatis的mapper接口类
@Repository
public interface UserMapper {
//@Select("select * from user")
List
@Data
public class User {
private Integer id;
private String username;
private String password;
}
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="com.fengye.springboot_mybatis.mapper.UserMapper">
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>
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.注解模式
CREATE TABLE city
(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(30),
state VARCHAR(30),
country VARCHAR(30)
);
@Data
public class City {
private Long id;
private String name;
private String state;
private String country;
}
@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);
}
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);
}
}
@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";
}
}
1.3.混合模式
@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);
}
文章标题:【java框架】SpringBoot(7) -- SpringBoot整合MyBatis
文章链接:http://soscw.com/index.php/essay/89352.html