springboot整合myBatis

官方文档: http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
Maven仓库地址: https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter

整合测试

新建一个模块springboot-06-mybatis与上一模块springboot-05-jdbc大致相同的
在这里插入图片描述
导入 MyBatis 所需要的依赖

 <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.4</version>
 </dependency>

maven配置资源过滤问题

 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
     <resource>
         <directory>src/main/java</directory>
         <includes>
             <include>**/*.yml</include>
             <include>**/*.properties</include>
             <include>**/*.xml</include>
         </includes>
         <filtering>false</filtering>
     </resource>
     <resource>
         <directory>src/main/resources</directory>
         <includes>
             <include>**/*.yml</include>
             <include>**/*.properties</include>
             <include>**/*.xml</include>
         </includes>
         <filtering>false</filtering>
     </resource>
 </resources>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.loey</groupId>
    <artifactId>springboot-06-mybatis</artifactId>
    <version>1.0.0</version>
    <name>springboot-06-mybatis</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>

        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>

        <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>

</project>

配置数据库连接信息(application.yml)

既然已经提供了 myBatis 的映射配置文件,自然要告诉 spring boot 这些文件的位置

# mybatis全局配置文件和mapper.xml文件的扫描配置
mybatis:
  type-aliases-package: com.loey.pojo
  #config-location: classpath:mybatis/mybatis-config.xml   #指定mybatis全局配置文件的路径
  mapper-locations: classpath:mapper/*.xml        #指定mapper.xml配置文件的路径
  configuration:
    map-underscore-to-camel-case: true #开启自动转换驼峰命名
spring:
  datasource:
    username: root
    password: 1127
    #?serverTimezone=UTC解决时区的报错
    url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource #自定义数据源

    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

# mybatis全局配置文件和mapper.xml文件的扫描配置
mybatis:
  type-aliases-package: com.loey.pojo
  #config-location: classpath:mybatis/mybatis-config.xml   #指定mybatis全局配置文件的路径
  mapper-locations: classpath:mapper/*.xml        #指定mapper.xml配置文件的路径
  configuration:
    map-underscore-to-camel-case: true #开启自动转换驼峰命名

创建实体类,导入 Lombok(Department)

package com.loey.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {

    private int id;
    private String departmentName;
}

创建mapper目录以及对应的 Mapper 接口 (DepartmentMapper)

package com.loey.mapper;

import com.loey.pojo.Department;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DepartmentMapper {

    List<Department> listAll();

    int add(Department department);
}

对应的Mapper映射文件(DepartmentMapper.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">
<!--namespace=绑定一个对应的mapper/Mapper接口-->
<mapper namespace="com.loey.mapper.DepartmentMapper">

    <select id="listAll" resultType="Department">
        select * from department;
    </select>

    <insert id="add" parameterType="Department">
        insert into department (id,department_name) values(#{id},#{departmentName})
    </insert>

</mapper>

编写部门的 DepartmentController 进行测试!

package com.loey.controller;

import com.loey.mapper.DepartmentMapper;
import com.loey.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName DepartmentController
 * @Author loey
 * @Date Create in 2021/2/1 20:17
 * @Version 1.0
 **/
@RestController
public class DepartmentController {

    @Autowired
    DepartmentMapper departmentMapper;

    @RequestMapping("/t1")
    public List<Department> getAll(){

        return departmentMapper.listAll();
    }

    @RequestMapping("/t2")
    public int add(){

        Department department = new Department(106, "新闻部");
        return departmentMapper.add(department);
    }
}

启动项目访问进行测试!

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值