21.1 需求说明/图解
将 Spring Boot 和 MyBatis 整合 查询出一条数据
21.2 综合案例
21.2.1 代码+配置实现
21.2.1.1 创建数据库和表
CREATE DATABASE ` springboot_mybatis`
USE ` springboot_mybatis`
CREATE TABLE ` monster` (
` id` INT NOT NULL AUTO_INCREMENT ,
` age` INT NOT NULL ,
` birthday` DATE DEFAULT NULL ,
` email` VARCHAR ( 255 ) DEFAULT NULL ,
` gender` CHAR ( 1 ) DEFAULT NULL ,
` name` VARCHAR ( 255 ) DEFAULT NULL ,
` salary` DOUBLE NOT NULL ,
PRIMARY KEY ( ` id` )
) CHARSET = utf8
SELECT * FROM ` monster`
INSERT INTO monster VALUES ( NULL , 20 , '2000-11-11' , 'nmw@sohu.com' , '男' , '牛魔王' , 5000.88 ) ;
INSERT INTO monster VALUES ( NULL , 10 , '2011-11-11' , 'bgj@sohu.com' , '女' , '白骨精' , 8000.88 ) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
21.2.1.2 创建 springboot-mybatis 项目,使用灵活的方式创建 maven
< project xmlns = " http://maven.apache.org/POM/4.0.0"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0 modelVersion>
< groupId> com.xjs groupId>
< artifactId> springboot-mybatis artifactId>
< version> 1.0-SNAPSHOT version>
< parent>
< artifactId> spring-boot-starter-parent artifactId>
< groupId> org.springframework.boot groupId>
< version> 2.5.3 version>
parent>
< dependencies>
< dependency>
< groupId> org.springframework.boot groupId>
< artifactId> spring-boot-starter-web artifactId>
dependency>
< dependency>
< groupId> org.mybatis.spring.boot groupId>
< artifactId> mybatis-spring-boot-starter artifactId>
< version> 2.2.2 version>
dependency>
< dependency>
< groupId> mysql groupId>
< artifactId> mysql-connector-java artifactId>
dependency>
< dependency>
< groupId> org.springframework.boot groupId>
< artifactId> spring-boot-configuration-processor artifactId>
dependency>
< dependency>
< groupId> org.projectlombok groupId>
< artifactId> lombok artifactId>
dependency>
< dependency>
< groupId> org.springframework.boot groupId>
< artifactId> spring-boot-starter-test artifactId>
dependency>
dependencies>
project>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
21.2.1.3 创建 resources/application.yml , 配置数据源参数,并完成 Spring Boot 项目启动测试
server :
port : 9090
spring :
datasource :
driver-class-name : com.mysql.cj.jdbc.Driver
url : jdbc: mysql: //localhost: 3306/springboot_mybatis? useSSL=true&useUnicode=true&characterEncoding=UTF- 8
username : root
password : hsp
21.2.1.4 切换数据源为 druid
修改 pom.xml (引入 druid依赖 ) 并加入配置文件 com/xjs/springboot/mybatis/config/DruidDataSourceConfig.java 完成测试
< dependency>
< groupId> com.alibaba groupId>
< artifactId> druid artifactId>
< version> 1.1.17 version>
dependency>
package com. xjs. springboot. mybatis. config ;
import com. alibaba. druid. pool. DruidDataSource ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import javax. sql. DataSource ;
@Configuration
public class DruidDataSourceConfig {
@ConfigurationProperties ( "spring.datasource" )
@Bean
public DataSource dataSource ( ) {
DruidDataSource druidDataSource = new DruidDataSource ( ) ;
return druidDataSource;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
21.2.1.5 bean
创建 com/xjs/springboot/mybatis/bean/Monster.java 回顾一下 mybatis 和 ssm项目整合
package com. xjs. springboot. mybatis. bean ;
import lombok. Data ;
import java. util. Date ;
@Data
public class Monster {
private Integer id;
private Integer age;
private Date birthday;
private String email;
private String name;
private String gender;
private Double salary;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
21.2.1.6 mapper 接口
创建 com/xjs/springboot/mybatis/mapper/MonsterMapper.java
package com. xjs. springboot. mybatis. mapper ;
import com. xjs. springboot. mybatis. bean. Monster ;
import org. apache. ibatis. annotations. Mapper ;
@Mapper
public interface MonsterMapper {
public Monster getMonsterById ( Integer id) ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
21.2.1.7 Mapper.xml 文件
创建 src/main/resources/mapper/MonsterMapper.xml 文件模板从 mybatis 官方文档拷贝
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.xjs.springboot.mybatis.mapper.MonsterMapper" >
< select id = " getMonsterById" resultType = " Monster" >
SELECT * FROM `monster` WHERE id=#{id}
select>
mapper>
21.2.1.8 创建 service 层
创建 com/xjs/springboot/mybatis/service/MonsterService.java
package com. xjs. springboot. mybatis. service ;
import com. xjs. springboot. mybatis. bean. Monster ;
public interface MonsterService {
public Monster getMonsterById ( Integer id) ;
}
创建 com/xjs/springboot/mybatis/service/impl/MonsterServiceImpl.java
package com. xjs. springboot. mybatis. service. impl ;
import com. xjs. springboot. mybatis. bean. Monster ;
import com. xjs. springboot. mybatis. mapper. MonsterMapper ;
import com. xjs. springboot. mybatis. service. MonsterService ;
import org. springframework. stereotype. Service ;
import javax. annotation. Resource ;
@Service
public class MonsterServiceImpl implements MonsterService {
@Resource
private MonsterMapper monsterMapper;
@Override
public Monster getMonsterById ( Integer id) {
return monsterMapper. getMonsterById ( id) ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
21.2.1.9 Controller
创建 com/xjs/springboot/mybatis/controller/MonsterController.java
package com. xjs. springboot. mybatis. controller ;
import com. xjs. springboot. mybatis. bean. Monster ;
import com. xjs. springboot. mybatis. service. MonsterService ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RequestParam ;
import org. springframework. web. bind. annotation. ResponseBody ;
import javax. annotation. Resource ;
@Controller
public class MonsterController {
@Resource
private MonsterService monsterService;
@GetMapping ( "/monster" )
@ResponseBody
public Monster getMonsterById ( @RequestParam ( value = "id" ) Integer id) {
Monster monster = monsterService. getMonsterById ( id) ;
return monster;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
21.2.1. 10 修改 resources/application.yml ,指定 mybatis 的配置参数
server :
port : 9090
spring :
datasource :
driver-class-name : com.mysql.cj.jdbc.Driver
url : jdbc: mysql: //localhost: 3306/springboot_mybatis? useSSL=true&useUnicode=true&characterEncoding=UTF- 8
username : root
password : hsp
mybatis :
mapper-locations : classpath: mapper/*.xml
type-aliases-package : com.xjs.springboot.mybatis.bean
configuration :
log-impl : org.apache.ibatis.logging.stdout.StdOutImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
21.2.2 测试页面效果
完成测试 浏览器: http://localhost:9090/monster?id=1
21.3 注意事项和细节说明(解决时区问题)
spring boot 整合 mybatis 取出的日期,出现 8 小时时差 解决方案
package com. xjs. springboot. mybatis. bean ;
import com. fasterxml. jackson. annotation. JsonFormat ;
import lombok. Data ;
import java. util. Date ;
@Data
public class Monster {
private Integer id;
private Integer age;
@JsonFormat ( pattern = "yyyy-MM-dd HH:mm:ss" , timezone = "GMT+8" )
private Date birthday;
private String email;
private String name;
private String gender;
private Double salary;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25