濮阳做网站,专业的铁岭做网站公司,昆明铁路局建设工程网站,网站制作岗位职责#x1f600;前言 本篇博文关于Spring Boot 整合MyBatis#xff0c;希望你能够喜欢 #x1f3e0;个人主页#xff1a;晨犀主页 #x1f9d1;个人简介#xff1a;大家好#xff0c;我是晨犀#xff0c;希望我的文章可以帮助到大家#xff0c;您的满意是我的动力#x… 前言 本篇博文关于Spring Boot 整合MyBatis希望你能够喜欢 个人主页晨犀主页 个人简介大家好我是晨犀希望我的文章可以帮助到大家您的满意是我的动力 欢迎大家这里是CSDN我总结知识的地方欢迎来到我的博客感谢大家的观看 如果文章有什么需要改进的地方还请大佬不吝赐教 先在此感谢啦 文章目录 Spring Boot 整合MyBatis需求说明/图解综合案例代码配置实现测试页面效果注意事项和细节说明 Spring Boot 整合MyBatis
需求说明/图解
将Spring Boot 和MyBatis 整合查询出一条数据
综合案例
代码配置实现
创建数据库和表
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,
韩顺平Java 工程师
gender char(1) DEFAULT NULL,
name VARCHAR(255) DEFAULT NULL,
salary DOUBLE NOT NULL,
PRIMARY KEY (id)
) CHARSETutf8
SELECT * FROM monster
insert into monster values(null, 20, 2000-11-11, nmwsohu.com, 男, 牛魔王, 5000.88);
insert into monster values(null, 10, 2011-11-11, bgjsohu.com, 女, 白骨精, 8000.88);
insert into monster values(null, 20, 2020-11-11, xhysohu.com, 男, 小虎牙, 3000.88);
insert into monster values(null, 18, 2001-06-18, xhysohu.com, 女, 小狐妖, 8888.88);创建springboot_mybatis 项目-创建maven pom.xml 需要引入相关依赖. !--引入相关的依赖--dependencies!--引入web starter--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--引入mybatis starter, 如果看不到版本自己手写2.2.2--dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.2/version/dependency!--引入mysql驱动: 这里使用版本仲裁 8.0.26--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency!--引入配置处理器 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactId/dependency!--引入lombok--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency!--引入test starter--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactId/dependency!--引入druid依赖--dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.17/version/dependency/dependencies创建resources/application.yml 配置数据源参数, 并完成Spring Boot 项目启动测试
server:port: 9090
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springboot_mybatis?useSSLtrueuseUnicodetruecharacterEncodingUTF-8username: rootpassword: 123456切换数据源为druid 修改pom.xml(如果没有mybatis-stater , 加入即可.) 并加入配置文件com/my/mybatis/config/DruidDataSourceConfig.java , 完成测试 !--引入druid依赖--dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.17/version/dependency配置文件com/my/mybatis/config/DruidDataSourceConfig.java
Configuration
public class DruidDataSourceConfig {ConfigurationProperties(spring.datasource)Beanpublic DataSource dataSource() throws SQLException {DruidDataSource druidDataSource new DruidDataSource();return druidDataSource;}
}创建com/my/mybatis/bean/Monster.java
Data
public class Monster {private Integer id;private Integer age;//这里通过注解来解决时区问题//GMT 就是格林尼治标准时间JsonFormat(pattern yyyy-MM-dd HH:mm:ss, timezone GMT8)private Date birthday;private String email;private String name;private String gender;private Double salary;
}创建com/my/mybatis/mapper/MonsterMapper.java
//在Mapper接口使用 Mapper 就会扫描,并将Mapper接口对象注入
Mapper
public interface MonsterMapper {//方法,根据id返回Monster对象public Monster getMonsterById(Integer id);
}创建springboot_mybatis\src\main\resources\mapper\MonsterMapper.xml 文件模板从mybatis 官方文档拷贝
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.my.springboot.mybatis.mapper.MonsterMapper!--配置getMonsterById--select idgetMonsterById resultTypecom.my.mybatis.bean.MonsterSELECT * FROM monster WHERE id#{id}/select
/mapper8.创建com/my/mybatis/service/MonsterService.java
public interface MonsterService {//根据id返回Monster对象public Monster getMonsterById(Integer id);
}创建com/my/mybatis/service/impl/MonsterServiceImpl.java
Service
public class MonsterServiceImpl implements MonsterService {//装配MonsterMapperResourceprivate MonsterMapper monsterMapper;Overridepublic Monster getMonsterById(Integer id) {return monsterMapper.getMonsterById(id);}
}创建com/my/mybatis/controller/MonsterController.java
Controller
public class MonsterController {//装配MonsterServiceResourceprivate MonsterService monsterService;ResponseBodyGetMapping(/monster)public Monster getMonsterById(RequestParam(value id) Integer id){return monsterService.getMonsterById(id);}
}修改resources/application.yml , 指定mybatis 的配置参数
server:port: 10000
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springboot_mybatis?useSSLtrueuseUnicodetruecharacterEncodingUTF-8username: rootpassword: 123456mybatis:#指定要扫描的 Xxxmapper.xmlmapper-locations: classpath:mapper/*.xml#通过config-location 可以指定mybatis-config.xml可以以传统的方式来配置mybatis#config-location: classpath:mybatis-config.xml#我们也可以直接在application.yml进行配置#举例说明1. 比如配置原来的 typeAliases#举例说明2 配置输出底层的原生sqltype-aliases-package: com.my.springboot.mybatis.beanconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl#配置mybatis的两种方式的选择: 如果配置比较简单就直接在application.yml配置即可#如果配置内容比较多可以考虑单独的做一个mybatis-config.xml测试页面效果
完成测试, 浏览器: http://localhost:10000/monster?id1 注意事项和细节说明
spring boot 整合mybatis 取出的日期, 出现8 小时时差解决方案
JsonFormat(patternyyyy-MM-dd HH:mm:ss,timezoneGMT8)热门专栏推荐 Thymeleaf快速入门及其注意事项
Spring Initailizr–快速入门–SpringBoot的选择
带你了解SpringBoot支持的复杂参数–自定义对象参数-自动封装
Rest 优雅的url请求处理风格及注意事项 文章到这里就结束了如果有什么疑问的地方请指出诸大佬们一起来评论区一起讨论 希望能和诸大佬们一起努力今后我们一起观看感谢您的阅读 如果帮助到您不妨3连支持一下创造不易您们的支持是我的动力