Spring Boot MyBatis

一、添加依赖
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>
二、数据源配置

在src/main/resources/application.properties中配置数据源信息:

spring.datasource.url = jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
三、配置连接池

HikariCP是目前速度最快的连接池,官网也给出了一个性能对比示意图:

性能对比示意图

HikariCP 的特点:

  • 代码量非常小
    要知道对于连接池来讲,代码越少,占用cpu和内存越少,Bug出现几率也是越小,那么代表他的执行率高,这就是为什么HikariCP受欢迎的原因之一
  • 稳定性,可靠性强
    HikariCP是经受了市场的考验,走过太上老君的丹炉,现如今拿到SpringBoot的入场证,走上了人生巅峰.
  • 速度奇快
    光有健壮性可不行,坦克很健壮,却跑不过飞机,但是HikariCP却依靠自己的代码少,重写数据结构等特点,成功晋级速度最快连接池冠军宝座.

Spring Boot 2.0 之后默认的数据库连接池由Tomcat换成HikariCP,可以在配置文件中进行配置:

## 数据库配置
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
##  Hikari 连接池配置 ------ 详细配置请访问:https://github.com/brettwooldridge/HikariCP
## 最小空闲连接数量
spring.datasource.hikari.minimum-idle=5
## 空闲连接存活最大时间,默认600000(10分钟)
spring.datasource.hikari.idle-timeout=180000
## 连接池最大连接数,默认是10
spring.datasource.hikari.maximum-pool-size=10
## 此属性控制从池返回的连接的默认自动提交行为,默认值:true
spring.datasource.hikari.auto-commit=true
## 连接池母子
spring.datasource.hikari.pool-name=MyHikariCP
## 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
spring.datasource.hikari.max-lifetime=1800000
## 数据库连接超时时间,默认30秒,即30000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
四、注解方式跟XML配置方式共同的模块编码

实体对象

public class Student {
    private String id;
    private String name;
    private String birth;
    private String sex;

    // SET和GET方法
}

Controller

@Controller
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        request.setAttribute("studentList",studentService.findAll());
        return "stu";
    }
}

Service接口

public interface StudentService {
    List<Student> findAll();
}

Service实现

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> findAll() {
        return studentMapper.findAll();
    }
}
五、注解方式

Mybatis注解的方式非常简单,只需要在定义的mapper接口上使用注解定义CRUD语句。最后给这个接口添加@Mapper注解或者在启动类上添加@MapperScan(“com.mybatis.dao”)注解都行。下面以@Mapper注解为例。

StudetMapper

//Component:注解是声明组件,往往是不明确这个组件在mvc中哪一层才宽泛的使用
//不添加也没事,只是不加service那边引入StudentMapper会有错误提示,但不影响
@Component
@Mapper
public interface StudentMapper {

    @Select("SELECT * FROM student")
	@Results({
        @Result(property = "id", column = "s_id"),
        @Result(property = "name", column = "s_name"),
        @Result(property = "birth",column = "s_birth"),
        @Result(property = "sex",column = "s_sex")
    })
    List<Student> findAll();
}
六、xml方式

创建接口Mapper(不是类)和对应的Mapper.xml文件
定义相关方法,注意方法名称要和Mapper.xml文件中的id一致,这样会自动对应上。

StudentMapper.java

public interface StudentMapper {
    List<Student> findAll();
}

修改application.properties 配置文件

#指定bean所在包
mybatis.type-aliases-package=com.wsttqs.springboot.domain
#指定映射文件
mybatis.mapperLocations=classpath:mapper/*.xml

StudentMapper.xml

在src/main/resources目录下新建一个mapper目录,在mapper目录下新建StudentMapper.xml文件。
通过mapper标签中的namespace属性指定对应的dao映射,这里指向LearnMapper。

<?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.wsttqs.springboot.dao.StudentMapper">

    <!-- type为实体类Student,包名已经配置,可以直接写类名 -->
    <resultMap id="stuMap" type="Student">
        <id property="id" column="s_id" />
        <result property="name" column="s_name" />
        <result property="birth" column="s_birth" />
        <result property="sex" column="s_sex" />
    </resultMap>

    <select id="findAll" resultMap="stuMap" resultType="list">
        SELECT * FROM STUDENT
    </select>

</mapper>
七、分页插件

这里使用物理分页插件pagehelper,使用方法比较简单。

pom.xml中添加依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>

然后你只需在查询list之前使用PageHelper.startPage(int pageNum, int pageSize)方法即可。pageNum是第几页,pageSize是每页多少条。

    @Override
    public List<Student> findAll() {
        PageHelper.startPage(1,2);
        return studentMapper.findAll();
    }

更多参数使用方法见: https://github.com/pagehelper/Mybatis-PageHelper

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值