博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2+spring+hibernate整合步骤
阅读量:7154 次
发布时间:2019-06-29

本文共 9298 字,大约阅读时间需要 30 分钟。

 

引用struts2、hibernate、spring所需jar包 

 

struts-core-2.x.x.jar  ----struts核心包 

xwork-core-2.x.x.jar   -----身体ruts在其撒很难过构建 

ognl-2.6.x.jar     ----对象导航语言 

freemarker-2.3.x.jar   ------struts2的ui标签的模板使用 

commons-fileupload-1.2.x.jar    ----文件上传组件 2.1.6版本后需加入此文件 

struts-spring-plugin-2.x.x.jar   ---用于struts2继承spring的插件 

 

hibernate核心安装包下的(下载路径:http://www.hibernate.org/ ,点击Hibernate Core 右边的download) 

hibernate2.jar 

lib\bytecode\hibernate-cglib-repack-2.1_3.jar 

lib\required\*.jar 

hibernate安装包下的(下载路径:http://www.hibernate.org/;点击Hibernate Annotations 右边的下载) 

hibernate-annotations.jar 

lib\ejb3-persistence.jar、hibernate-commons-annotations.jar 

hibernate针对JPA的实现包(下载路径:http://www.hibernate.org/ ,点击Hibernate Entitymanager右边下载)

 hibernate-entitymanager.jar 

lib\test\log4j.jar、  slf4j-log4j12.jar 

 

spring安装包下的 

dist\spring.jar 

lib\c3p0\c3p0-0.9.1.2.jar 

lib\aspecti\aspectjweaver.jar 

aspectjrt.jar 

lib\colib\cglib-nodep-2.1_3.jar 

lib\j2ee\common-annotations.jar 

vlib\log4j\log4j-1.2.15.jar 

lib\jakarta-commons\commons_loggin.jar 

 

数据库驱动包 

 

引用

创建mysql数据库ssh 设置编码为utf-8 语句: 

create database ssh character set 'utf8' collate 'utf8_general_ci' 

 

引用

1.先整合spring和hibernate 

  *将spring和hibernate的jar包放入lib下; 

  *创建spring的beans.xml配置文件 

 

 

 

配置bean.xml

1.
2.
11. 12.
13.
16.
17. 18. 19.
20.
21.
23.
24.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41. 42.
44.
45.
46.
47.
com/yss/bean/Employee.hbm.xml
48.
49.
50.
51.
52. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 53. hibernate.hbm2ddl.auto=update 54. hibernate.show_sql=true 55. hibernate.format_sql=false 56.
57.
58.
59. 60.
61.
62.
63.
64. 65.
66.
67.

 

   *配置hibernate的model.hbm.xml和创建model类 

   *创建service 

 

EmployeeService .java: 

public interface EmployeeService { public boolean save(Employee employee); public boolean update(Employee employee); public Employee find(String username); public boolean delete(String... username);//表示可变参数  public List
findAll(); }

 

service实现类 EmployeeServiceBean .java: 

import java.util.List; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.hibernate.SessionFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.yss.bean.Employee; import com.yss.service.EmployeeService; /**  * @author qing 默认bean名称 employeeServiceBean  *@Service @Transactional 注入service和开启事务 */ @Service  @Transactional  public class EmployeeServiceBean implements EmployeeService { private static Logger logger = Logger.getLogger(Employee.class); /**      * 注入sessionFactory */     @Resource SessionFactory factory;  public boolean delete(String... usernames) { try { for (String username : usernames) {                 factory.getCurrentSession().delete(                          factory.getCurrentSession().load(Employee.class,                                 username));              }          } catch (Exception e) {             logger.error(e.getMessage());  return false;         }  return true;     }  /*      * (non-Javadoc)      *       * @see com.yss.service.EmployeeService#find(com.yss.bean.Employee)      * 此标注表示不需要事务处理 */     @Transactional(propagation = Propagation.NOT_SUPPORTED)  public Employee find(String username) { return (Employee) factory.getCurrentSession().get(Employee.class,                 username);      }      @SuppressWarnings("unchecked")      @Transactional(propagation = Propagation.NOT_SUPPORTED)  public List
findAll() { return factory.getCurrentSession().createQuery("from Employee emp") .list(); } public boolean save(Employee employee) { try { factory.getCurrentSession().persist(employee);// .save(employee);// // 获取已经开好的Session } catch (Exception e) { logger.error(e.getMessage()); return false; } return true; } public boolean update(Employee employee) { try { factory.getCurrentSession().merge(employee);// 类似于saveOrUpdate()方法 } catch (Exception e) { logger.error(e.getMessage()); return false; } return true; } }

 

 

 *新建测试类   EmployeeTest.java

Java代码

1.public class EmployeeTest { 2.    private static EmployeeService employeeService; 3.  4.    @BeforeClass  5.    public static void setUpBeforeClass() throws Exception { 6.        try { 7.            ApplicationContext context = new ClassPathXmlApplicationContext( 8.                    "beans.xml");  9.            employeeService = (EmployeeService) context  10.                    .getBean("employeeServiceBean");  11.        } catch (Exception e) { 12.            System.out.println(e.getMessage());  13.        }  14.    } 15.  16.    @Test  17.    public void createTable() { 18.        //new ClassPathXmlApplicationContext("beans.xml");  19.    };  20.      21.    @Test  22.    public void save() { 23.        boolean result=employeeService.save(new Employee("long","long")); 24.        if (result) { 25.            System.out.println("保存成功。。。。。");  26.        }else{ 27.            System.out.println("保存出错....");  28.        }  29.    };  30.  31.    @Test  32.    public void delete() { 33.        boolean result=employeeService.delete("long"); 34.        if (result) { 35.            System.out.println("删除成功。。。。。");  36.        }else{ 37.            System.out.println("删除出错....");  38.        }  39.    };  40.  41.    @Test  42.    public void update() { 43.        boolean result=employeeService.update((new Employee("qing","long"))); 44.        if (result) { 45.            System.out.println("更新成功。。。。。");  46.        }else{ 47.            System.out.println("更新出错....");  48.        }  49.    };  50.  51.    @Test  52.    public void findAll() { 53.        List
elist=employeeService.findAll(); 54. Iterator itor=elist.iterator(); 55. while(itor.hasNext()){ 56. Employee emp=(Employee)itor.next(); 57. System.out.println(emp.getPassword()); 58. } 59. }; 60. 61. @Test 62. public void find(){ 63. Employee employee=employeeService.find("qing"); 64. System.out.println(employee.getPassword()); 65. } 66.}

 

 *ok  没问题spring和hibernate整合完毕 

 *再将struts2所需包加入lib中 

 *创建struts.xml配置文件 

1.
2. 5.
6.
7.
8. 9.
10.
11.
12. /WEB-INF/feapp/employee.jsp 13.
14.
15. 16.
17.
18. /WEB-INF/feapp/employeeadd.jsp 19.
20.
21. /WEB-INF/feapp/result.jsp 22.
23.
24.
25.

*在web.xml中加入 

1.
2.
6. 7.
10.
11.
contextConfigLocation
12.
classpath:beans.xml
13.
14.
15.
16.
org.springframework.web.context.ContextLoaderListener
17.
18. 19.
20.
21.
struts2
22.
23. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
24.
25.
26.
struts2
27.
/*
28.
29. 30.
31.
index.jsp
32.
33.

*创建相关jsp和action  

1.@Controller  @Scope("prototype")  2.public class EmployeeManagerAction extends ActionSupport { 3.    @Resource EmployeeService employeeService;  4.    private Employee employee; 5.          6.    public String addUI(){ 7.        //System.out.println("user come");  8.        return SUCCESS; 9.    }  10.      11.    public String add(){ 12.        //System.out.println("--------------");  13.        boolean result=employeeService.save(employee); 14.        //ActionContext.getContext().put("genders", Gender.values());  15.        if(result){ 16.            ActionContext.getContext().put("message", "保存成功!");  17.        }else{ 18.            ActionContext.getContext().put("message", "保存失败!");  19.        }  20.        return "message"; 21.    }  22.  23.    public Employee getEmployee() { 24.        return employee; 25.    }  26.  27.    public void setEmployee(Employee employee) { 28.        this.employee = employee; 29.    }  30.}

 

 

转载于:https://www.cnblogs.com/stephen-vv/archive/2012/04/07/strust2-hibernet-spring.html

你可能感兴趣的文章
关于斯特林数
查看>>
关于逆元的求法
查看>>
mongo索引
查看>>
iOS开发:插件记录
查看>>
一次sql注入中转
查看>>
使用pd设计表的 多对多的中间表的设计方式, 有图有真相
查看>>
Spring注解详解
查看>>
OSGI API
查看>>
《转》Objective-C Runtime(4)- 成员变量与属性
查看>>
Rational Rose
查看>>
Webbrowser控件中屏蔽弹出脚本错误对话框
查看>>
华为交换机S5700系列配置通过STelnet登录设备示例
查看>>
error: The following untracked working tree files would be overwritten by merge: 错误解决
查看>>
ASP.NET MVC 3.0 HTML辅助方法
查看>>
linux shell命令行下操作mysql 删除mysql指定数据库下的所有表--亲测成功百分百测试通过--绝对可靠...
查看>>
嵌入式交叉编译环境搭建
查看>>
zabbix 编译
查看>>
linux 网络Socket实战
查看>>
大河奔流的精神 ——俞敏洪
查看>>
HTML5 划线,画圆
查看>>