整合时建议一步步来,把每一个框架都做通,然后在处理整合的问题整合环境是struts2.2和hibernate4.2和spring4.2,jdk1.6
第一步:列举需要的jar包:
c3p0数据库jar包,当然也可以是使用spring提供的一个c3p0包:
数据库驱动,这里采用mysql数据库:mysql-connector-java-5.1.28-bin.jar
struts2jar包,注意有重复的jar,要删除,javassist,一般保留版本高的:
整合包spring和struts2:struts2-spring-plugin-2.3.28
做分布整合时,建议先不要导入,不然struts会无法起作用
aop联盟:
最后要注意的是如果使用注解开发,struts需要导入注解包:
第二步:各个层次的代码接口就不在写了:
po类:
public class User {
private Integer id;
private String name;
private Double money;
sett..
getter..
}
hibernate映射文件:
dao层:
package com.leige.dao.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import com.leige.dao.UserDao;
import com.leige.domain.User;
public class UserDaoImpl implements UserDao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void addUser(User user) {
//保存对象
hibernateTemplate.save(user);
}
@Override
public void update(User user) {
this.hibernateTemplate.update(user);
}
@Override
public User findById(Integer id) {
// TODO Auto-generated method stub
return this.hibernateTemplate.get(User.class, id);
}
}
package com.leige.service.impl;
import javax.ejb.FinderException;
import com.leige.dao.UserDao;
import com.leige.dao.impl.UserDaoImpl;
import com.leige.domain.User;
import com.leige.service.UserService;
public class UserServiceImpl implements UserService {
UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/* 实现注册业务*/
@Override
public void regist(User form) {
userDao.addUser(form);
}
//实现转账业务
@Override
public void transfer(Integer from, Integer to, Double money) {
/*调用dao层实现转账业务:
*
*
* */
User fr=userDao.findById(from);
fr.setMoney(fr.getMoney()-money);
userDao.update(fr);
User tt=userDao.findById(to);
//int i=5/0;
tt.setMoney(tt.getMoney()+money);
userDao.update(tt);
}
}
package com.leige.web.action;
import com.leige.domain.User;
import com.leige.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/* service由spring注入*/
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
private User user;
//转账业务所需参数
private Integer userTo;
private Integer userFrom;
private Double money;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getUserTo() {
return userTo;
}
public void setUserTo(Integer userTo) {
this.userTo = userTo;
}
public Integer getUserFrom() {
return userFrom;
}
public void setUserFrom(Integer userFrom) {
this.userFrom = userFrom;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public String add(){
userService.regist(user);
return "success";
}
public String pay(){
/*System.out.println("from----"+userFrom);
System.out.println("to----"+userTo);
System.out.println("money----"+money);*/
/* 调用service层实现转账*/
userService.transfer(userFrom, userTo, money);
return "success";
}
}
第三步各个层次的配置文件配置,详细解释都在配置文件中:
web.xml
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
SSh
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
Struts2.0
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
Struts2.0
/*
index.jsp
struts2.的配置文件:
/WEB-INF/jsp/error.jsp
/show.jsp
com.mysql.jdbc.Driver
jdbc:mysql:///test
root
org.hibernate.dialect.MySQLDialect
true
update
4
10
3
4000
3
100
业务表单:
org.hibernate.dialect.MySQLDialect
true
true
update
none
dao层继承
HibernateDaoSupport
package com.leige.dao.impl;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import com.leige.dao.UserDao;
import com.leige.domain.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void addUser(User user) {
//保存对象
super.getHibernateTemplate().save(user);
}
@Override
public void update(User user) {
super.getHibernateTemplate().update(user);
}
@Override
public User findById(Integer id) {
return super.getHibernateTemplate().get(User.class, id);
}
}