Spring Boot整合MyBatis
Posted 黑色的小蚂蚁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot整合MyBatis相关的知识,希望对你有一定的参考价值。
【Spring Boot】整合MyBatis
配置
server: port: 9915 # 数据库 相关设置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://mysql.ycx:3306/ycxdemo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&autoReConnect=true username: root password: 123456 # hikari连接池的参数 相关设置 hikari: # 生效超时 validationTimeout: 3000 # 定义获取连接的超时时间。最小250ms,默认30s connectionTimeout: 60000 # 定义连接空闲时间。最小10s,默认10m idleTimeout: 60000 # 定义最小的空闲连接数。推荐不设置。或与最大连接数一致;保持固定的连接数目 minimumIdle: 10 # 定义最大的连接数。默认10 maximumPoolSize: 10 # 定义连接的最大生命周期。推荐设置该属性。最小30s,默认30m maxLifeTime: 60000 # 从连接池获取到连接后,进行检查的查询语句。推荐设置该属性。默认值为none connectionTestQuery: select 1 # MyBatis配置 mybatis: # 搜索指定包别名 type-aliases-package: ycx.mybatis.entity # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapper-locations: classpath*:mapper/*Mapper.xml
起始
package ycx.mybatis; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import ycx.mybatis.entity.Blog; @MapperScan("ycx.mybatis.mapper") @SpringBootApplication public class MybatisDemoApplication { public static void main(String[] args) { SpringApplication.run(MybatisDemoApplication.class, args); } }
配置扫描映射 @MapperScan("ycx.mybatis.mapper")
实体
package ycx.mybatis.entity; import lombok.Data; import org.apache.ibatis.type.Alias; @Alias("Blog") @Data public class Blog { private String id; private String title; private String content; public String toString() { return "[id=" + id + ",title=" + title + ",content=" + content + "]"; } }
映射
package ycx.mybatis.mapper; import org.springframework.stereotype.Repository; import ycx.mybatis.entity.Blog; @Repository public interface BlogMapper { // @Select("select * from Blog where id = #{id}") Blog selectBlog(String id); }
<?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="ycx.mybatis.mapper.BlogMapper"> <select id="selectBlog" resultType="ycx.mybatis.entity.Blog"> select * from Blog where id = #{id} </select> </mapper>
控制器
package ycx.mybatis.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import ycx.mybatis.entity.Blog; import ycx.mybatis.service.BlogService; @RestController public class BlogController { @Autowired BlogService blogService; @RequestMapping("/getBlog") public Blog getBlog(String id) { return blogService.getBlog(id); } }
服务
package ycx.mybatis.service; import ycx.mybatis.entity.Blog; public interface BlogService { Blog getBlog(String id); } package ycx.mybatis.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import ycx.mybatis.entity.Blog; import ycx.mybatis.mapper.BlogMapper; import ycx.mybatis.service.BlogService; @Service public class BlogServiceImpl implements BlogService { @Autowired BlogMapper blogMapper; @Override public Blog getBlog(String id) { return blogMapper.selectBlog(id); } }
以上是关于Spring Boot整合MyBatis的主要内容,如果未能解决你的问题,请参考以下文章