不能回家,感觉无聊透顶,乱写点东西,打发时间
今天无意中在springframework中见到这个函数
this.getHibernateTemplate().findByExampl(Object obj)
以前没注意到,看了一下源码,原来就是Criteria中的Example.
以前代码如下
package martin.xpost.model;
import martin.xpost.hibernate.UserDAO;
import martin.xpost.util.ListUtil;
import java.util.ArrayList;
import java.util.List;
/**
* @author martin
*/
public class User {
private String id;
private String userName;
private String password;
private String realName;
private String email;
private List blogEntities = new ArrayList();
private List rolls = new ArrayList();
private UserDAO service;
//getter and setter
public void save() {
this.service.save(this);
}
public void remove() {
this.service.remove(this);
}
public boolean valid() {
return ListUtil.isNotEmpty(this.service.valid(this));
}
}
import martin.xpost.hibernate.UserDAO;
import martin.xpost.util.ListUtil;
import java.util.ArrayList;
import java.util.List;
/**
* @author martin
*/
public class User {
private String id;
private String userName;
private String password;
private String realName;
private String email;
private List blogEntities = new ArrayList();
private List rolls = new ArrayList();
private UserDAO service;
//getter and setter
public void save() {
this.service.save(this);
}
public void remove() {
this.service.remove(this);
}
public boolean valid() {
return ListUtil.isNotEmpty(this.service.valid(this));
}
}
DAO代码如下
package martin.xpost.hibernate.impl;
import martin.xpost.hibernate.UserDAO;
import martin.xpost.model.User;
import martin.xpost.util.ListUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;
/**
* @author martin
*/
public class HibernateUserDAO extends HibernateDaoSupport implements UserDAO {
public void save(User user) {
this.getHibernateTemplate().merge(user);
}
public void remove(User user) {
this.getHibernateTemplate().delete(user);
}
public boolean valid(final User user) {
List list = (List) this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws org.hibernate.HibernateException {
Query query = session.createQuery("from User user where user.userName=:userName and user.password=:password");
query.setString("userName", user.getUserName());
query.setString("password", user.getPassword());
return query.list();
}
});
return ListUtil.isEmpty(list);
}
}
import martin.xpost.hibernate.UserDAO;
import martin.xpost.model.User;
import martin.xpost.util.ListUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;
/**
* @author martin
*/
public class HibernateUserDAO extends HibernateDaoSupport implements UserDAO {
public void save(User user) {
this.getHibernateTemplate().merge(user);
}
public void remove(User user) {
this.getHibernateTemplate().delete(user);
}
public boolean valid(final User user) {
List list = (List) this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws org.hibernate.HibernateException {
Query query = session.createQuery("from User user where user.userName=:userName and user.password=:password");
query.setString("userName", user.getUserName());
query.setString("password", user.getPassword());
return query.list();
}
});
return ListUtil.isEmpty(list);
}
}
重构
一:修改User的valid函数
public boolean valid() {
return ListUtil.isNotEmpty(this.service.find(this));
}
return ListUtil.isNotEmpty(this.service.find(this));
}
DAO中只负责和数据源打交道,我们将逻辑全部转到model里面(就是逻辑太简单了o_o)。
二:修改DAO代码
package martin.xpost.hibernate.impl;
import martin.xpost.hibernate.UserDAO;
import martin.xpost.model.User;
import martin.xpost.util.ListUtil;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;
/**
* @author martin
*/
public class HibernateUserDAO extends HibernateDaoSupport implements UserDAO {
public void save(User user) {
this.getHibernateTemplate().merge(user);
}
public void remove(User user) {
this.getHibernateTemplate().delete(user);
}
public List find(User user) {
return this.getHibernateTemplate().findByExample(user);
}
}
import martin.xpost.hibernate.UserDAO;
import martin.xpost.model.User;
import martin.xpost.util.ListUtil;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;
/**
* @author martin
*/
public class HibernateUserDAO extends HibernateDaoSupport implements UserDAO {
public void save(User user) {
this.getHibernateTemplate().merge(user);
}
public void remove(User user) {
this.getHibernateTemplate().delete(user);
}
public List find(User user) {
return this.getHibernateTemplate().findByExample(user);
}
}
这样就简单多了:)
Test 一下:
package martin.xpost;
import martin.xpost.model.User;
/**
* @author martin
*/
public class UserTest extends TestBase {
public void testAdd() {
User user = (User) this.getBean("user");
user.setUserName("martin");
user.setPassword("1234");
user.setRealName("martin xus");
user.setEmail("martin.xus@gmail.com");
user.save();
assertNotNull(user.getId());
}
public void testValid() {
User user = (User) this.getBean("user");
user.setUserName("martin");
user.setPassword("1234");
assertTrue(user.valid());
}
public void testRemove() {
User user = (User) this.getBean("user");
user.setUserName("martin");
user.setPassword("1234");
user.remove();
assertNull(user.getId());
}
}
import martin.xpost.model.User;
/**
* @author martin
*/
public class UserTest extends TestBase {
public void testAdd() {
User user = (User) this.getBean("user");
user.setUserName("martin");
user.setPassword("1234");
user.setRealName("martin xus");
user.setEmail("martin.xus@gmail.com");
user.save();
assertNotNull(user.getId());
}
public void testValid() {
User user = (User) this.getBean("user");
user.setUserName("martin");
user.setPassword("1234");
assertTrue(user.valid());
}
public void testRemove() {
User user = (User) this.getBean("user");
user.setUserName("martin");
user.setPassword("1234");
user.remove();
assertNull(user.getId());
}
}
第三个没有通过:(看了一下log,原来this.getHibernateTemplate().delete(user)中user必须是持久层对象,也就是说user的id不能为空。
再修改:首先判断user.getId()是否为空,如果不为空,直接remvoe,否则,先find然后在remove.
public void remove(User user) {
if (null != user.getId() && !"".equals(user.getId().trim())) {
this.getHibernateTemplate().delete(user);
} else {
List list = find(user);
if (ListUtil.isNotEmpty(list))
for (Object o : list) {
this.getHibernateTemplate().delete(o);
}
}
}
if (null != user.getId() && !"".equals(user.getId().trim())) {
this.getHibernateTemplate().delete(user);
} else {
List list = find(user);
if (ListUtil.isNotEmpty(list))
for (Object o : list) {
this.getHibernateTemplate().delete(o);
}
}
}
OK,测试通过!