springboot+hikaricp+mybatis

/ java / 0 条评论 / 2747浏览

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.14.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ouyqy.hikaricp</groupId>
    <artifactId>demo</artifactId>
    <version>1.0.0</version>
    <name>demo</name>
    <description>HikariCP 样例</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

DateSourceConfig

@Configuration
@ConditionalOnClass({SqlSessionFactoryBean.class, SqlSessionFactory.class})
@MapperScan(basePackages = "com.ouyqy.hikaricp.demo.mapper",sqlSessionFactoryRef = "sqlSessionFactory")
public class DateSourceConfig extends MybatisAutoConfiguration {

    public DateSourceConfig(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
        super(properties, interceptorsProvider, typeHandlersProvider, languageDriversProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider);
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public HikariConfig hikariConfig(){
        return new HikariConfig();
    }

    @Primary
    @Bean
    public DataSource dataSource(HikariConfig hikariConfig){
        return new HikariDataSource(hikariConfig);
    }

    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        return super.sqlSessionFactory(dataSource);
    }
}

application.yml

server:
  port: 8090
  servlet:
    context-path: /demo
logging:
  level: 
    root: debug
spring:
  application:
    name: demo
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&failOverReadOnly=false&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
    connection-timeout: 30000
    minimum-idle: 5
    maximum-pool-size: 50
    auto-commit: true
    idle-timeout: 600000
    pool-name: ClearingHikariCP
    max-lifetime: 1800000
    connection-test-query: SELECT 1
mybatis:
  type-aliases-package: com.ouyqy.hikaricp.demo
  mapper-locations: classpath:mapper/*.xml

TestController.java

@RestController
@RequestMapping("/test")
public class TestController {
    
    @Resource
    private TestMapper testMapper;
    
    @Resource
    private JdbcTemplate jdbcTemplate;
    
    @RequestMapping("/query")
    public List<String> query(){
        return testMapper.query();
    }

    @RequestMapping("/query_sql")
    public List<String> query_sql(){
        String sql ="SELECT NAME FROM S_DIC";
        return jdbcTemplate.queryForList(sql,String.class);
    }
}

TestMapper.java

public interface TestMapper {
    List<String> query();
}

TestMapper.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.ouyqy.hikaricp.demo.mapper.TestMapper">

    <select id="query" resultType="java.lang.String">
        SELECT `NAME` FROM S_DIC
    </select>
    
</mapper>
评论已关闭.