这个列子的来源是springside下的miniweb项目,算是对springside的学习总结,通用dao,单元测试等。
1.数据库及表仍然是第二个demo的。
2.实体类还是第二个demo,同样改下包名。
3.加入dao层,先来个CommanDao.java,这个dao顾名思义,包含一下通用的dao操作。
package com.isa.demo4.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.Assert; import org.springside.modules.utils.ReflectionUtils; public class CommanDao<T,PK extends Serializable> { protected Logger logger = LoggerFactory.getLogger(getClass()); protected SessionFactory sessionFactory; protected Class<T> entityClass; /** * 用于Dao层子类使用的构造函数. * 通过子类的泛型定义取得对象类型Class. * eg. * public class UserDao extends CommanDao<User, Long> */ public CommanDao() { this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass()); } /** * 用于用于省略Dao层, 在Service层直接使用通用SimpleHibernateDao的构造函数. * 在构造函数中定义对象类型Class. * eg. * CommanDao<User, Long> userDao = new SimpleHibernateDao<User, Long>(sessionFactory, User.class); */ public CommanDao(final SessionFactory sessionFactory, final Class<T> entityClass) { this.sessionFactory = sessionFactory; this.entityClass = entityClass; } /** * 取得sessionFactory. */ public SessionFactory getSessionFactory() { return sessionFactory; } /** * 采用@Autowired按类型注入SessionFactory,当有多个SesionFactory的时候Override本函数. */ @Autowired public void setSessionFactory(final SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * 取得当前Session. */ public Session getSession() { return sessionFactory.getCurrentSession(); } /** * 按id获取对象. */ public T get(final PK id) { Assert.notNull(id, "id不能为空"); return (T) getSession().get(entityClass, id); } /** * 获取全部对象. */ public List<T> getAll() { return find(); } /** * 按Criteria查询对象列表. * * @param criterions 数量可变的Criterion. */ public List<T> find(final Criterion... criterions) { return createCriteria(criterions).list(); } /** * 根据Criterion条件创建Criteria. * * 本类封装的find()函数全部默认返回对象类型为T,当不为T时使用本函数. * * @param criterions 数量可变的Criterion. */ public Criteria createCriteria(final Criterion... criterions) { Criteria criteria = getSession().createCriteria(entityClass); for (Criterion c : criterions) { criteria.add(c); } return criteria; } /** * 保存新增或修改的对象. */ public void save(final T entity) { Assert.notNull(entity, "entity不能为空"); getSession().saveOrUpdate(entity); logger.debug("save entity: {}", entity); } /** * 按id删除对象. */ public void delete(final PK id) { Assert.notNull(id, "id不能为空"); delete(get(id)); logger.debug("delete entity {},id is {}", entityClass.getSimpleName(), id); } /** * 删除对象. * * @param entity 对象必须是session中的对象或含id属性的transient对象. */ public void delete(final T entity) { Assert.notNull(entity, "entity不能为空"); getSession().delete(entity); logger.debug("delete entity: {}", entity); } }
接下来是UserDao.java,可以看到相当简洁。
package com.isa.demo4.dao; import org.springframework.stereotype.Repository; import com.isa.demo4.domain.User; @Repository public class UserDao extends CommanDao<User, Long>{ }
4.前几个demo都没有User的插入操作,哪里来的查询?所以这个demo就增加几个接口方法。UserService.java如下:
package com.isa.demo4.service; import java.util.List; import com.isa.demo4.domain.User; public interface UserService { List<User> getAllUsers(); User getUserById(long id); void deleteUserById(long id); void saveUser(User user); }
接口的实现类UserServiceImpl.java
package com.isa.demo4.service; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.isa.demo4.dao.UserDao; import com.isa.demo4.domain.User; @Service //默认将类中的所有函数纳入事务管理. @Transactional public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override @Transactional(readOnly = true) public List<User> getAllUsers() { return userDao.getAll(); } @Override @Transactional(readOnly = true) public User getUserById(long id) { return userDao.get(id); } @Override @Transactional public void deleteUserById(long id) { userDao.delete(id); } @Override @Transactional public void saveUser(User user) { userDao.save(user); } }
5.测试,但不是TDD。以下代码来源于springside,抽出来的原因就是,类少了,看起来方便。
测试的基类:CommonTestCase.java
package com.isa.demo4.test.base; import org.hibernate.SessionFactory; import org.junit.Assert; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; import org.unitils.reflectionassert.ReflectionAssert; import org.unitils.reflectionassert.ReflectionComparatorMode; //默认载入applicationContext-test.xml,子类中的@ContextConfiguration定义将合并父类的定义. @ContextConfiguration(locations = { "applicationContext-test.xml" }) public class CommonTestCase extends AbstractTransactionalJUnit4SpringContextTests { /** * 刷新sessionFactory,强制Hibernate执行SQL以验证ORM配置. * * sessionFactory名默认为"sessionFactory". * * @see #flush(String) */ protected void flush() { flush("sessionFactory"); } /** * 刷新sessionFactory,强制Hibernate执行SQL以验证ORM配置. * 因为没有执行commit操作,不会更改测试数据库. * * @param sessionFactoryName applicationContext中sessionFactory的名称. */ protected void flush(final String sessionFactoryName) { ((SessionFactory) applicationContext.getBean(sessionFactoryName)).getCurrentSession().flush(); } /** * 将对象从session中消除, 用于测试对象的初始化情况. * * sessionFactory名默认为"sessionFactory". */ protected void evict(Object entity) { evict(entity, "sessionFactory"); } /** * 将对象从session中消除, 用于测试初对象的始化情况. * */ protected void evict(final Object entity, final String sessionFactoryName) { ((SessionFactory) applicationContext.getBean(sessionFactoryName)).getCurrentSession().evict(entity); } /** * sleep等待,单位毫秒. */ protected void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { } } //-- Assert 函数 --// /** * 反射比较对象间的所有属性,忽略expected对象的Null对象和集合中对象的次序. */ protected void assertReflectionEquals(Object expected, Object actual) { ReflectionAssert.assertReflectionEquals(expected, actual, ReflectionComparatorMode.IGNORE_DEFAULTS, ReflectionComparatorMode.LENIENT_ORDER); } /** * @see #assertReflectionEquals(Object, Object) */ protected void assertReflectionEquals(String message, Object expected, Object actual) { ReflectionAssert.assertReflectionEquals(message, expected, actual, ReflectionComparatorMode.IGNORE_DEFAULTS, ReflectionComparatorMode.LENIENT_ORDER); } protected void assertEquals(Object expected, Object actual) { Assert.assertEquals(expected, actual); } protected void assertEquals(String message, Object expected, Object actual) { Assert.assertEquals(message, expected, actual); } protected void assertTrue(boolean condition) { Assert.assertTrue(condition); } protected void assertTrue(String message, boolean condition) { Assert.assertTrue(message, condition); } protected void assertFalse(boolean condition) { Assert.assertFalse(condition); } protected void assertFalse(String message, boolean condition) { Assert.assertFalse(message, condition); } protected void assertNull(Object object) { Assert.assertNull(object); } protected void assertNull(String message, Object object) { Assert.assertNull(message, object); } protected void assertNotNull(Object object) { Assert.assertNotNull(object); } protected void assertNotNull(String message, Object object) { Assert.assertNotNull(message, object); } }
默认测试所用的配置文件applicationContext-test.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-lazy-init="true"> <description>测试配置</description> </beans>
额,怎么是空的?是的,真正测试的时候我们在单元测试类加入如下注释指定具体的配置文件:
@ContextConfiguration(locations = { "classpath:demo4/applicationContext.xml" })
比如:下面对dao层的测试类UserDaoTest.java
package com.isa.demo4.test.unit; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import com.isa.demo4.dao.UserDao; import com.isa.demo4.domain.User; import com.isa.demo4.test.base.CommonTestCase; import com.isa.demo4.test.data.TestData; @ContextConfiguration(locations = { "classpath:demo4/applicationContext.xml" }) public class UserDaoTest extends CommonTestCase { @Autowired private UserDao userDao; @Test //如果你需要真正插入数据库,将Rollback设为false //@Rollback(false) public void crudEntity(){ //new User newUser=TestData.getRandomUser(); userDao.save(newUser); //强制执行sql flush(); //get User getUser=userDao.get(newUser.getId()); assertReflectionEquals(newUser,getUser); //delete userDao.delete(getUser.getId()); flush(); User nullUser=userDao.get(getUser.getId()); assertNull(nullUser); } }
而这个测试类中有一个TestData,此类为测试提供数据,
package com.isa.demo4.test.data; import org.apache.commons.lang.RandomStringUtils; import com.isa.demo4.domain.User; public class TestData { public static String random() { return RandomStringUtils.randomAlphanumeric(5); } public static User getRandomUser() { String userName = "User" + random(); User user = new User(); user.setName(userName); return user; } }
UserServiceTest.java类是对服务层的测试,
package com.isa.demo4.test.unit; import java.util.Arrays; import java.util.List; import junit.framework.Assert; import org.easymock.classextension.EasyMock; import org.junit.Before; import org.junit.Test; import org.springside.modules.utils.ReflectionUtils; import com.isa.demo4.dao.UserDao; import com.isa.demo4.domain.User; import com.isa.demo4.service.UserServiceImpl; import com.isa.demo4.test.data.TestData; public class UserServiceTest extends Assert { private UserServiceImpl userServiceImpl; private UserDao userDao; @Before public void setUp() { userServiceImpl=new UserServiceImpl(); userDao = EasyMock.createNiceMock(UserDao.class); ReflectionUtils.setFieldValue(userServiceImpl, "userDao", userDao); } @Test public void loadAllUser(){ User user1=TestData.getRandomUser(); user1.setId(1L); User user2=TestData.getRandomUser(); user2.setId(2L); //录制脚本 List<User> testUsers=Arrays.asList(user1,user2); EasyMock.expect(userDao.getAll()).andReturn(testUsers); EasyMock.replay(userDao); List<User> result=userServiceImpl.getAllUsers(); assertEquals(testUsers, result); } @Test public void deleteUser(){ userServiceImpl.deleteUserById(1L); } @Test public void loadUserExist() { User user=TestData.getRandomUser(); user.setId(1L); //录制脚本 EasyMock.expect(userDao.get(user.getId())).andReturn(user); EasyMock.replay(userDao); User result=userServiceImpl.getUserById(user.getId()); assertEquals(user.getName(), result.getName()); } }
这里使用了EasyMock,听名字就知道干什么的了,而且上手容易。具体教程可参见http://macrochen.iteye.com/blog/298032。
这里还有一个同样来自于springside的对映射类的简单测试,HibernateMappingTest.java
package com.isa.demo4.test.unit; 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 com.isa.demo4.test.base.CommonTestCase; @ContextConfiguration(locations = { "classpath:demo4/applicationContext.xml" }) public class HibernateMappingTest extends CommonTestCase { private static Logger logger=LoggerFactory.getLogger(HibernateMappingTest.class); @Autowired private SessionFactory sessionFactory; @SuppressWarnings("unchecked") @Test public void testColumnMapping() throws Exception { 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(); } } }
三个测试类执行一下,可爱的green bar :-D
6.怎么没有看见配置文件,这里就省略了,跟demo3差不多。具体参见附件。
7.最后再啰嗦一下,学习是有技术曲线的,先从简单开始,一步步走,踏踏实实,就如每个语言的第一个程序都是helloworld一样。其实生活也一样,在it行业,开始的时候都是很惨的可以说,加班,加班,无穷的免费加班,羡慕别人的悠闲,羡慕别人的高薪,其实高手都是从菜鸟过来的。记得明朝那些事儿的朱重八,心的坚强才是可怕的。作为一个coder,不仅要有一颗坚强的心,也需要一个健康的体魄。又扯远了。算是记录最近的一些感受吧!