SSH2的集成
来源:互联网 发布:数据分析师报考 编辑:程序博客网 时间:2024/06/10 14:53
1.集成思想
首先进行spring和hibernate的集成,集成后通过junit测试两者是否集成成功,再进行struts2和两者集成,验证是否成功。
2.示范项目结构图
3.完成spring配置文件beans.xml
<span style="font-size:12px;"><span style="background-color: rgb(255, 255, 51);"><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"></span><context:component-scan base-package="cn.itcast"/><!--配置数据源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="org.gjt.mm.mysql.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/><property name="user" value="root"/><property name="password" value="root"/><!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --><property name="initialPoolSize" value="1"/><!--连接池中保留的最小连接数。--><property name="minPoolSize" value="1"/><!--连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="300"/><!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="60"/><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="5"/><!--每60秒检查所有连接池中的空闲连接。Default: 0 --><property name="idleConnectionTestPeriod" value="60"/></bean></span><span style="font-size:12px;"><span style="white-space:pre"></span><!--将数据源配置进sessionFactory中去--><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>cn/itcast/bean/Employee.hbm.xml</value> <!--将要用到的数据库映射文件--> </list></property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false </value> </property></bean><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!--使用基于注解方式配置事务 --><tx:annotation-driven transaction-manager="txManager"/></beans></span>
4.进行junit测试
但是进行junit测试之前,需要进行一些配置,设计一个实体类Employee.java,对应一个数据库表
<span style="font-size:12px;">package cn.itcast.bean;public class Employee {private String username;private String password;private Gender gender = Gender.MAN;public Employee(){}public Employee(String username, String password) {this.username = username;this.password = password;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Gender getGender() {return gender;}public void setGender(Gender gender) {this.gender = gender;}}</span>这其中的性别用到了枚举,所以配置一个枚举类Gender.java<span style="font-size:12px;">package cn.itcast.bean;/** * 性别 */public enum Gender {MAN,WOMEN;}</span>
配置实体类的映射文件Employee.hbm.xml<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.bean"> <class name="Employee"> <id name="username" length="20"/> <property name="password" length="20" not-null="true"/> <property name="gender" not-null="true" length="5"> <type name="org.hibernate.type.EnumType"> <param name="enumClass">cn.itcast.bean.Gender</param><!-- 12为java.sql.Types.VARCHAR常量值,即保存枚举的字面值到数据库。如果不指定type参数,保存枚举的索引值(从0开始)到数据库--> <param name="type">12</param> </type> </property> </class></hibernate-mapping></span>然后写服务层接口,EmployeeService.java
<span style="font-size:12px;">package cn.itcast.service;import java.util.List;import cn.itcast.bean.Employee;public interface EmployeeService {public void save(Employee employee);public void update(Employee employee);public Employee find(String username);public void delete(String... usernames);public List<Employee> list();}</span>
写服务层接口的实现EmployeeServiceBean.java<pre name="code" class="java"><span style="font-size:12px;">package cn.itcast.service.impl;import java.util.List;import javax.annotation.Resource;import org.hibernate.SessionFactory;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import cn.itcast.bean.Employee;import cn.itcast.service.EmployeeService;@Service @Transactionalpublic class EmployeeServiceBean implements EmployeeService{@Resource SessionFactory factory;public void delete(String... usernames) {for(String username : usernames){factory.getCurrentSession().delete(factory.getCurrentSession().load(Employee.class, username));}}@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<Employee> list() {return factory.getCurrentSession().createQuery("from Employee").list();} public void save(Employee employee) {factory.getCurrentSession().persist(employee);}public void update(Employee employee) {factory.getCurrentSession().merge(employee);}}</span>
最后写junit测试类EmployeeTest.java,测试前必须导入MySQL驱动包,否则报错
<pre name="code" class="java"><span style="font-size:12px;">package junit.test;import java.util.List;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.itcast.bean.Employee;import cn.itcast.bean.Gender;import cn.itcast.service.EmployeeService;public class EmployeeTest {private static EmployeeService employeeService;@BeforeClasspublic static void setUpBeforeClass() throws Exception {try {ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");employeeService = (EmployeeService)act.getBean("employeeServiceBean");} catch (RuntimeException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Testpublic void save(){employeeService.save(new Employee("liming", "123456"));}@Testpublic void update(){Employee employee = employeeService.find("liming");employee.setGender(Gender.WOMEN);employeeService.update(employee);}@Testpublic void delete(){employeeService.delete("liming");}@Testpublic void list(){List<Employee> ems = employeeService.list();for(Employee em: ems)System.out.println(em.getPassword());}@Testpublic void find(){Employee em = employeeService.find("liming");System.out.println(em.getPassword());}}</span>
5.测试成功后,进行struts2的集成
首先写两个Action类,EmployeeAction.java,展示人员信息;EmployeeManageAction.java,展示添加人员信息
package cn.itcast.action;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionContext;import cn.itcast.service.EmployeeService;@Controller // employeeActionpublic class EmployeeAction { @Resource EmployeeService employeeService; public String execute(){ActionContext.getContext().put("employees", employeeService.list());return "list";}}
--------------------------------------------------------------------------------------------------------package cn.itcast.action;import javax.annotation.Resource;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionContext;import cn.itcast.bean.Employee;import cn.itcast.service.EmployeeService;@Controller @Scope("prototype")public class EmployeeManageAction {@Resource EmployeeService employeeService;private Employee employee;public Employee getEmployee() {return employee;}public void setEmployee(Employee employee) {this.employee = employee;}public String addUI(){return "add";}public String add(){employeeService.save(employee);ActionContext.getContext().put("message", "保存成功");return "message";}}
---------------------------------------------------------------------------------------------------
写struts2配置文件struts.xml
<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 默认的视图主题 --> <constant name="struts.ui.theme" value="simple" /> <constant name="struts.objectFactory" value="spring" /> <package name="employee" namespace="/employee" extends="struts-default"> <action name="list" class="employeeAction"> <result name="list">/WEB-INF/page/employee.jsp</result> </action> <action name="manage_*" class="employeeManageAction" method="{1}"> <result name="add">/WEB-INF/page/employeeAdd.jsp</result> <result name="message">/WEB-INF/page/message.jsp</result> </action> </package></struts></span>----------------------------------------------------------------------------------------------------------------------------
配置展示人员信息的视图employee.jsp
<span style="font-size:12px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>员工列表</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> </head> <body> ONGL: <br/> <s:iterator value="#request.employees"> <s:property value="username"/> ,<s:property value="password"/>, <s:property value="gender"/><br/> </s:iterator> <br/> <a href="<s:url action="manage_addUI" namespace="/employee"/>">员工添加</a> <br/> JSTL/EL: <br/> <c:forEach items="${employees}" var="employee"> ${employee.username}, ${employee.password}, ${employee.gender}<br/> </c:forEach> </body></html></span>--------------------------------------------------------------------------------------------------------------------------------
配置展示添加人员信息的视图employeeAdd.jsp<span style="font-size:12px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>员工添加</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> </head> <body> <s:form action="manage_add" namespace="/employee" method="post"> 用户名:<s:textfield name="employee.username"/><br/> 密码:<s:textfield name="employee.password"/><br/> 性别:<s:radio list="#{'MAN':'男','WOMEN':'女'}" listKey="key" listValue="value" name="employee.gender"/> <input type="submit" value="保存"/> </s:form> </body></html></span>---------------------------------------------------------------------------------------------------------------------------------------
配置添加人员信息成功信息展示视图message.jsp<span style="font-size:12px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>结果信息</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> </head> <body> <h1><font color="red"> <s:property value="#request.message"/> </font></h1> </body></html></span><span style="font-size:12px;"></span>
6.SSH2整合,配置web.xml文件
<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value></context-param><!-- 对Spring容器进行实例化 --><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app></span>
7.关于包的引入声明
除了Referenced Libraries中的jar包,是从外部引入的。其他都是MyEclipse中集成的库文件
8.注意事项
当配置实体类时用到枚举类时,例如例子中的性别,需要引入Hibernate对JPA的实现包, hibernate-entitymanager.jar lib\test\log4j.jar slf4j-log4j12.jar
0 0
- SSH2的集成
- 搭建SSH2的集成开发环境
- SSH2集成开发步骤
- SSH2三大框架的集成开发环境搭建
- SSH2三大框架的集成开发环境搭建
- SSH2集成CXF提供WebService服务
- Eclipse的SSH2插件
- ssh2的整合
- SSH2实现的分页
- ssh2的重要代码
- 再谈ssh2的测试
- ssh2的搭建步骤
- SSH2的filter过滤器
- ssh2的整合
- ssh2拓展的使用
- ssh2
- SSH2
- SSH2
- JNI配置信息保存
- 不解决这六个问题,农商行怎么去玩大数据?
- UVA 1152 4 Values whose Sum is 0
- windows下python配置numpy、matplotlib、scipy
- 使用自定义对话框和Activity对话框的区别
- SSH2的集成
- c_str()方法【string2char *】
- ios编程——使用抽屉MMDrawerController和系统手势interactivePopGestureRecognizer冲突的解决方法
- django admin
- zookeeper3.4.5自动清理日志
- 怎么使点击一个popupwindow窗口之外的区域popupwindow消失呢?
- 关于用MDK定义无法跳转的问题
- PopupWindow 用法 PopupWindow不消失 解决方法
- linux启动svn