标签:stat tor password value tar inter ges config ati
首先配置maven包
<dependency><!-- jdbc -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency><!--mybatis-->
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
注意pom.xml中配置
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>static/*.*</include>
<include>templates/*.*</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
针对不同的数据库,引入对应的包:SqlServer-sqljdbc4
,DB2-db2jcc4
,以SqlServer为例
在com.cmb.**.mapper.sqlserver
目录下新建接口文件
@Repository
public interface SqlserverMapper {
List<String> function_name();
}
同时在resources/mapperconfig/sqlserver
目录下新建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.cmb.afvapp.api.mapper.sqlserver.SqlserverMapper">
<select id="function_name" resultType="String">
select ... from ...
</select>
</mapper>
数据源类和工厂类配置如下
@MapperScan(basePackages = "com.cmb.**.mapper.sqlserver", sqlSessionFactoryRef = "sqlserverSqlSessionFactoryBean")
@SpringBootConfiguration
public class SqlserverDataSourceConfiguration {
private static String jdbcUrl, jdbcUser, jdbcPassword, maximumPoolSize;
static { ... }
@Bean(name = "sqlserverDataSource")
public DataSource createDataSource() {
return DataSourceConfig.getHikariDataSourceInstance(jdbcUrl, jdbcUser, jdbcPassword, maximumPoolSize);
}
}
@Configuration
public class SqlserverSessionFactoryConfiguration {
@Autowired
@Qualifier(value = "sqlserverDataSource")
private DataSource sqlserverDataSource;
private String mapperXmlConfigPath = "/mapperconfig/sqlserver/**.xml", mapperPackagePath = "com.cmb.**.mapper.sqlserver";
@Bean(name = "sqlserverSqlSessionFactoryBean")
public SqlSessionFactoryBean createSqlSessionFactory() throws Exception {
return DataSourceConfig.getSqlSessionFactoryBeanInstance(mapperXmlConfigPath, mapperPackagePath, sqlserverDataSource);
}
}
其中,DataSourceConfig
工具类
public static HikariDataSource getHikariDataSourceInstance(String jdbcUrl, String jdbcUser, String jdbcPassword, String maximumPoolSize) {
HikariDataSource source = new HikariDataSource();
source.setJdbcUrl(jdbcUrl);
source.setUsername(jdbcUser);
source.setPassword(jdbcPassword);
source.setMaximumPoolSize(Integer.valueOf(maximumPoolSize));
return source;
}
public static SqlSessionFactoryBean getSqlSessionFactoryBeanInstance(String mapperXmlConfigPath, String mapperPackagePath, DataSource ds) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String xmlConfigPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperXmlConfigPath;
bean.setMapperLocations(resolver.getResources(xmlConfigPath));//设置mapper对应的XML文件的路径
bean.setDataSource(ds);//设置数据源
bean.setTypeAliasesPackage(mapperPackagePath);//设置mapper接口所在的包
bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return bean;
}
标签:stat tor password value tar inter ges config ati
原文地址:https://www.cnblogs.com/wjcx-sqh/p/12404118.html