Spring Learning -10-SSH Integration

Source: Internet
Author: User

1) Introduction of SSH jar file

Struts Core Jar

Hibernate Core Jar

Spring

Core Features

Web-to-Web module support

AOP AOP Support

ORM Support for Hibernate

JDBC/TX JDBC-related packages, transaction-related packages

2) configuration

Xml

Initialize struts and spring features

Struts

Configure the relationship between the request path and the map. Xml shot action

Spring

IOC container Configuration

Bean-base.xml [Shared information]

Bean-dao.xml

Bean-service.xml

Bean-action.xml

3) Development

Entity/dao/service/action '

Instance Code

Project structure

Entity

Package com.cx.entity;/** * Created by Cxspace on 16-8-11. */public class Dept {    private int id;    private String name;    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;    }}

  

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public        "-//hibernate/hibernate mapping DTD 3.0//en"        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

  

Package com.cx.entity;/** * Created by Cxspace on 16-8-11. */public class Employee {    private int id;    Private String EmpName;    private double salary;    Private Dept Dept;    Public Dept getdept () {        return Dept;    }    public void Setdept (Dept Dept) {        this.dept = Dept;    }    Public String Getempname () {        return empname;    }    public void Setempname (String empname) {        this.empname = empname;    }    public int getId () {        return ID;    }    public void setId (int id) {        this.id = ID;    }    Public double getsalary () {        return salary;    }    public void Setsalary (double salary) {        this.salary = salary;    }}

  

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public        "-//hibernate/hibernate mapping DTD 3.0//en"        "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

  

Dao

Package Com.cx.dao;import Com.cx.entity.employee;import Org.hibernate.sessionfactory;import java.io.Serializable;/ * * Created by cxspace on 16-8-11. */public class EmployeeDAO {    private sessionfactory sessionfactory;    public void Setsessionfactory (Sessionfactory sessionfactory) {        this.sessionfactory = sessionfactory;    }    Public Employee FindByID (Serializable id) {        return (Employee) sessionfactory.getcurrentsession (). Get ( EMPLOYEE.CLASS,ID); Primary key Query    }}

  

Service

Package Com.cx.service;import Com.cx.dao.employeedao;import Com.cx.entity.employee;import java.io.Serializable;/** * Created by Cxspace on 16-8-11. */public class EmployeeService {    private EmployeeDAO EmployeeDAO;    public void Setemployeedao (EmployeeDAO employeedao) {        This.employeedao = EmployeeDAO;    }    Public Employee FindByID (Serializable id) {        Employee emp = Employeedao.findbyid (ID);        return emp;    }}

  

Action

Package Com.cx.action;import Com.cx.entity.employee;import Com.cx.service.employeeservice;import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.actionsupport;import java.util.Map;/** * Created by Cxspace on 16-8-11. */public class Employeeaction extends actionsupport{    private employeeservice employeeservice;    public void Setemployeeservice (EmployeeService employeeservice) {        this.employeeservice = EmployeeService;    }    @Override public    String execute () throws Exception {        int empId = 1;        Employee employee = Employeeservice.findbyid (empId);        Get request        map<string, object> request = (map<string, object>) Actioncontext.getcontext (). Get (" Request ");        Request.put ("EMP", employee);        return SUCCESS;}    }

 

Configuration

<?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:p= "http://www.springframework.org/schema/p" Xmlns:contex t= "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 HT Tp://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP htt P://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPR Ingframework.org/schema/tx/spring-tx.xsd "> <bean id=" employeeaction "class=" Com.cx.action.EmployeeAction " Scope= "Prototype" > &LT;PROperty name= "EmployeeService" ref= "EmployeeService" ></property> </bean> </beans> 

<?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:p= "http://www.springframework.org/schema/p" Xmlns:contex t= "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 HT Tp://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP htt P://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPR Ingframework.org/schema/tx/spring-tx.xsd "> <!--Data source object C3P0 Connection pool--<bean id=" DataSource "class=" Com.mchange.v2.c3p0.ComboPooledDataSourcE "> <property name=" driverclass "value=" Com.mysql.jdbc.Driver "></property> <property name= "Jdbcurl" value= "jdbc:mysql:///learnhibernate" ></property> <property name= "user" value= "root" ></ property> <property name= "password" value= "33269456.cx" ></property> <property name= "Initia Lpoolsize "value=" 3 "></property> <property name=" maxpoolsize "value=" ten "></property> &L T;property name= "maxstatements" value= "></property> <property name=" Acquireincrement "value=" 2 "> </property> </bean> <bean id= "sessionfactory" class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <!--Connection pool-<property name=" Datasour Ce "ref=" DataSource "></property> <!--hibernate commonly configured--<property name=" Hibernateproperties " > <props> <prop key= "hibernate.dialECT ">org.hibernate.dialect.MySQL5Dialect</prop> <prop key=" Hibernate.show_sql ">true</prop > <prop key= "Hibernate.hbm2ddl.auto" >update</prop> </props> </pro                perty> <!--load mapping configuration-<property name= "Mappinglocations" > <list> <value>classpath:com/cx/entity/*.hbm.xml</value> </list> </property> &lt ;/bean> <!--transaction Configuration--<bean id= "Txmanager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref="        Sessionfactory "></property> </bean> <tx:advice id=" Txadvice "transaction-manager=" Txmanager "> <tx:attributes> <tx:method name= "*" read-only= "false"/> </tx:attributes> &lt ;/tx:advice> <!--AOP configuration-<aop:config> <aop:pointcut id= "PT "expression=" Execution (* com.cx.service.*.* (..)) " /> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "pt"/> </aop:config></beans>

<?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:p= "http://www.springframework.org/schema/p" Xmlns:contex t= "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 HT Tp://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP htt P://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPR Ingframework.org/schema/tx/spring-tx.xsd "> <bean id =" EmployeeDAO "class=" Com.cx.dao.EmployeeDao "> &lt ;p roperty name= "SessionfactoRy "ref=" sessionfactory "></property> </bean></beans> 

<?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:p= "http://www.springframework.org/schema/p" Xmlns:contex t= "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 HT Tp://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP htt P://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/tx HTTP://WWW.SPR Ingframework.org/schema/tx/spring-tx.xsd "> <bean id =" EmployeeService "class=" Com.cx.service.EmployeeService "> <property name="EmployeeDAO "ref=" EmployeeDAO "></property> </bean></beans> 

  

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts public        "-//apache software foundation//dtd struts Configuration 2.3//en"        "/http Struts.apache.org/dtds/struts-2.3.dtd "><struts>    <package name=" emp "extends=" Struts-default ">        <action name= "show" class= "Employeeaction" method= "execute" >            <result name= "Success" >/ index.jsp</result>        </action>    </package></struts>

  

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "version=" 3.1 "> <!--Configure Spring's Opensessioninview mode" Purpose: JSP page access Lazy Loading Data "-<filter> <filter-name>OpenSessionInView</filter-name> <filter-class&gt ;org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class> </filter> < Filter-mapping> <filter-name>OpenSessionInView</filter-name> <url-pattern>*.action</ Url-pattern> </filter-mapping> <!--struts configuration--<filter> <filter-name>struts2&lt ;/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> & Lt;/filter-mapping> <!--spring Configuration-<context-param> <param-name>contextconfiglocation&lt ;/param-name> <param-value>classpath:bean*.xml</param-value> </context-param> <listen Er> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </l Istener> </web-app>

  

<%--  Created by IntelliJ idea.  User:cxspace  date:16-8-11 Time  : PM 9:16 To change this  template use File | Settings | File templates.--%><%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>

  

Spring Learning -10-SSH Integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.