Commit 3e0a4f02 by 朽木不可雕也

修改mybatis的mapper构建方式

parent 69f59e92
......@@ -18,4 +18,4 @@ bootstrap 模块的 com.zhiweidata.automatictest.bootstrap.AutomaticTestBootstra
所有模块的入口都需要实现 java.lang.Runnable 接口
mybatis的mapper xml文件扫描resources的mappers文件夹,所以请吧所有mapper xml都放在mappers文件夹下
\ No newline at end of file
mybatis mapper配置统一在 [mybatis.xml](public/src/main/resources/mybatis.xml) 中配置
\ No newline at end of file
......@@ -38,12 +38,44 @@
<finalName>automatic-test</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<verbose/>
<!--需要引入一些jdk的工具包, 不然会报错-->
<bootclasspath>${java.home}/lib/rt.jar${path.separator}${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-assembly-plugin -->
<!--maven打包插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.zhiweidata.automatictest.bootstrap.AutomaticTestBootstrap</mainClass>
</manifest>
</archive>
<descriptorRefs>
<!--指定为jar打包模式-->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<!--指定打包时需要执行指令-->
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>repackage</goal>
<goal>single</goal>
</goals>
</execution>
</executions>
......
package com.zhiweidata.automatictest.publics;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.jetbrains.annotations.NotNull;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
......@@ -79,38 +71,10 @@ public class BeanContainer {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 先读取xml中的配置
XMLConfigBuilder configBuilder = new XMLConfigBuilder(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
configBuilder.parse();
Configuration configuration = configBuilder.getConfiguration();
// 扫描mapper
File mappersDir = Resources.getResourceAsFile("mappers");
scanMapper(mappersDir, configuration);
sqlSessionFactory = builder.build(configuration);
sqlSessionFactory = builder.build(inputStream);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
SQL_SESSION_FACTORY = sqlSessionFactory;
}
/**
* 扫描mapper
*/
private static void scanMapper(@NotNull File file, @NotNull Configuration configuration) throws IOException {
if (file.isDirectory()) {
File[] mappers = requireNonNull(file.listFiles());
for (File mapper : mappers) {
scanMapper(mapper, configuration);
}
} else {
String fileName = file.getName();
if (".xml".equals(fileName.substring(fileName.lastIndexOf(".")))) {
String resource = file.toPath().toUri().toString();
try (InputStream inputStream = Resources.getUrlAsStream(resource)) {
XMLMapperBuilder mapperBuilder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
mapperBuilder.parse();
}
}
}
}
}
......@@ -8,10 +8,17 @@
<transactionManager type="JDBC"/>
<dataSource type="com.zhiweidata.automatictest.publics.HikariDataSourceFactory">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.0.119:3306/automatic_test?useSSL=FALSE&amp;serverTimezone=Asia/Shanghai"/>
<property name="url"
value="jdbc:mysql://192.168.0.119:3306/automatic_test?useSSL=FALSE&amp;serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="z199809051593"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/BarrageCollectionTestTaskMapper.xml"/>
<mapper resource="mappers/BarrageExportResultMapper.xml"/>
<mapper resource="mappers/ServerResponseMessageMapper.xml"/>
</mappers>
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment