一、新建一个web项目,以 Struts2.3.28 为基础
载入一些jsp提示类库(在tomcat中jsp-api,servlet-api,tomcat-api)
载入一个日志类库(commons-logging-1.1.3,文件在struts2的lib目录下)
打开struts2中的apps,解压一个blank的项目,把其中的lib全部复制到项目根目录下的lib中,并部署到web-inf中的lib里
复制web.xml到项目根目录的web-inf下
在src源文件目录下新建一个struts.xml,内容如下:
/index.jsp
/{1}_{2}_success.jsp
三、载入数据库相关类库
commons-dbcp2-2.1.1;commons-pool2-2.4.2;aspectj1.8.9;aspectjweaver-1.8.9;mysql-connector;
四、在src下再新建一个bean.xml
com.wangy.model.User
org.hibernate.dialect.MySQL5Dialect
true
六、内容如下:
UserDAO:
package com.wangy.dao;
import com.wangy.model.User;
/**
* Created by wangy on 2016/9/16.
*/
public interface UserDAO {
public void save(User u);
}
UserDAOImpl:
package com.wangy.dao.impl;
import com.wangy.dao.UserDAO;
import com.wangy.model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created by wangy on 2016/9/16.
*/
@Component("u")
public class UserDAOImpl implements UserDAO{
private int daoId;
private String daoStatus;
private SessionFactory sessionFactory;
public int getDaoId() {
return daoId;
}
public void setDaoId(int daoId) {
this.daoId = daoId;
}
public String getDaoStatus() {
return daoStatus;
}
public void setDaoStatus(String daoStatus) {
this.daoStatus = daoStatus;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource(name="mySessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void save(User u) {
Session session = sessionFactory.getCurrentSession();
session.save(u);
System.out.println("User is saved!");
}
}
package com.wangy.service;
import com.wangy.dao.UserDAO;
import com.wangy.model.User;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created by wangy on 2016/9/16.
*/
@Component("userService")
public class UserService {
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="u")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void add(User u){
this.userDAO.save(u);
}
}
package com.wangy;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wangy.model.User;
import com.wangy.service.UserService;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Map;
/**
* Created by wangy on 2016/9/15.
*/
public class HelloAction extends ActionSupport
implements ModelDriven,RequestAware {
private User user = new User();
private Map request;
@Override
public String execute() throws Exception {
return ERROR;
}
public String hello() {
return SUCCESS;
}
public String index() {
BeanFactory factory = new ClassPathXmlApplicationContext("bean.xml");
UserService service = (UserService)factory.getBean("userService");
service.add(user);
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
@Override
public void setRequest(Map request) {
this.request = request;
}
}
Model-User
package com.wangy.model;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* Created by wangy on 2016/9/18.
*/
@Entity
public class User {
private int id;
private String username;
private int age;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1、struts2负责连接M和V,在其中充当一个C的作用,即Controller控制器
2、业务逻辑放在UserService中,调用实现了UserDAO的UserDAOImpl这个类来操纵数据库
3、数据库的表与Model中的User这个类通过Hibernate这个框架连接在了一起
4、而Spring提供了IoC容器,并通过Aop的方式,通过单例,极大的简化了这些bean的初始化和调用
八、Hibernate模版注入
新建一个bean,注入sessionFactory
package com.wangy.dao;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created by wangy on 2016/9/17.
*/
@Component
public class AbstractDAO{
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
调用时
this.getHibernateTemplate().save(u);