Recent work, is generally the project team boss to build a good SSH2 framework for US these small rookie use, so oneself also a long time not independent to frame, think about the past or do their own small project or step by step oneself build up slowly, to tell the truth, at that time to set up an environment or feel very laborious, Because always forget to lead some jar package, cause an exception, or some jar package will also create a conflict and throw the exception is the pit father. Now although still an experience only a year of small rookie, some live or want to dry, always feel that the past did not solve good things, now must be taken out to try hackers, so, idle time or the environment set up, by the way can also comb their previous knowledge.
1: Of course, the Guide jar bag;
STRUTS2:
Spring
Hibernate:
As for what these jar packs are, I guess I don't have to explain, everyone knows, SSH2 basic jar package;
There are other jar packages: Struts2-spring-plugin-2.1.8.1.jar (jar package used by struts2-spring integration), C3p0-0.9.2-pre1.jar (using linked pool link database)
2: Add Struts.xml file
<?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> <!--Overwrite Convention- <package Name= "Users" namespace= "/lishun" extends= "Struts-default" > <!--Here the action is called Spring Management, So the class property value here is the ID name of the injected action, followed by the action class that mentions-- <action name= "users" class= "Usersaction" method= " Execute "> <result name=" Success ">/index.jsp</result> </action> </package ></struts>
Configure Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <display-name></display-name> < Welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- The core listener of the struts-<filter> <filter-name>action2</filter-name> <filter-class>or G.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> < Filter-mapping> <filter-name>action2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--to integrate struts with spring;; Be sure not to forget the Guide Struts2-spring-plugin-2.1.8.1.jar pack--<listener> <LISTENER-CLASS>ORG.SPRINGFramework.web.context.contextloaderlistener</listener-class> </listener> <context-param> < ;p aram-name>contextconfiglocation</param-name> <param-value>classpath:application.xml</ Param-value> </context-param></web-app>
Configure Hibernate.cfg.xml
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
Configure Application.xml
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xmlns:jdbc=" Http://www.springframework.org/schema/jdbc "xmlns:context=" http:/ /www.springframework.org/schema/context "xmlns:util=" Http://www.springframework.org/schema/util "xsi: schemalocation= "Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/ Spring-jdbc-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd Http://www.springframEwork.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!-- Automatically scans all the beans under the Com.lishun package, and then injects these beans through annotations--<context:component-scan base-package= "Com.lishun" ></ Context:component-scan> <!--load Properties file to read the database link string--<context:property-placeholder location= "Claspath :D atabaseconnection.properties "/> <bean name=" sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <!--Hibernate to spring Management--<property Nam E= "Configlocation" value= "Classpath:hibernate.cfg.xml" ></property> <property name= "DataSource" > <bean class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <!--front four items are four strings of database links, Here is the database link string read through the properties file--<property name= "Jdbcurl" value= "${jdbcurl}" ></property> <property name= "user" value= "${user}" ></property> <propeRty name= "Password" value= "${password}" ></property> <property name= "Driverclass" value= "${drive Rclass} ></property> <!--other Configurations-<!--get three connections when initializing, values should be in Minpoolsize and Maxpools Between the ize. Default:3--<property name= "Initialpoolsize" value= "3" ></property> <!-- The minimum number of connections that are kept in the connection pool. Default:3---<property name= "Minpoolsize" value= "3" ></property> <!--connection pool The maximum number of connections that are retained. Default:15--<property name= "Maxpoolsize" value= "5" ></property> <!--when connected When the connection in the pool runs out, c3p0 the number of connections that are fetched at one time. Default:3--<property name= "Acquireincrement" value= "3" ></property> <!- -Controls the number of preparedstatements loaded within the data source. If both maxstatements and maxstatementsperconnection are 0, the cache is closed. default:0--<property name= "maxstatements" value= "8" ></property> <!--ma XstatemeNtsperconnection defines the maximum number of cache statements that a single connection in a connection pool has. default:0--<property name= "Maxstatementsperconnection" value= "5" ></property> <!--maximum idle time, unused in 1800 seconds, the connection is discarded. If 0, it will never be discarded. default:0--<property name= "MaxIdleTime" value= "1800" ></property> </bean> ; </property> </bean> <!--Configure Sessionfactory transactions based on annotations--<bean name= "Txmanager" class= "Org.s Pringframework.orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref=" Sessionfactory "></property> </bean> <tx:annotation-driven transaction-manager=" Txmanager "/>& Lt;/beans>
Databaseconnection.properties Properties File
3: Test
This is my directory structure, is a layer of calls to a layer
Controller action:
@Controller ("usersaction")//Controller annotation, and the definition of change Bean ID is usersaction, @Scope ("prototype")// The declared bean is scoped to prototype: Each fetch of the bean from the container is a new object. public class Usersaction extends Actionsupport { @Resource private iuserservice servise; Public String Execute () { Servise.sava (); Return "Success"; }}
Business Layer Seivice:
@Service//service Layer Annotation public class Userservicebean implements Iuserservice { //Add value to attribute @Resource public Iuserdao Userdao; @Override @Transactional//This method automatically opens the transaction and commits the transaction, and rolls back the transaction public void Sava () { Userdao.sava () when there is an exception;} }
Data Manipulation Layer DAO:
public class Userdaobean implements Iuserdao { //inject Sessionfactory value from configuration file @Resource Private Sessionfactory seesionfactory; @Override public void Sava () { Session session = Seesionfactory.getcurrentsession (); SYSTEM.OUT.PRINTLN (session); Users u=new users (); U.setuname ("Wang Nima"); Session.save (u); }}
Most have a javabean entity, this entity is simple only the ID and name two fields, the test did not get a lot of fields up
public class Users { private Integer uid; Private String uname; Public Integer Getuid () { return uid; } public void SetUid (Integer uid) { this.uid = uid; } Public String Getuname () { return uname; } public void Setuname (String uname) { this.uname = uname; } @Override public String toString () { return "Users [uid=" + uid + ", uname=" + uname + "]"; }}
corresponding mapping file
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
Last Visit url:http://localhost:8080/lishun_ssh2_01/lishun/users
The test is successful, the framework is completed
SSH2 integration of the Java EE