springboot整合mybatis

9 篇文章 0 订阅
5 篇文章 0 订阅

mybatis是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO为数据库中的记录。
今天,总结如何快速整合mybatis。
老步骤,引入依赖jar:

<!-- mybatis引入 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

<!--mysql 驱动包-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

application.yml配置文件中添加数据源相关配置:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/system?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

url——配置MySQL的连接路径(ip)localhost,端口(port)3306,所指定的数据库sytem。后面的参数说明:
useUnicode:是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true
characterEncoding:当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk
autoReconnect:当数据库连接异常中断时,是否自动重新连接
autoReconnectForPools:是否使用针对数据库连接池的重连策略
failOverReadOnly : 自动重连成功后,连接是否设置为只读
maxReconnects:autoReconnect设置为true时,重试连接的次数
initialTimeout:autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒
connectTimeout: 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK
socketTimeout: socket操作(读写)超时,单位:毫秒。 0表示永不超时
serverTimezone:设置时区
allowMultiQueries:是否允许批量sql操作,一般在xml中进行batchUpdate或batchInsert时,需要设置此参数,否则会报错。
zeroDateTimeBehavior:这个比较难解释放后面
Java连接MySQL数据库,在操作异常的timestamp类型(如日期时间为0000-00-00 00:00:00)时不能正确处理 ,而是默认抛出一个异 常,比如所见的java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column XX to TIMESTAMP
举个实际的栗子,你的方法中要传入来自页面的日期参数值 ,vue项目中的日期控件经常会传入了0000-00-00 00:00:00,并没有设置正确的数据,那么这时默认抛出java.sql.SQLException异常,如果设定这一项 zeroDateTimeBehavior = convertToNull,就是在遇到这种为0的异常日期时间类型,就把它转换为null来代替异常处理。其实,这类操作情况的处理策略有3种:
1.exception(不指定,则默认)——默认抛出异常
2.convertToNull——转化为null
3.round——替换成最近的日期即XXXX-01-01
username——用户名
password——密码
driver-class-name——连接MySQL驱动
Mapper文件:

@Mapper
public interface UserMapper {
    /**
     * 查询用户列表信息
     * @param user
     * @return
     */
    List<User> getUserList(User user);
}

ps:如果在启动类上加入扫包注解,此处的@Mapper就不用加了(一般项目中都会加入扫包注解,方便后面不用每次都加这个注解)

XML文件配置

<?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.xx.xxx.xxx.mapper.UserMapper">
    <resultMap id="UserResult" type="User">
        <id property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="password" column="password"/>
        <result property="age" column="age"/>
        <result property="gender" column="gender"/>
        <result property="isDeleted" column="is_deleted"/>
    </resultMap>

    <select id="getUserList" resultMap="UserResult">
        <include refid="selectUser"/>
        <where>
            <if test="userName != null and userName != ''"> and user_name = #{userName}</if>
            <if test="password != null and password != ''"> and password = #{password}</if>
            <if test="isDeleted != null"> and is_deleted = #{isDeleted}</if>
            <if test="age != null"> and age = #{age}</if>
            <if test="gender != null"> and gender = #{gender}</if>
        </where>
    </select>
</mapper>

namespace中已经绝对路径,映射对应的Mapper文件

启动项目,swagger中测试接口访问查询数据库:

至此,mybatis整合完毕!

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值