Showcase也转为Spring Data Jpa
This commit is contained in:
@@ -5,9 +5,7 @@
|
||||
TODO部分:
|
||||
|
||||
1.主要改动
|
||||
* 引入Profile管理環境
|
||||
* 用Spring Data JPA代替BaseDao基类
|
||||
--Showcase
|
||||
--清理相关类
|
||||
|
||||
2.次要改动
|
||||
@@ -19,14 +17,15 @@ TODO部分:
|
||||
* MVC框架从Struts2转为Spring MVC3.
|
||||
* DAO框架增加MyBatis的使用,与Hibernate在不同的场景各展所长
|
||||
* 安全框架从SpringSecurity转为Apache Shiro.
|
||||
* SpringSide Modules的重构
|
||||
* 使用SiteMesh3实现Layout Decoration
|
||||
* CSS框架从YUI转为Blueprint
|
||||
* Spring引入Profile概念统一测试、生产環境的配置
|
||||
* 演示Hibernate Validator
|
||||
* 升級Jackson1.9 演示,包括可擴展的對象, 對象循環引用,局部更新,自定义类型的持久化,自定义Naming策略,已存在對象等新特性.
|
||||
* 添加各种常用Utility的Demo, 升级Guava和Commons-Lang3.0.
|
||||
* 升级Selenium2.0演示,融合Selenium 1.0与2.0的API
|
||||
* Mock框架从EasyMock+JMock换成Mockito + PowerMock
|
||||
* SpringSide Modules的重构
|
||||
* 依赖包重要版本升级如:Maven 3.0, Hibernate 3.6.9, Jetty 7.6.3
|
||||
|
||||
2. 次要改动
|
||||
@@ -55,7 +54,8 @@ TODO部分:
|
||||
|
||||
1.主要改动
|
||||
* 升级Hibernate 4.0
|
||||
* 升级Quartz2.x, Spring Schedule Executor配置升级
|
||||
* Schedule演示升级,升级Quartz2.x,Spring Schedule Executor配置升级
|
||||
* Search Form与分页
|
||||
|
||||
2.次要改动
|
||||
* 包括Guava的Cache Builder做的简单缓存
|
||||
@@ -64,16 +64,17 @@ TODO部分:
|
||||
* Jersey的Jackson JSON绑定看有没有简化,并升级其他用法。
|
||||
* 升级Jquery-validation用法??
|
||||
* JPA2.0特征
|
||||
* Shiro的方法级权限控制, Test Helper, enable/diable用户抛出用户已锁定的异常.
|
||||
* Hash与加密要加入salt
|
||||
* UT只初始化一次數據
|
||||
* 用JUnit自带的Catetory的演示,Harmset演示
|
||||
* Selenium使用自带的Wait类, 检查新版API变化
|
||||
* 更深入演示PowerMock
|
||||
* Shiro的方法级权限控制, Test Helper, enable/diable用户抛出用户已锁定的异常.
|
||||
* 取消GBK编码的bat
|
||||
* 第三方repository也移到github,依赖包次要版本升级:JSW 3.5.7, Log4jdbc,Spymemcached(需自行打包)
|
||||
* 在Jenkins上运行覆盖率,Checkstyle,PMD,Findbugs检查
|
||||
* 参考springfuse改进404.jsp,500.jsp
|
||||
* 取消GBK编码的bat
|
||||
* 第三方repository也移到github,依赖包次要版本升级:JSW 3.5.7, Log4jdbc,Spymemcached(需自行打包),在pom.xml中标注自己提供的包
|
||||
* 在Jenkins上运行覆盖率,Checkstyle,PMD,Findbugs检查
|
||||
|
||||
|
||||
|
||||
3.其他非代码改动
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"
|
||||
default-lazy-init="true">
|
||||
|
||||
<description>Spring公共配置文件 </description>
|
||||
@@ -68,8 +69,6 @@
|
||||
<!-- 数据源配置,使用应用服务器的数据库连接池 -->
|
||||
<!--<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ExampleDB" />-->
|
||||
</beans>
|
||||
|
||||
|
||||
|
||||
<beans profile="test">
|
||||
<!-- 定义易受环境影响的变量 -->
|
||||
|
||||
@@ -77,7 +77,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springside.examples.miniweb.entity.account.Group;
|
||||
|
||||
/**
|
||||
* 权限组对象的泛型DAO.
|
||||
* 权限组对象的Dao interface.
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
package org.springside.examples.miniweb.dao.account;
|
||||
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
|
||||
@NoRepositoryBean
|
||||
/**
|
||||
* GroupDao的扩展行为interface.
|
||||
*/
|
||||
public interface GroupDaoCustom {
|
||||
|
||||
/**
|
||||
|
||||
+3
-1
@@ -9,6 +9,9 @@ import org.springframework.stereotype.Component;
|
||||
import org.springside.examples.miniweb.entity.account.Group;
|
||||
import org.springside.examples.miniweb.entity.account.User;
|
||||
|
||||
/**
|
||||
* GroupDao的扩展行为实现类.
|
||||
*/
|
||||
@Component
|
||||
public class GroupDaoImpl implements GroupDaoCustom {
|
||||
|
||||
@@ -24,7 +27,6 @@ public class GroupDaoImpl implements GroupDaoCustom {
|
||||
List<User> users = (List<User>) em.createQuery(QUERY_USER_BY_GROUPID).setParameter(1, id).getResultList();
|
||||
for (User u : users) {
|
||||
u.getGroupList().remove(group);
|
||||
em.persist(u);
|
||||
}
|
||||
em.remove(group);
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springside.examples.miniweb.entity.account.User;
|
||||
|
||||
/**
|
||||
* 用户对象的Interface类.
|
||||
* 用户对象的Dao interface.
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
|
||||
@@ -3,15 +3,14 @@
|
||||
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
|
||||
default-lazy-init="true">
|
||||
|
||||
<description>Spring公共配置 </description>
|
||||
@@ -21,10 +20,11 @@
|
||||
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
|
||||
</context:component-scan>
|
||||
|
||||
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
|
||||
p:dataSource-ref="dataSource"
|
||||
p:persistenceUnitName="defaultPU"
|
||||
p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter" >
|
||||
<!-- Jpa Entity Manager 配置 -->
|
||||
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="persistenceUnitName" value="defaultPU"/>
|
||||
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
|
||||
<property name="jpaProperties">
|
||||
<props>
|
||||
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
||||
@@ -37,10 +37,13 @@
|
||||
|
||||
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
|
||||
|
||||
<!-- Spring Data Jpa配置 -->
|
||||
<jpa:repositories base-package="org.springside.examples.miniweb" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
|
||||
p:entityManagerFactory-ref="entityManagerFactory"/>
|
||||
<!-- JPpa 事务配置 -->
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
|
||||
<!-- 使用annotation定义事务 -->
|
||||
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
|
||||
|
||||
<!-- 自动扫描且只扫描@Controller -->
|
||||
<context:component-scan base-package="org.springside.examples.miniweb" use-default-filters="false">
|
||||
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
|
||||
</context:component-scan>
|
||||
|
||||
@@ -63,10 +63,18 @@
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
<version>3.12.1.GA</version>
|
||||
</dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-ehcache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.javax.persistence</groupId>
|
||||
<artifactId>hibernate-jpa-2.0-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
@@ -78,6 +86,10 @@
|
||||
</dependency>
|
||||
|
||||
<!-- spring data access-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package org.springside.examples.showcase.common.dao;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springside.examples.showcase.common.entity.Role;
|
||||
import org.springside.modules.orm.hibernate.HibernateDao;
|
||||
|
||||
/**
|
||||
* 角色对象的泛型Hibernate Dao.
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
@Component
|
||||
public class RoleHibernateDao extends HibernateDao<Role, String> {
|
||||
}
|
||||
-65
@@ -1,65 +0,0 @@
|
||||
package org.springside.examples.showcase.common.dao;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.FetchMode;
|
||||
import org.hibernate.Query;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springside.examples.showcase.common.entity.User;
|
||||
import org.springside.modules.orm.hibernate.HibernateDao;
|
||||
|
||||
/**
|
||||
* 用户对象的泛型Hibernate Dao.
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
@Component
|
||||
public class UserHibernateDao extends HibernateDao<User, Long> {
|
||||
|
||||
private static final String QUERY_USER_WITH_ROLE = "select u from User u left join fetch u.roleList order by u.id";
|
||||
private static final String COUNT_USERS = "select count(u) from User u";
|
||||
private static final String DISABLE_USERS = "update User u set u.status='disabled' where id in(:ids)";
|
||||
|
||||
/**
|
||||
* 批量修改用户状态.
|
||||
*/
|
||||
public void disableUsers(List<Long> ids) {
|
||||
Map<String, List<Long>> map = Collections.singletonMap("ids", ids);
|
||||
batchExecute(UserHibernateDao.DISABLE_USERS, map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 HQL 预加载lazy init的List<Role>,用DISTINCE_ROOT_ENTITY排除重复数据.
|
||||
*/
|
||||
|
||||
public List<User> getAllUserWithRoleByDistinctHql() {
|
||||
Query query = createQuery(QUERY_USER_WITH_ROLE);
|
||||
return distinct(query).list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用Criteria 预加载lazy init的List<Role>, 用DISTINCE_ROOT_ENTITY排除重复数据.
|
||||
*/
|
||||
|
||||
public List<User> getAllUserWithRolesByDistinctCriteria() {
|
||||
Criteria criteria = createCriteria().setFetchMode("roleList", FetchMode.JOIN);
|
||||
return distinct(criteria).list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计用户数.
|
||||
*/
|
||||
public Long getUserCount() {
|
||||
return findUnique(UserHibernateDao.COUNT_USERS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化User的延迟加载关联roleList.
|
||||
*/
|
||||
public void initUser(User user) {
|
||||
initProxyObject(user.getRoleList());
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package org.springside.examples.showcase.common.dao;
|
||||
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springside.examples.showcase.common.entity.User;
|
||||
|
||||
public interface UserJpaDao extends PagingAndSortingRepository<User, Long> {
|
||||
|
||||
User findByName(String name);
|
||||
|
||||
User findByLoginName(String loginName);
|
||||
}
|
||||
+20
-22
@@ -9,13 +9,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springside.examples.showcase.cache.memcached.MemcachedObjectType;
|
||||
import org.springside.examples.showcase.common.dao.UserHibernateDao;
|
||||
import org.springside.examples.showcase.common.dao.UserJpaDao;
|
||||
import org.springside.examples.showcase.common.dao.UserMyBatisDao;
|
||||
import org.springside.examples.showcase.common.entity.User;
|
||||
import org.springside.examples.showcase.jms.simple.NotifyMessageProducer;
|
||||
import org.springside.examples.showcase.security.ShiroDbRealm;
|
||||
import org.springside.modules.mapper.JsonMapper;
|
||||
import org.springside.modules.memcached.SpyMemcachedClient;
|
||||
import org.springside.modules.orm.jpa.JpaUtils;
|
||||
import org.springside.modules.utils.security.Digests;
|
||||
|
||||
/**
|
||||
@@ -29,7 +30,7 @@ import org.springside.modules.utils.security.Digests;
|
||||
public class AccountManager {
|
||||
private static Logger logger = LoggerFactory.getLogger(AccountManager.class);
|
||||
|
||||
private UserHibernateDao userHibernateDao;
|
||||
private UserJpaDao userJpaDao;
|
||||
|
||||
private UserMyBatisDao userMyBatisDao;
|
||||
|
||||
@@ -59,7 +60,7 @@ public class AccountManager {
|
||||
String shaPassword = Digests.sha1Hex(user.getPlainPassword());
|
||||
user.setShaPassword(shaPassword);
|
||||
|
||||
userHibernateDao.save(user);
|
||||
userJpaDao.save(user);
|
||||
|
||||
if (shiroRealm != null) {
|
||||
shiroRealm.clearCachedAuthorizationInfo(user.getLoginName());
|
||||
@@ -68,6 +69,10 @@ public class AccountManager {
|
||||
sendNotifyMessage(user);
|
||||
}
|
||||
|
||||
public List<User> getAllUser() {
|
||||
return (List<User>) userJpaDao.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否超级管理员.
|
||||
*/
|
||||
@@ -76,13 +81,13 @@ public class AccountManager {
|
||||
}
|
||||
|
||||
public User getUser(Long id) {
|
||||
return userHibernateDao.get(id);
|
||||
return userJpaDao.findOne(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得用户, 并对用户的延迟加载关联进行初始化.
|
||||
* 取得用户,先尝试从缓存获取,然后用Mybatis查询.
|
||||
*/
|
||||
public User getInitializedUser(Long id) {
|
||||
public User getUserEffective(Long id) {
|
||||
if (memcachedClient != null) {
|
||||
return getUserFromMemcached(id);
|
||||
} else {
|
||||
@@ -117,30 +122,22 @@ public class AccountManager {
|
||||
/**
|
||||
* 按名称查询用户, 并对用户的延迟加载关联进行初始化.
|
||||
*/
|
||||
public User searchInitializedUserByName(String name) {
|
||||
User user = userHibernateDao.findUniqueBy("name", name);
|
||||
userHibernateDao.initUser(user);
|
||||
return user;
|
||||
}
|
||||
public User findUserByNameInitialized(String name) {
|
||||
User user = userJpaDao.findByName(name);
|
||||
JpaUtils.initLazyProperty(user.getRoleList());
|
||||
|
||||
/**
|
||||
* 取得所有用户, 预加载用户的角色.
|
||||
*/
|
||||
public List<User> getAllUserWithRole() {
|
||||
List<User> list = userHibernateDao.getAllUserWithRoleByDistinctHql();
|
||||
logger.info("get {} user sucessful.", list.size());
|
||||
return list;
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户数量.
|
||||
*/
|
||||
public Long getUserCount() {
|
||||
return userHibernateDao.getUserCount();
|
||||
return userJpaDao.count();
|
||||
}
|
||||
|
||||
public User findUserByLoginName(String loginName) {
|
||||
return userHibernateDao.findUniqueBy("loginName", loginName);
|
||||
return userJpaDao.findByLoginName(loginName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,8 +157,8 @@ public class AccountManager {
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setUserHibernateDao(UserHibernateDao userDao) {
|
||||
this.userHibernateDao = userDao;
|
||||
public void setUserJpaDao(UserJpaDao userJpaDao) {
|
||||
this.userJpaDao = userJpaDao;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@@ -183,4 +180,5 @@ public class AccountManager {
|
||||
public void setShiroRealm(ShiroDbRealm shiroRealm) {
|
||||
this.shiroRealm = shiroRealm;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ public class UserController {
|
||||
|
||||
@RequestMapping(value = { "list", "" })
|
||||
public String list(Model model) {
|
||||
List<User> users = accountManager.getAllUserWithRole();
|
||||
List<User> users = accountManager.getAllUser();
|
||||
model.addAttribute("users", users);
|
||||
return "common/userList";
|
||||
}
|
||||
|
||||
+3
-3
@@ -57,7 +57,7 @@ public class UserResourceService {
|
||||
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML + WsConstants.CHARSET })
|
||||
public List<UserDTO> getAllUser() {
|
||||
try {
|
||||
List<User> entityList = accountManager.getAllUserWithRole();
|
||||
List<User> entityList = accountManager.getAllUser();
|
||||
return BeanMapper.mapList(entityList, UserDTO.class);
|
||||
} catch (RuntimeException e) {
|
||||
throw WebExceptionFactory.buildDefaultException(e, logger);
|
||||
@@ -72,7 +72,7 @@ public class UserResourceService {
|
||||
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML + WsConstants.CHARSET })
|
||||
public UserDTO getUser(@PathParam("id") Long id) {
|
||||
try {
|
||||
User entity = accountManager.getInitializedUser(id);
|
||||
User entity = accountManager.getUserEffective(id);
|
||||
return BeanMapper.map(entity, UserDTO.class);
|
||||
} catch (ObjectNotFoundException e) {
|
||||
String message = "用户不存在(id:" + id + ")";
|
||||
@@ -93,7 +93,7 @@ public class UserResourceService {
|
||||
public Response searchUser(@QueryParam("name") String name,
|
||||
@QueryParam("format") @DefaultValue("json") String format) {
|
||||
try {
|
||||
User entity = accountManager.searchInitializedUserByName(name);
|
||||
User entity = accountManager.findUserByNameInitialized(name);
|
||||
if ("html".equals(format)) {
|
||||
//返回html格式的特定字符串.
|
||||
String html = "<div>" + entity.getName() + ", your mother call you...</div>";
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm/orm_2_0.xsd" version="2.0">
|
||||
</entity-mappings>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
|
||||
<!-- Keep this file empty, Spring will scan the @Entity classes -->
|
||||
<persistence-unit name="defaultPU" transaction-type="RESOURCE_LOCAL">
|
||||
<mapping-file>META-INF/orm.xml</mapping-file>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
@@ -1,11 +1,13 @@
|
||||
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
|
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
|
||||
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
|
||||
default-lazy-init="true">
|
||||
|
||||
<description>Spring公共配置</description>
|
||||
@@ -15,20 +17,26 @@
|
||||
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
|
||||
</context:component-scan>
|
||||
|
||||
<!-- Hibernate配置 -->
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="namingStrategy">
|
||||
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
|
||||
</property>
|
||||
<property name="hibernateProperties">
|
||||
<!-- Jpa Entity Manager 配置 -->
|
||||
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="persistenceUnitName" value="defaultPU"/>
|
||||
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
|
||||
<property name="jpaProperties">
|
||||
<props>
|
||||
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
||||
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
|
||||
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
|
||||
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
|
||||
</props>
|
||||
</property>
|
||||
<property name="packagesToScan" value="org.springside.examples.showcase.common.entity" />
|
||||
</bean>
|
||||
|
||||
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
|
||||
|
||||
<!-- Spring Data Jpa配置 -->
|
||||
<jpa:repositories base-package="org.springside.examples.showcase" transaction-manager-ref="defaultTransactionManager" entity-manager-factory-ref="entityManagerFactory"/>
|
||||
|
||||
<!-- MyBatis配置 -->
|
||||
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
@@ -36,9 +44,9 @@
|
||||
<property name="mapperLocations" value="classpath:/mapper/*Mapper.xml" />
|
||||
</bean>
|
||||
|
||||
<!-- 事务管理器配置, Hibernate单数据源事务 -->
|
||||
<bean id="defaultTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
<!-- 事务管理器配置, Jpa单数据源事务 -->
|
||||
<bean id="defaultTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
|
||||
<!-- 另一个事务管理器, Jdbc单数据源事务 -->
|
||||
@@ -86,7 +94,6 @@
|
||||
</beans>
|
||||
|
||||
<beans profile="test">
|
||||
|
||||
<!-- 定义受环境影响易变的变量 -->
|
||||
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
|
||||
@@ -135,5 +142,4 @@
|
||||
<bean class="org.springside.modules.test.spring.SpringContextHolder" lazy-init="false" />
|
||||
</beans>
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
<bean id="shiroDbRealm" class="org.springside.examples.showcase.security.ShiroDbRealm" />
|
||||
|
||||
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
|
||||
<bean id="shiroFilter" class="org.springside.modules.shiro.ShiroFilterFactoryBean">
|
||||
<property name="securityManager" ref="securityManager" />
|
||||
<property name="loginUrl" value="/login" />
|
||||
<property name="successUrl" value="/" />
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
|
||||
|
||||
<!-- 自动扫描且只扫描@Controller -->
|
||||
<context:component-scan base-package="org.springside.examples.showcase" use-default-filters="false">
|
||||
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
|
||||
</context:component-scan>
|
||||
|
||||
@@ -95,13 +95,13 @@
|
||||
<dispatcher>FORWARD</dispatcher>
|
||||
</filter-mapping>
|
||||
|
||||
<!-- Hibernate Open Session In View filter -->
|
||||
<!-- Open Entity Manager in View filter-->
|
||||
<filter>
|
||||
<filter-name>hibernateOpenSessionInViewFilter</filter-name>
|
||||
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
|
||||
<filter-name>openEntityManagerInViewFilter</filter-name>
|
||||
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>hibernateOpenSessionInViewFilter</filter-name>
|
||||
<filter-name>openEntityManagerInViewFilter</filter-name>
|
||||
<url-pattern>/common/*</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
|
||||
+3
-3
@@ -7,7 +7,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springside.examples.showcase.common.dao.UserHibernateDao;
|
||||
import org.springside.examples.showcase.common.dao.UserJpaDao;
|
||||
import org.springside.examples.showcase.common.entity.User;
|
||||
import org.springside.examples.showcase.common.service.AccountManager;
|
||||
import org.springside.examples.showcase.common.service.ServiceException;
|
||||
@@ -17,7 +17,7 @@ public class AccountManagerTest {
|
||||
|
||||
private AccountManager accountManager;
|
||||
@Mock
|
||||
private UserHibernateDao mockUserDao;
|
||||
private UserJpaDao mockUserDao;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -25,7 +25,7 @@ public class AccountManagerTest {
|
||||
ShiroTestHelper.mockSubject("foo");
|
||||
|
||||
accountManager = new AccountManager();
|
||||
accountManager.setUserHibernateDao(mockUserDao);
|
||||
accountManager.setUserJpaDao(mockUserDao);
|
||||
}
|
||||
|
||||
@After
|
||||
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package org.springside.examples.showcase.common;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.persister.entity.EntityPersister;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.TransactionConfiguration;
|
||||
import org.springside.modules.test.data.Fixtures;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
|
||||
/**
|
||||
* 简单测试所有Entity类的O/R Mapping.
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
@ContextConfiguration(locations = { "/applicationContext.xml" })
|
||||
@TransactionConfiguration(transactionManager = "defaultTransactionManager")
|
||||
public class HibernateMappingTest extends SpringTxTestCase {
|
||||
private static Logger logger = LoggerFactory.getLogger(HibernateMappingTest.class);
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@Test
|
||||
public void allClassMapping() throws Exception {
|
||||
Fixtures.reloadAllTable(dataSource, "/data/sample-data.xml");
|
||||
|
||||
Session session = sessionFactory.openSession();
|
||||
try {
|
||||
Map metadata = sessionFactory.getAllClassMetadata();
|
||||
for (Object o : metadata.values()) {
|
||||
EntityPersister persister = (EntityPersister) o;
|
||||
String className = persister.getEntityName();
|
||||
Query q = session.createQuery("from " + className + " c");
|
||||
q.iterate();
|
||||
logger.debug("ok: " + className);
|
||||
}
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package org.springside.examples.showcase.common;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.metamodel.EntityType;
|
||||
import javax.persistence.metamodel.Metamodel;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.TransactionConfiguration;
|
||||
import org.springside.modules.test.data.Fixtures;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
|
||||
@ContextConfiguration(locations = { "/applicationContext.xml" })
|
||||
@TransactionConfiguration()
|
||||
public class JpaMappingTest extends SpringTxTestCase {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(JpaMappingTest.class);
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Test
|
||||
public void allClassMapping() throws Exception {
|
||||
Fixtures.reloadAllTable(dataSource, "/data/sample-data.xml");
|
||||
Metamodel model = em.getEntityManagerFactory().getMetamodel();
|
||||
for (EntityType entityType : model.getEntities()) {
|
||||
String entityName = entityType.getName();
|
||||
em.createQuery("select o from " + entityName + " o").getFirstResult();
|
||||
logger.info("ok: " + entityName);
|
||||
}
|
||||
}
|
||||
}
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
package org.springside.examples.showcase.common;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.transaction.TransactionConfiguration;
|
||||
import org.springside.examples.showcase.common.dao.UserHibernateDao;
|
||||
import org.springside.examples.showcase.common.entity.User;
|
||||
import org.springside.modules.test.data.Fixtures;
|
||||
import org.springside.modules.test.spring.SpringTxTestCase;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
/**
|
||||
* UserDao的集成测试用例,测试ORM映射及特殊的DAO操作.
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
@ContextConfiguration(locations = { "/applicationContext.xml" })
|
||||
//演示指定非默认名称的TransactionManager.
|
||||
@TransactionConfiguration(transactionManager = "defaultTransactionManager")
|
||||
public class UserHibernateDaoTest extends SpringTxTestCase {
|
||||
|
||||
@Autowired
|
||||
private UserHibernateDao userDao;
|
||||
|
||||
@Before
|
||||
public void reloadSampleData() throws Exception {
|
||||
Fixtures.reloadAllTable(dataSource, "/data/sample-data.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void eagerFetchCollection() {
|
||||
int userCount = countRowsInTable("SS_USER");
|
||||
//init by hql
|
||||
List<User> userList1 = userDao.getAllUserWithRoleByDistinctHql();
|
||||
assertEquals(userCount, userList1.size());
|
||||
assertTrue(Hibernate.isInitialized(userList1.get(0).getRoleList()));
|
||||
|
||||
//init by criteria
|
||||
List<User> userList2 = userDao.getAllUserWithRolesByDistinctCriteria();
|
||||
assertEquals(userCount, userList2.size());
|
||||
assertTrue(Hibernate.isInitialized(userList2.get(0).getRoleList()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchDisableUser() {
|
||||
List<Long> ids = Lists.newArrayList(1L, 2L);
|
||||
|
||||
userDao.disableUsers(ids);
|
||||
|
||||
assertEquals("disabled", userDao.get(1L).getStatus());
|
||||
assertEquals("disabled", userDao.get(2L).getStatus());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.springside.modules.orm.jpa;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
|
||||
public class JpaUtils {
|
||||
|
||||
public static void initLazyProperty(Object proxyed) {
|
||||
Hibernate.initialize(proxyed);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user