读书人

Struts2+Spring3+Hibernate3.6调整之一

发布时间: 2012-11-23 00:03:43 作者: rapoo

Struts2+Spring3+Hibernate3.6整合之一:用户管理




前些天弄了一个Struts2+Spring3+iBatis的整合,第一部分:http://blog.csdn.net/ruantao1989/article/details/8143899第二部分:http://blog.csdn.net/ruantao1989/article/details/8143979第三部分:http://blog.csdn.net/ruantao1989/article/details/8144129



这回使用上次的程序,把iBatis剔除出去,ibatis的功能都用Hibernate实现,其实道理一样,就是具体配置牵扯到不少东西。

一共是两组action和service:

用户登录、注销是一个流程,用户管理是另一个流程,还和上次ssi一样。

这回时间紧,就简单罗列一下,相当于笔记,过些天要记得来补……



整合Hibernate和iBatis的主要区别是:

1.sessionFactory和hibernateTemplate代替iBatis的sqlMapClient

2.Hibernate可以用annotation管理实体

3.Hibernate的DaoImpl中要通过聚合的HibernateTemplate配合HQL实现SQL的具体操作

4.HibernateTemplateL的一些操作返回值和sqlMapClient不一样,比如不返回更改的数量

5.web.xml中,Hibernate需要OpenSessionInViewFilter


Struts2+Spring3+Hibernate3.6调整之一:用户管理




0.建表语句

public class UserDaoImpl implements IUserDao {private HibernateTemplate hibernateTemplate;public HibernateTemplate getHibernateTemplate() {return hibernateTemplate;}public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}//下边是HQL的具体实现public List<User_SSH> queryUser() {List<User_SSH> list = (List<User_SSH>)hibernateTemplate.find("from User_SSH");for(int i=0;i<list.size();i++){System.out.println(list.get(i));}return list;}public boolean deleteUser(int id) {User_SSH user = (User_SSH) hibernateTemplate.load(User_SSH.class,new Integer(id));  hibernateTemplate.delete(user);return true;}public void insertUser(User_SSH u) {System.out.println("ManagerService=>insertUser=>"+u);hibernateTemplate.save(u);}public boolean updateUser(User_SSH u) { hibernateTemplate.update(u); return true;}public List<User_SSH> queryUserName(String name) {String hql = "from User_SSH u where u.username IN ?";List<User_SSH> users = new ArrayList<User_SSH>();users = (List<User_SSH>)hibernateTemplate.find(hql,name); return users;}}




=========================================================================================

遇到好几个bug,简单说下:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
这是tomcat发出的警告,原因是“log4j的配置文件”没有出现在项目中

从之前项目拷贝一个过来就行了



org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: name of: vo.User_SSH [from vo.User_SSH t where t.name IN ?]; nested exception is org.hibernate.QueryException: could not resolve property: name of: vo.User_SSH [from vo.User_SSH t where t.name IN ?]

这个是因为HQL语句中字段和表中不对应,

我这儿把主键改成id了,hql里没改过来


























读书人网 >Web前端

热点推荐