0% found this document useful (0 votes)
13 views

springboot整合mybatis

This document provides steps to integrate Mybatis with SpringBoot. It describes importing dependencies, configuring application.yml, creating DAO interfaces, adding mapper annotations, and testing with a sample controller. Dependencies include MySQL, Druid, Mybatis, and logging libraries. The application.yml configures Druid and Mybatis. DAO interfaces with XML mappers are created. A controller tests database queries.

Uploaded by

dabuliuxi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

springboot整合mybatis

This document provides steps to integrate Mybatis with SpringBoot. It describes importing dependencies, configuring application.yml, creating DAO interfaces, adding mapper annotations, and testing with a sample controller. Dependencies include MySQL, Druid, Mybatis, and logging libraries. The application.yml configures Druid and Mybatis. DAO interfaces with XML mappers are created. A controller tests database queries.

Uploaded by

dabuliuxi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SpringBoot整合Mybatis

前置环境
数据库:mysql
数据库连接池:druid

整合步骤如下:
1. pom.xml导入依赖
2. 配置applictaion.yml
3. 创建dao接口
4. 启动类加注解 @MapperScan
5. 创建Controller测试

step1:pom.xml导入依赖

<dependency>
<!-- 添加该jar后springboot默认启用连接池 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>

step2:配置applictaion.yml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/shopping?
characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: fy2019
password: Qinfo20180507@
max-active: 20
max-wait: 6000
min-idle: 1
test-on-borrow: true
test-on-return: true
mybatis:
mapper-locations: classpath*:com/dfrt/dao/*Mapper.xml
config-location: classpath:mybatis-config.xml

mybatis-config.xml内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置Log4j日志-->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>

<typeAliases>
</typeAliases>
</configuration>

step3:创建dao接口
com.dfrt.dao包下创建接口

1 package com.dfrt.dao;

2
3 import java.util.Map;

5 /**
6 * @Author guoyangy
7 * @Date 2023/7/12 8:45
8 * @Description todo
9 * @Version 1.0
10 */
11 public interface IOrderDao {
12 Map<String,Object> find();
13 }

14

设置mapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/myba

4 <mapper namespace="com.dfrt.dao.IOrderDao">

7 <select id="find" parameterType="java.lang.String" resultType="java.util.Map">


8 select * from stwb_data_settled limit 1;
9 </select>

10

11 </mapper>

step4:启动类加注解 @MapperScan

step5:在resources下创建log4j.properties
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

step6: 配置连接数据库url
url: jdbc:mysql://localhost:3306/shopping?
characterEncoding=utf8&serverTimezone=Asia/Shanghai

step7:测试
@RestController
public class HelloControoler {
@Autowired
ILoginDao loginDao;
@RequestMapping(value = "/user/login")
public Admin login(String username,String password){

return loginDao.findAdminByUsernameAndPassword(username,password);
}
}

application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/shopping?
characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=6000
spring.datasource.druid.min-idle=1
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-on-return=true

You might also like