Spring data JPA + HikariCP简单应用

maven配置

Spring mvc我使用4.1.6,其对应的jpa版本为:Spring mvc我使用4.1.6,其对应的jpa版本为1.9.0.M1(不然mvc的一些方法,在jpa上使用不了,导致加载错误之类):

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.0.M1</version>
        </dependency>
    
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP-java6</artifactId>
            <version>2.2.5</version>
        </dependency>
        
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.10.Final</version>
        </dependency>

data-access.properties (数据库配置)

abc.jdbc.driverClassName=com.mysql.jdbc.Driver
abc.jdbc.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;characterEncoding=utf-8
abc.user=xxx
abc.password=xxxx
abc.maxActive=10

datasource-config.xml (数据库连接池配置)

<!-- HikariCP 高性能数据库连接池 -->
    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="poolName" value="springHikariCP" />
        <property name="maximumPoolSize" value="7" />
        <property name="idleTimeout" value="30000" />
        <property name="connectionTimeout" value="120000" />
        <property name="jdbcUrl" value="${uadb.jdbc.url}" />
        <property name="username" value="${uadb.user}" />
        <property name="password" value="${uadb.password}" />
        <property name="driverClassName" value="${uadb.jdbc.driverClassName}" />
    </bean>
    <!-- HikariCP configuration -->
    <bean id="dataSourceMySql" class="com.zaxxer.hikari.HikariDataSource"
        destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>


applicationcontext.xml(部分重要配置)

<context:property-placeholder location="classpath:data-access.properties" />
<context:component-scan base-package="com.test.devel.*" />
<import resource="datasource-config.xml" />

<!-- 数据库相关 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource">
            <ref bean="dataSourceMySql" /> <!--指定数据库连接池-->
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false" />
                <property name="showSql" value="true" />                
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
            </bean>
        </property> 
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.cache.use_second_level_cache">false</prop><!-- 二级缓存 -->  
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>  
                <prop key="hibernate.format_sql">true</prop>
                <!-- <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop> -->
            </props> 
        </property>
        <property name="packagesToScan" value="com.test.devel.repositories.entity"/>   
    </bean>
    <jpa:repositories base-package="com.test.devel.repositories.*" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" />


例子:

    Entity:

@Entity(name="develConfig")
@Table(name = "devel_config")
public class DevelConfig {
    @Id
    @Column(name = "config_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int configId;
    
    @Column(name = "config_key")
    private String configKey;
    
    @Column(name = "config_value")
    private String configValue;
    
    @Column(name = "config_des")
    private String configDes;

    public int getConfigId() {
        return configId;
    }

    public void setConfigId(int configId) {
        this.configId = configId;
    }

    public String getConfigKey() {
        return configKey;
    }

    public void setConfigKey(String configKey) {
        this.configKey = configKey;
    }

    public String getConfigValue() {
        return configValue;
    }

    public void setConfigValue(String configValue) {
        this.configValue = configValue;
    }

    public String getConfigDes() {
        return configDes;
    }

    public void setConfigDes(String configDes) {
        this.configDes = configDes;
    }

    @Override
    public String toString() {
        return "DevelConfig [configId=" + configId + ", configKey=" + configKey + ", configValue=" + configValue
                + ", configDes=" + configDes + "]";
    }  
}

    Dao:

@Transactional(readOnly = true)
public interface DevelConfigDao extends Repository<DevelConfig, Long>{
     
     public List<DevelConfig> findByConfigId(int configId); 
     
     public List<DevelConfig> findAll();
     
     @Query("select a from develConfig a where a.configId = ?1 and a.configKey = ?2") 
     public List<DevelConfig> findmyQuery(int configId, String configKey); 
}

   Junit  Test

@RunWith(SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations= "classpath:applicationcontext.xml")
@Transactional
public class TestDevelConfig {
    
    @Autowired
    DevelConfigDao develConfigDao;
    
    @Before
    public void setUp() {
    }
    
    @Test
    public void query() {
        List<DevelConfig> list = develConfigDao.findAll();
        for (DevelConfig develConfig : list) {
            System.out.println(develConfig.toString());
        }
    } 
    
    @Test
    public void findConfigById(){
        List<DevelConfig> list = develConfigDao.findByConfigId(1);
        for (DevelConfig develConfig : list) {
            System.out.println(develConfig.toString());
        }
    }
    
    @Test
    public void findByConfigIdAndConfigKey(){
        List<DevelConfig> list = develConfigDao.findmyQuery(1, "SVN_COUNT_URL");
        for (DevelConfig develConfig : list) {
            System.out.println(develConfig.toString());
        }
    }
}


你可能感兴趣的:(Spring data JPA + HikariCP简单应用)