关于源码
本课程的代码位置在
Google code
的
appfuse-demos
项目里的
"tutorial-hibernate"
模块里
.
使用以下命令从
Subversion
服务器中检出
:
svn checkout http://appfuse-demos.googlecode.com/svn/trunk/tutorial-hibernate |
注意,若使用
AppFuse 2.0 M5+
,
applicationContext.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.0.xsd">
<bean id="personDao" class="org.appfuse.dao.hibernate.GenericDaoHibernate">
<constructor-arg value="org.appfuse.tutorial.model.Person"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
public void setPersonDao(GenericDao<Person, Long> personDao) {
this.personDao = personDao;
}
package org.appfuse.tutorial.dao;
import java.util.List;
import org.appfuse.dao.BaseDaoTestCase;
import org.appfuse.tutorial.model.Person;
import org.springframework.dao.DataAccessException;
public class PersonDaoTest extends BaseDaoTestCase {
private PersonDao personDao = null;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}
public void testFindPersonByLastName() throws Exception {
List<Person> people = personDao.findByLastName("Raible");
assertTrue(people.size() > 0);
}
<table name='person'>
<column>id</column>
<column>first_name</column>
<column>last_name</column>
<row>
<value>1</value>
<value>Matt</value>
<value>Raible</value>
</row>
</table>
如果重新格式
sample-data.xml
中的
XML,
要确保在标签(
tag
)值内没有换行符(
line breaks
)
.
但令我吃惊的是当
<password>xxxxxxx</password>
被分为三行的情景,
由于
password
标签不再对额外的
tabs
和换行符进行解密
,
现在想用任意用户名登录将不再可能。而允许在数据库连接串里使用任意字符是
DBUnit
的特性
.
|
import org.springframework.dao.DataAccessException;
public void testAddAndRemovePerson() throws Exception {
Person person = new Person();
person.setFirstName("Country");
person.setLastName("Bry");
person = personDao.save(person);
flush();
person = personDao.get(person.getId());
assertEquals("Country", person.getFirstName());
assertNotNull(person.getId());
log.debug("removing person...");
personDao.remove(person.getId());
flush();
try {
personDao.get(person.getId());
fail("Person found in database");
} catch (DataAccessException dae) {
log.debug("Expected exception: " + dae.getMessage());
assertNotNull(dae);
}
}
firstName=Matt
lastName=Raible
也许觉得可以用硬编码来在程序里设置测试值,但是在处理大的对象时,
.properties
文件是一个很好的选择
.
|
Person person = new Person();
person = (Person) populate(person);
package org.appfuse.tutorial.dao;
import org.appfuse.dao.GenericDao;
import org.appfuse.tutorial.model.Person;
import java.util.List;
public interface PersonDao extends GenericDao<Person, Long> {
public List<Person> findByLastName(String lastName);
}
Running org.appfuse.tutorial.dao.PersonDaoTest
INFO - AbstractSingleSpringContextTests.loadContextLocations(179) | Loading context for: classpath*:/applicationContext-*.xml
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.449 sec <<< FAILURE!
---------------------------------------------------------------------
Test set: org.appfuse.tutorial.dao.PersonDaoTest
---------------------------------------------------------------------
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.444 sec <<< FAILURE!
testFindPersonByLastName(org.appfuse.tutorial.dao.PersonDaoTest) Time elapsed: 0.401 sec <<< ERROR!
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
with name 'org.appfuse.tutorial.dao.PersonDaoTest': Unsatisfied dependency expressed
through bean property 'personDao': Set this property value or disable dependency
checking for this bean.
在控制台(控制窗口)中显示错误
在 mvn test 命令里添加参数 -Dsurefire.useFile=false 就可以在控制台( console )中现实测试错误信息。 |
package org.appfuse.tutorial.dao.hibernate;
import java.util.List;
import org.appfuse.dao.hibernate.GenericDaoHibernate;
import org.appfuse.tutorial.model.Person;
import org.appfuse.tutorial.dao.PersonDao;
public class PersonDaoHibernate extends GenericDaoHibernate<Person, Long> implements PersonDao {
public PersonDaoHibernate() {
super(Person.class);
}
public List<Person> findByLastName(String lastName) {
return getHibernateTemplate().find("from Person where lastName=?", lastName);
}
}
<?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.0.xsd">
<bean id="personDao" class="org.appfuse.tutorial.dao.hibernate.PersonDaoHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
在视图过滤器(
View Filter
)中打开会话(
Session
)
如果希望在应用中使用 Hibernate 的延迟载入( lazy-loading )特性, 那么,需要在文件 web.xml 中去掉 lazyLoadingFilter ( 及其 mapping) 的注释 . |