目 录CONTENT

文章目录

SpringBoot整合Mybatis

Josue
2022-04-01 / 0 评论 / 0 点赞 / 64 阅读 / 1,636 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-05-05,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1、依赖

<!-- 整合mybatis所需的-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
 </dependency>
    
<!-- 数据库连接驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.23</version>
 </dependency>
    
<!-- 数据库连接池-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.4</version>
</dependency>

解决:maven项目运行时打包,而java目录下的xml资源在打包时会被忽略

<build>
     <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
</build>

2、yml

server:
  port: 8080

spring:
  datasource:
    username: root
    password: a123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true


mybatis:
  type-aliases-package: com.test.pojo    #别名
  mapper-locations: classpath:com/test/mapper/*.xml     #xml文件

3、*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.test.mapper.UserMapper">
    <select id="queryUser" parameterType="String" resultType="User">
        SELECT * FROM user
    </select>
</mapper>
0

评论区