Java测试开发平台搭建(六)持久化之mybatis配置
1. 前置-搭建本地mysql环境
这个文章写的很细,按照步骤来没毛病
Mac安装及配置MySql及图形化工具MySQLworkbench安装_mysql workbench mac-CSDN博客
2. mybatis依赖
pom.xml添加:
具体代码见文末git
3. 添加配置
具体代码见文末git
4. 添加mybatis配置
application-dev.yml及其他环境的配置
server:
port: 8080 #启动端口号
connection-timeout: PT18000000S #链接超时
servlet:
session:
timeout: 30m #30分钟,测试的话时间不能太短,否则不准。m是分钟单位,s是秒的单位,h是小时的单位,不写单位的话默认是毫秒
spring:
application:
name: test-platform-backend
datasource:
url: jdbc:mysql://127.0.0.1:13306/itest_mini?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: admin123
driver-class-name: com.mysql.cj.jdbc.Driver
mvc:
pathmatch:
matching-strategy: ant_path_matcher
iTest:
env: dev
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.itestmini.testplatformbackend.entity
configuration:
mapUnderscoreToCamelCase: true
logging:
level:
com.hogwartstest.aitestmini.dao: debug #打印sql
map:
local:
env: 开发环境
5. 启动类增加扫描路径
package com.itestmini.testplatformbackend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.itestmini.testplatformbackend.dao")
public class TestPlatformBackendApplication {
public static void main(String[] args) {
SpringApplication.run(TestPlatformBackendApplication.class, args);
}
}
6. 在common添加mapper映射关系
package com.itestmini.testplatformbackend.common;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* @author moso
* @Date 2024/12/28 10:55
*/
public interface MySqlExtensionMapper<T> extends Mapper<T>, MySqlMapper<T>, IdsMapper<T> {
}
7. entity增加基础entity
package com.itestmini.testplatformbackend.entity;
import java.io.Serializable;
/**
* @author moso
* @Date 2024/12/28 11:11
*/
public class BaseEntityNew implements Serializable {
}
8. 其他配置如果有遗漏可以看提交记录信息
另外配置文件中的mysql及路径相关的配置大家自行修改
https://gitee.com/moso520/test-platform-backend
fde47f6
9. 创建表
我在本地创建了itest_mini的库
再创建表:
CREATE TABLE `itest_user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名',
`password` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '密码',
`email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '邮箱',
`auto_create_case_job_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '自动生成用例job名称 不为空时表示已经创建job',
`start_test_job_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '执行测试job名称 不为空时表示已经创建job',
`default_jenkins_id` int DEFAULT NULL COMMENT '默认Jenkins服务器',
`create_time` datetime NOT NULL COMMENT '创建时间',
`update_time` datetime NOT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='用户表';
10. 测试运行:
点击下面的generator
看到如下日志说明成功:
下面的文件自动生成: