完成之后目录结构
然后是选择struts的包,根据需要将struts的包添加进来,一般需要添加的包如下
Struts Core libraries
Struts必须包
StrutsSpring libraries
Spring支持包
如果选择next进入下一步,里面是指定spring配置文件的位置,
一般都不需要修改直接点finish就可以了,这样就使你的工程添加进了spring的支持
然后点finish,完成hibernate的配置
项目结构如下
package com.xwd.action;
import java.util.List;
import com.xwd.entity.User;
import com.xwd.service.TestService;
public class TestAction {
private User user;
private TestService testservice;
private List list;
public String execute(){
int save=testservice.add(user);
System.out.println("save:"+save);
list=testservice.findAll(User.class);
return "success";
}
public String delete(){
testservice.delete(User.class,user.getId());
System.out.println("delete:"+1);
//锟斤拷询锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷,锟斤拷示页锟斤拷
list=testservice.findAll(User.class);
return "success";
}
public String update(){
int update = testservice.update(user);
System.out.println("update:"+update);
//锟斤拷询锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷,锟斤拷示页锟斤拷
list = testservice.findAll(User.class);
return "success";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public TestService getTestservice() {
return testservice;
}
public void setTestservice(TestService testservice) {
this.testservice = testservice;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
package com.xwd.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class TestDao {
private SessionFactory sf;
public int insert(Object o){
try {
Session session=sf.getCurrentSession();
session.save(o);
return 1;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
public int delete(Class cls,Serializable id){
try {
Session session=sf.getCurrentSession();
Object o = session.get(cls,id);
session.delete(o);
return 1;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
public int update(Object o){
try {
Session session=sf.getCurrentSession();
session.update(o);
return 1;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
public List getAll(Class cls){
Session session=sf.getCurrentSession();
Query query = session.createQuery("from "+cls.getSimpleName());
System.out.println("TestDao getAll cls.getSimpleName()"+cls.getSimpleName());
return query.list();
}
public SessionFactory getSf() {
return sf;
}
public void setSf(SessionFactory sf) {
this.sf = sf;
}
}
package com.xwd.entity;
public class User {
private int id;
private String name;
private int pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPwd() {
return pwd;
}
public void setPwd(int pwd) {
this.pwd = pwd;
}
public User(int id, String name, int pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd=" + pwd +
'}';
}
}
package com.xwd.service;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.xwd.dao.TestDao;
import com.xwd.entity.User;
public class TestService {
private SessionFactory sf;
private TestDao testdao;
public int add(Object o){//添加
System.out.println("我在TestService类中,添加的方法运行了"+o);
return testdao.insert(o);
}
public int delete(Class cls,Serializable id){//删除
System.out.println("我在TestService类中,删除的方法运行了");
return testdao.delete(cls, id);
}
public int update(Object o){//修改
System.out.println("我在TestService类中,修改的方法运行了"+o);
return testdao.update(o);
}
public List findAll(Class cls){//查询
System.out.println("我在TestService类中,查询的方法运行了");
return testdao.getAll(cls);
}
public SessionFactory getSf() {
return sf;
}
public void setSf(SessionFactory sf) {
this.sf = sf;
}
public TestDao getTestdao() {
return testdao;
}
public void setTestdao(TestDao testdao) {
this.testdao = testdao;
}
}
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
源码下载 点击打开链接https://download.csdn.net/download/sinat_37001576/10375278