Spring boot集成mybatis

Spring boot集成mybatis

maven依赖

我的spring boot版本是2.5.0,集成mybatis,首先需要数据库的支持,这里我选择mysql数据库,版本是8.0.11,然后使用druid连接池,其次就需要加上mybatis的依赖。

xml 复制代码
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>

        <!--myabtis-springboot-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

yml配置

yml中配置了数据源和mybatis的配置

yml 复制代码
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    username: root
    password: 123456
# mybatis配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: org.syx.dts.entity
  configuration:
    map-underscore-to-camel-case: true

项目结构

entity包放的是定义的实体,mapper包是各mapper的接口,resources下的mapper放的是mybatis的xml

mapper注解

启动类上的MapperScan("org.syx.dts.mapper")这个注解可写可不写。主要看mapper接口有没有加@Mapper注解,加了,启动类就不需要加MapperScan,当然你加了也无所谓。

各类展示

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DailyReport {
    private Integer id;
    private Integer empTotal;
    private Integer deviceTotal;
    private Integer deviceUsedNum;
    private Integer birthdayEmpNum;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
}
java 复制代码
@Mapper
public interface DailyReportMapper {

    @Select("insert into daily_report(emp_total,device_total,device_used_num,birthday_emp_num,create_time) values(#{empTotal},#{deviceTotal},#{deviceUsedNum},#{birthdayEmpNum},NOW())")
    void save(DailyReport dailyReport);

    @Select("select * from daily_report")
    List<DailyReport> lists();
}
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="org.syx.dts.mapper.DeviceMapper">

    <insert id="batchSave">
        INSERT INTO `device` (
           name,no,description,type,create_time,status
        ) VALUES
        <!-- 使用foreach遍历列表 -->
        <foreach item="device" index="index" collection="list" separator=",">
            (#{device.name}, #{device.no}, #{device.description}, #{device.type}, NOW(),0)
        </foreach>

    </insert>

    <update id="update">
        update `device` set
        <if test="status != null and status != ''">
            status = #{status}
        </if>
        where id = #{id}

    </update>

    <select id="getDeviceList" resultType="org.syx.dts.entity.Device">
        select id,name,no,description,type,create_time,status from `device` where 1=1
        <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="flag != null ">
            AND status = #{flag}
        </if>
    </select>
</mapper>

总结

如此,Spring boot集成mybatis就完成了,整个过程是非常简单的。

相关推荐
毕设源码_钟学姐6 分钟前
计算机毕业设计springboot宿舍管理信息系统 基于Spring Boot的高校宿舍管理平台设计与实现 Spring Boot框架下的宿舍管理系统开发
spring boot·后端·课程设计
军军君0114 分钟前
基于Springboot+UniApp+Ai实现模拟面试小工具二:后端项目搭建
前端·javascript·spring boot·spring·微信小程序·前端框架·集成学习
方圆想当图灵1 小时前
ScheduledFutureTask 踩坑实录
后端
全栈凯哥1 小时前
16.Spring Boot 国际化完全指南
java·spring boot·后端
M1A11 小时前
Java集合框架深度解析:LinkedList vs ArrayList 的对决
java·后端
栈溢出了2 小时前
MyBatis实现分页查询-苍穹外卖笔记
java·笔记·mybatis
31535669132 小时前
Springboot实现一个接口加密
后端
后端小肥肠4 小时前
效率革命!10分钟用Dify+Spring Boot打造AI热点雷达,自媒体选赛道再不难!(附保姆级教程)
人工智能·spring boot·agent
Linn4 小时前
Spring WebSocket 服务实现的主流方案与最佳实践
spring boot·后端·spring
M1A14 小时前
企业信息化管理(EIM):数字化转型的核心引擎
后端