1、【microboot项目】修改配置文件,引入所需要的相关依赖库:
ext.versions = [ // 定义所有要使用的版本号springboot : '2.4.3', // SpringBoot版本号junit : '5.7.1', // 配置JUnit测试工具的版本编号junitPlatformLauncher : '1.7.1', // JUnit测试工具运行平台版本编号lombok : '1.18.18', // Lombok插件对应的版本号mysql : '8.0.25', // MySQL驱动的版本druid : '1.2.6', // Druid版本mybatisSpringBoot : '2.2.0', // Mybatis-SpringBoot整合依赖
]
ext.libraries = [ // 定义所有的依赖库'spring-boot-gradle-plugin': "org.springframework.boot:spring-boot-gradle-plugin:${versions.springboot}",// 以下的配置为与项目用例测试有关的依赖'junit-jupiter-api': "org.junit.jupiter:junit-jupiter-api:${versions.junit}",'junit-vintage-engine': "org.junit.vintage:junit-vintage-engine:${versions.junit}",'junit-jupiter-engine': "org.junit.jupiter:junit-jupiter-engine:${versions.junit}",'junit-platform-launcher': "org.junit.platform:junit-platform-launcher:${versions.junitPlatformLauncher}",'junit-bom': "org.junit:junit-bom:${versions.junit}",// 以下的配置为Lombok组件有关的依赖'lombok': "org.projectlombok:lombok:${versions.lombok}",// 以下的配置为数据库开发所需要的依赖:'mysql-connector-java': "mysql:mysql-connector-java:${versions.mysql}",'druid-spring-boot-starter': "com.alibaba:druid-spring-boot-starter:${versions.druid}",'spring-jdbc': "org.springframework:spring-jdbc:${versions.springJdbc}",'druid': "com.alibaba:druid:${versions.druid}",// 以下的配置为MyBatis开发框架所需要的依赖:'mybatis-spring-boot-starter': "::${versions.mybatisSpringBoot}",
]
project('microboot-database') { // 子模块dependencies { // 配置子模块依赖compile(libraries.'mysql-connector-java')compile(libraries.'druid') // 添加原始依赖compile(libraries.'mybatis-spring-boot-starter')}
}
2、【microboot-database子模块】在“src/main/resources”目录之中创建“META-INF/mybatis/mybatis.cfg xmI”文件;
<?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><!-- Mybatis环境的属性配置 --><settings> <!-- 开启二级缓存 --><setting name="cacheEnabled" value="true"/></settings>
</configuration>
3、【microboot-database子模块】application.yml
spring:datasource:type: com.alibaba.druid.pool.DruidDataSource # 配置数据源的类型driver-class-name: com.mysql.cj.jdbc.Driver # MySQL8之后的驱动url: jdbc:mysql://localhost:3306/yootk # 数据库的JDBC连接地址username: root # 用户名password: mysqladmin # 密码druid: # 定义数据库连接池所需要的相关配置项initial-size: 5 # 初始化连接池大小min-idle: 10 # 最小维持的连接池大小max-active: 50 # 最大可提供的连接数量max-wait: 60000 # 最长等待时间time-between-eviction-runs-millis: 6000 # 关闭空闲连接间隔(毫秒)min-evictable-idle-time-millis: 30000 # 连接最小的生存时间validation-query: SELECT 1 FROM dual # 数据库的状态检测test-while-idle: true # 判断连接是否可用test-on-borrow: false # 在获得连接之前是否要进行测试test-on-return: false # 归还连接前是否需要进行测试pool-prepared-statements: false # 不缓存PSTMTmax-pool-prepared-statement-per-connection-size: 20 # 配置PSTMT缓存个数mybatis:config-location: classpath:META-INF/mybatis/mybatis.cfg.xml # mybatis配置文件所在路径type-aliases-package: com.yootk.vo # 定义所有操作类的别名所在包mapper-locations: classpath:META-INF/mybatis/mapper/**/*.xml # 所有的mapper映射文件
4、【microboot-database子模块】创建IMemberDAO接口实现member数据表的配置
package com.yootk.dao;import com.yootk.vo.Member;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface IMemberDAO { // 定义DAO接口public List<Member> findAll(); // 查询全部
}
5、 【microboot-database子模块】随后创建一个与IMemberDAO接口关联的映射配置文件,该文件的路径:src/main/resources/META-INE/mybatis/mapper/MemberMapper.xml,编辑 MemberMapper.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.yootk.dao.IMemberDAO"><select id="findAll" resultType="Member">SELECT mid, name, age, salary, birthday, content FROM member;</select>
</mapper>
6、 【microboot-database子模块】IMemberService
package com.yootk.service;import com.yootk.vo.Member;import java.util.List;public interface IMemberService {public List<Member> list();
}
7、 【microboot-database子模块】MemberServiceImpl
package com.yootk.service.impl.impl;import com.yootk.dao.IMemberDAO;
import com.yootk.service.IMemberService;
import com.yootk.vo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class MemberServiceImpl implements IMemberService {@Autowiredprivate IMemberDAO memberDAO;@Overridepublic List<Member> list() {return this.memberDAO.findAll();}
}
8、 【microboot-database子模块】按照Mybatis设计来说,是可以直接进行日志信息打印的,所以修改
logback-spring.xml配置文件,追加日志输出配置项
<logger name="com.yootk.dao" level="DEBUG"/>
9、【microboot-database子模块】编写一个测试类,对当前的业务接口的功能进行测试
package com.yootk.test;import com.yootk.StartSpringBootDatabaseApplication;
import com.yootk.service.IMemberService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;@ExtendWith(SpringExtension.class) // 使用JUnit5测试工具
@WebAppConfiguration // 启动WEB运行环境
@SpringBootTest(classes = StartSpringBootDatabaseApplication.class) // 配置程序启动类
public class TestMemberService { // 编写测试类@Autowiredprivate IMemberService memberService;@Testpublic void testFindAll() {System.out.println(this.memberService.list());}
}