Spring boot + Mybatis配置(IDEA)
程序员文章站
2022-04-23 12:04:32
...
目录
一、关于本文
本文可以快速在Spring boot项目中集成Mybatis,完成相关配置和类代码。
二、需要加入的配置和类
- application.yml
- pom.xml
- mybatis-config.xml
- pojo类
- mapper接口
- mapper.xml
- 测试类
三、加入配置和类
3.1 application.yml
#配置数据库信息
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.4.227.237:3306/ccpaccount_qa
username: test-001
password: test-001
type: org.apache.commons.dbcp.BasicDataSource
#配置mybatis mapper.xml
mybatis:
config-location:
classpath: mybatis-config.xml
mapper-locations:
classpath: mapper/*Mapper.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#mybatis-pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
3.2 pom.xml
<!-- springboot和mybatis集成中间件 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons.dbcp.version}</version>
</dependency>
<!-- 数据库连接的jdbc依赖 已经在mybatis-spring集成包中包含了-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.starter.jdbc.version}</version>
</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
3.3 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>
<settings>
<setting name="cacheEnabled" value="false"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 是否使用插入数据后自增主键的值,需要配合keyProperty使用 -->
<setting name="useGeneratedKeys" value="true"/>
<setting name="callSettersOnNulls" value="true"/>
</settings>
<!--<typeAliases>
<typeAlias type="org.maggiezhang.springbootdemo.domain.IntegerEnumTypeHandler" alias="IntegerEnumTypeHandler" />
</typeAliases>-->
</configuration>
3.4 pojo类
package org.maggiezhang.springbootdemo.domain;
import lombok.Data;
/**
* @auther: maggie zhang
* @date: 2018/9/27 14:07
*/
@Data
public class CouponPO {
/**
* 唯一标识
*/
private long id;
/**
* 息费券类型,券配置信息
*/
private CouponType couponType;
/**
* 免息天数,券配置信息
*/
private Integer canFreeInterestDays;
/**
* 绑定的借据id
*/
private Long loanBillId;
/**
* 作用的期数单id
*/
private Long periodBillId;
/**
* 免息券是否使用
*/
private Boolean isUsed;
/**
* 券可免金额
*/
private Money canFreeInterestAmount;
/**
* 券实际已免金额
*/
private Money freedInterestAmount;
/**
* 券实际免息天数
*/
private Integer freedInterestDays;
}
3.5 mapper接口
package org.maggiezhang.springbootdemo.dao.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.maggiezhang.springbootdemo.domain.CouponPO;
@Mapper
public interface CouponMapper {
CouponPO queryByLoanBillId(long loanBillId);
}
3.6 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="org.maggiezhang.springbootdemo.dao.mapper.CouponMapper">
<resultMap id="ResultMap" type="org.maggiezhang.springbootdemo.domain">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="coupon_type" property="couponType" typeHandler="IntegerEnumTypeHandler" />
<result column="can_free_interest_days" property="canFreeInterestDays" jdbcType="INTEGER" />
<result column="loan_bill_id" property="loanBillId" jdbcType="BIGINT" />
<result column="period_bill_id" property="periodBillId" jdbcType="BIGINT" />
<result column="is_used" property="isUsed" jdbcType="BOOLEAN" />
<result column="can_free_interest_amount" property="canFreeInterestAmount" typeHandler="MoneyTypeHandler"/>
<result column="freed_interest_amount" property="freedInterestAmount" typeHandler="MoneyTypeHandler" />
<result column="freed_interest_days" property="freedInterestDays" jdbcType="INTEGER" />
</resultMap>
<sql id="BaseColumns">
id, coupon_type, can_free_interest_days, loan_bill_id, period_bill_id, is_used, can_free_interest_amount, freed_interest_amount, freed_interest_days
</sql>
<select id="queryByLoanBillId" resultMap="ResultMap">
SELECT
<include refid="BaseColumns"/>
FROM
coupon
WHERE loan_bill_id = #{loanBillId}
</select>
</mapper>
3.7 测试类
package org.maggiezhang.springbootdemo.dao.mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.maggiezhang.springbootdemo.StartApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
import javax.sql.DataSource;
@SpringBootTest(classes = StartApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestDataSource {
@Resource
private DataSource dataSource;
@Autowired
private CouponMapper couponMapper;
@Test
public void testConnection(){
System.out.println(this.dataSource);
}
@Test
public void testMapper(){
System.out.println(couponMapper);
//couponMapper.queryByLoanBillId(1l);
}
}
推荐阅读
-
Spring Boot整合mybatis(一)实例代码
-
spring Boot与Mybatis整合优化详解
-
详解Spring Boot中MyBatis的使用方法
-
spring boot devtools在Idea中实现热部署方法
-
Spring Boot 配置 IDEA和DevTools 热部署的方法
-
Spring Boot + Vue 前后端分离开发之前端网络请求封装与配置
-
Spring Boot + Kotlin整合MyBatis的方法教程
-
Spring Boot配置AOP打印日志的全过程
-
Spring Boot集成Mybatis的实例代码(简洁版)
-
Spring Boot+Mybatis的整合过程