Created
September 16, 2014 05:49
-
-
Save greycode/dea7d1130dc70a452501 to your computer and use it in GitHub Desktop.
Spring Data JPA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:jdbc="http://www.springframework.org/schema/jdbc" | |
xmlns:jee="http://www.springframework.org/schema/jee" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
xmlns:jpa="http://www.springframework.org/schema/data/jpa" | |
default-lazy-init="true" | |
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd | |
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd | |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd | |
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd | |
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd | |
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> | |
<description>Spring公共配置</description> | |
<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 --> | |
<context:component-scan base-package="org.demoweb"> | |
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> | |
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> | |
</context:component-scan> | |
<!-- Jpa Entity Manager 配置 --> | |
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | |
<property name="dataSource" ref="dataSource"/> | |
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/> | |
<property name="packagesToScan" value="org.demoweb"/> | |
<property name="jpaProperties"> | |
<props> | |
<!-- | |
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> | |
<prop key="net.sf.ehcache.configurationResourceName">cache/ehcache-hibernate-local.xml</prop>--> | |
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> | |
<prop key="hibernate.show_sql">true</prop> | |
</props> | |
</property> | |
</bean> | |
<!-- 事务管理器配置, Jpa单数据源事务 --> | |
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> | |
<property name="entityManagerFactory" ref="entityManagerFactory"/> | |
</bean> | |
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> | |
<property name="databasePlatform"> | |
<bean factory-method="getDialect" class="org.xxx.modules.persistence.Hibernates"> | |
<constructor-arg ref="dataSource"/> | |
</bean> | |
</property> | |
</bean> | |
<!-- Spring Data Jpa配置, 扫描base-package下所有继承于Repository<T,ID>的接口 --> | |
<jpa:repositories base-package="org.demoweb" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/> | |
<!-- production环境 --> | |
<beans profile="production"> | |
<!-- | |
<context:property-placeholder ignore-resource-not-found="true" | |
location="classpath*:/application.properties" /> --> | |
<!-- 数据源配置,使用应用内的Tomcat JDBC连接池 --> | |
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> | |
<!-- Connection Info --> | |
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> | |
<property name="url" value="jdbc:oracle:thin:@ip:port/dbservice" /> | |
<property name="username" value="name" /> | |
<property name="password" value="pwd" /> | |
<property name="maxActive" value="50" /> | |
<property name="maxIdle" value="10" /> | |
<property name="minIdle" value="0" /> | |
<property name="defaultAutoCommit" value="false" /> | |
<!-- 连接Idle10分钟后超时,每1分钟检查一次 --> | |
<property name="timeBetweenEvictionRunsMillis" value="60000" /> | |
<property name="minEvictableIdleTimeMillis" value="600000" /> | |
</bean> | |
</beans> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.demoweb.jpa.entity; | |
import java.io.Serializable; | |
import javax.persistence.*; | |
import java.util.Date; | |
/** | |
* The persistent class for the CC_CARE_TASK database table. | |
* | |
*/ | |
@Entity | |
@Table(name="CC_CARE_TASK") | |
@NamedQuery(name="CcCareTask.findAll", query="SELECT c FROM CcCareTask c") | |
public class CcCareTask implements Serializable { | |
private static final long serialVersionUID = -3376025673880534235L; | |
@Id | |
@SequenceGenerator(name="CC_CARE_TASK_TASKID_GENERATOR", sequenceName="CARE_TASK_S") | |
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CC_CARE_TASK_TASKID_GENERATOR") | |
@Column(name="TASK_ID", unique=true, nullable=false, length=20) | |
private String taskId; | |
@Column(name="ASSIGN_TYPE", length=20) | |
private String assignType; | |
@Column(name="BD_TASK_ID", length=64) | |
private String bdTaskId; | |
@Column(name="CREATE_PERSON", length=20) | |
private String createPerson; | |
@Temporal(TemporalType.DATE) | |
@Column(name="CREATE_TIME") | |
private Date createTime; | |
@Column(name="HANDLE_GROUP", length=20) | |
private String handleGroup; | |
@Column(name="HANDLE_PERSON", length=20) | |
private String handlePerson; | |
@Temporal(TemporalType.DATE) | |
@Column(name="TASK_BEGIN") | |
private Date taskBegin; | |
@Column(name="TASK_CSV", length=256) | |
private String taskCsv; | |
@Column(name="TASK_DESC", length=256) | |
private String taskDesc; | |
@Temporal(TemporalType.DATE) | |
@Column(name="TASK_END") | |
private Date taskEnd; | |
@Column(name="TASK_NAME", length=200) | |
private String taskName; | |
@Column(name="TASK_QUEST", length=128) | |
private String taskQuest; | |
@Column(name="TASK_STATE", length=10) | |
private String taskState; | |
@Column(name="TASK_TYPE", length=4) | |
private String taskType; | |
@Column(name="UPDATE_PERSON", length=20) | |
private String updatePerson; | |
@Temporal(TemporalType.DATE) | |
@Column(name="UPDATE_TIME") | |
private Date updateTime; | |
public CcCareTask() { | |
} | |
public String getTaskId() { | |
return this.taskId; | |
} | |
public void setTaskId(String taskId) { | |
this.taskId = taskId; | |
} | |
public String getAssignType() { | |
return this.assignType; | |
} | |
public void setAssignType(String assignType) { | |
this.assignType = assignType; | |
} | |
public String getBdTaskId() { | |
return this.bdTaskId; | |
} | |
public void setBdTaskId(String bdTaskId) { | |
this.bdTaskId = bdTaskId; | |
} | |
public String getCreatePerson() { | |
return this.createPerson; | |
} | |
public void setCreatePerson(String createPerson) { | |
this.createPerson = createPerson; | |
} | |
public Date getCreateTime() { | |
return this.createTime; | |
} | |
public void setCreateTime(Date createTime) { | |
this.createTime = createTime; | |
} | |
public String getHandleGroup() { | |
return this.handleGroup; | |
} | |
public void setHandleGroup(String handleGroup) { | |
this.handleGroup = handleGroup; | |
} | |
public String getHandlePerson() { | |
return this.handlePerson; | |
} | |
public void setHandlePerson(String handlePerson) { | |
this.handlePerson = handlePerson; | |
} | |
public Date getTaskBegin() { | |
return this.taskBegin; | |
} | |
public void setTaskBegin(Date taskBegin) { | |
this.taskBegin = taskBegin; | |
} | |
public String getTaskCsv() { | |
return this.taskCsv; | |
} | |
public void setTaskCsv(String taskCsv) { | |
this.taskCsv = taskCsv; | |
} | |
public String getTaskDesc() { | |
return this.taskDesc; | |
} | |
public void setTaskDesc(String taskDesc) { | |
this.taskDesc = taskDesc; | |
} | |
public Date getTaskEnd() { | |
return this.taskEnd; | |
} | |
public void setTaskEnd(Date taskEnd) { | |
this.taskEnd = taskEnd; | |
} | |
public String getTaskName() { | |
return this.taskName; | |
} | |
public void setTaskName(String taskName) { | |
this.taskName = taskName; | |
} | |
public String getTaskQuest() { | |
return this.taskQuest; | |
} | |
public void setTaskQuest(String taskQuest) { | |
this.taskQuest = taskQuest; | |
} | |
public String getTaskState() { | |
return this.taskState; | |
} | |
public void setTaskState(String taskState) { | |
this.taskState = taskState; | |
} | |
public String getTaskType() { | |
return this.taskType; | |
} | |
public void setTaskType(String taskType) { | |
this.taskType = taskType; | |
} | |
public String getUpdatePerson() { | |
return this.updatePerson; | |
} | |
public void setUpdatePerson(String updatePerson) { | |
this.updatePerson = updatePerson; | |
} | |
public Date getUpdateTime() { | |
return this.updateTime; | |
} | |
public void setUpdateTime(Date updateTime) { | |
this.updateTime = updateTime; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright (c) 2005, 2014 springside.github.io | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
*******************************************************************************/ | |
package org.xxx.modules.persistence; | |
import java.sql.Connection; | |
import java.sql.SQLException; | |
import javax.sql.DataSource; | |
import org.apache.commons.lang3.StringUtils; | |
import org.hibernate.Hibernate; | |
import org.hibernate.dialect.H2Dialect; | |
import org.hibernate.dialect.MySQL5InnoDBDialect; | |
import org.hibernate.dialect.Oracle10gDialect; | |
import org.hibernate.dialect.PostgreSQL82Dialect; | |
import org.hibernate.dialect.SQLServer2008Dialect; | |
public class Hibernates { | |
/** | |
* Initialize the lazy property value. | |
* | |
* e.g. Hibernates.initLazyProperty(user.getGroups()); | |
*/ | |
public static void initLazyProperty(Object proxyedPropertyValue) { | |
Hibernate.initialize(proxyedPropertyValue); | |
} | |
/** | |
* 从DataSoure中取出connection, 根据connection的metadata中的jdbcUrl判断Dialect类型. | |
* 仅支持Oracle, H2, MySql, PostgreSql, SQLServer,如需更多数据库类型,请仿照此类自行编写。 | |
*/ | |
public static String getDialect(DataSource dataSource) { | |
String jdbcUrl = getJdbcUrlFromDataSource(dataSource); | |
// 根据jdbc url判断dialect | |
if (StringUtils.contains(jdbcUrl, ":h2:")) { | |
return H2Dialect.class.getName(); | |
} else if (StringUtils.contains(jdbcUrl, ":mysql:")) { | |
return MySQL5InnoDBDialect.class.getName(); | |
} else if (StringUtils.contains(jdbcUrl, ":oracle:")) { | |
return Oracle10gDialect.class.getName(); | |
} else if (StringUtils.contains(jdbcUrl, ":postgresql:")) { | |
return PostgreSQL82Dialect.class.getName(); | |
} else if (StringUtils.contains(jdbcUrl, ":sqlserver:")) { | |
return SQLServer2008Dialect.class.getName(); | |
} else { | |
throw new IllegalArgumentException("Unknown Database of " + jdbcUrl); | |
} | |
} | |
private static String getJdbcUrlFromDataSource(DataSource dataSource) { | |
Connection connection = null; | |
try { | |
connection = dataSource.getConnection(); | |
if (connection == null) { | |
throw new IllegalStateException("Connection returned by DataSource [" + dataSource + "] was null"); | |
} | |
return connection.getMetaData().getURL(); | |
} catch (SQLException e) { | |
throw new RuntimeException("Could not get database url", e); | |
} finally { | |
if (connection != null) { | |
try { | |
connection.close(); | |
} catch (SQLException e) { | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.demoweb.jpa.service; | |
import org.demoweb.jpa.entity.CcCareTask; | |
import org.demoweb.jpa.repository.TaskDao; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.PageRequest; | |
import org.springframework.stereotype.Service; | |
@Service | |
public class JpaService { | |
@Autowired | |
private TaskDao taskDao; | |
public Object getTasks () { | |
Page<CcCareTask> tasks = taskDao.findAll(new PageRequest(0, 20)); | |
return tasks; | |
} | |
public CcCareTask getTask (String taskId) { | |
return taskDao.findByTaskId(taskId); | |
} | |
public Object getNotAsignedTasks (String taskState) { | |
return taskDao.findByTaskState(taskState, new PageRequest(0, 20)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<properties> | |
<spirng.version>4.0.7.RELEASE</spirng.version> | |
<hibernate.version>4.3.5.Final</hibernate.version> | |
<spring-data-jpa.version>1.7.0.RELEASE</spring-data-jpa.version> | |
</properties> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-core</artifactId> | |
<version>${hibernate.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-entitymanager</artifactId> | |
<version>${hibernate.version}</version> | |
</dependency> | |
<!-- spring data jpa --> | |
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-jpa</artifactId> | |
<version>${spring-data-jpa.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.tomcat</groupId> | |
<artifactId>tomcat-jdbc</artifactId> | |
<version>8.0.11</version> | |
</dependency> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.demoweb.jpa.repository; | |
import java.util.List; | |
import javax.persistence.LockModeType; | |
import org.demoweb.jpa.entity.CcCareTask; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.Pageable; | |
import org.springframework.data.jpa.repository.Lock; | |
import org.springframework.data.repository.PagingAndSortingRepository; | |
public interface TaskDao extends PagingAndSortingRepository<CcCareTask, String> { | |
//@Lock(LockModeType.READ) | |
CcCareTask findByTaskId(String taskId); | |
Page<CcCareTask> findByTaskState(String taskState,Pageable pageable); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Open Entity Manager in View filter --> | |
<filter> | |
<filter-name>openEntityManagerInViewFilter</filter-name> | |
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> | |
</filter> | |
<filter-mapping> | |
<filter-name>openEntityManagerInViewFilter</filter-name> | |
<url-pattern>/*</url-pattern> | |
</filter-mapping> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment