Spring+struts2+hibernate Integrated development projects in addition to our regular methods, you can also use full annotations to make development more convenient and fast.
One: Introducing a jar package that supports full annotation integration related
<!--Oracle Driver--
<!--Https://mvnrepository.com/artifact/com.oracle/ojdbc14--
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>
<!--Hibernate core dependency--
<!--Https://mvnrepository.com/artifact/org.hibernate/hibernate-core--
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>
<!--JTA The primary role is transaction--
<!--Https://mvnrepository.com/artifact/javax.transaction/jta--
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!--Https://mvnrepository.com/artifact/javax/javaee-api--
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!--spring configuration jar-->
<!--Https://mvnrepository.com/artifact/org.springframework/spring-context--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK/SPRING-TX--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!--jar--> used by AOP
<!--Https://mvnrepository.com/artifact/org.aspectj/aspectjweaver--
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!--Web Programming jar-->
<!--Https://mvnrepository.com/artifact/org.springframework/spring-web--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK/SPRING-WEBMVC--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<!--c3p0 Connection pool--
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
<!--Spring Integration hibernate-->
<!--Https://mvnrepository.com/artifact/org.springframework/spring-orm--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!--struts2 Core Pack--
<!--Https://mvnrepository.com/artifact/org.apache.struts/struts2-core--
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4</version>
</dependency>
<!--xwork Core Pack--
<!--Https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core--
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.4</version>
</dependency>
<!--struts to integrate spring jar--
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
<!--Struts2 Annotations support JAR--
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
Second: Build the project structure (to add students as an example)
Beans Layer
@Entity
@Table //database table name defaults to the entity name if it is not written
public class Studentssh {
@Id
the generation strategy of @GeneratedValue//ID
private int id;
@Column
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;
}
}
DAO layer
Public interface Istudentdao {
/**
* Add Students
* @param student
* @return
*/
public int Add (STUDENTSSH student);
}
@Repository ("Studentdao")
public class Studentdaoimpl implements Istudentdao {
Injected into the sessionfactory plant
@Resource
Private Sessionfactory sessionfactory;
Add to
public int Add (STUDENTSSH student) {
Serializable count = Sessionfactory.getcurrentsession (). Save (student);
Return (Integer) count;
}
Public Sessionfactory getsessionfactory () {
return sessionfactory;
}
public void Setsessionfactory (Sessionfactory sessionfactory) {
This.sessionfactory = sessionfactory;
}
}
Service Layer
Public interface Istudentservice {
/**
* Add Students
* @param student
* @return
*/
public int Add (STUDENTSSH student);
}
@Service ("Studentservice")
public class Studentserviceimpl implements Istudentservice {
@Resource (name = "Studentdao")
Private Istudentdao DAO;
Public Istudentdao Getdao () {
return DAO;
}
public void Setdao (Istudentdao dao) {
This.dao = DAO;
}
/**
* Add
* @param student
* @return
*/
@Transactional
public int Add (STUDENTSSH student) {
int result = Dao.add (student);
return result;
}
}
Action Layer
@Controller ("Studentaction")
@ParentPackage ("Struts-default")
@Namespace ("/")
@Scope ("prototype")//struts as a singleton for multiple spring cases
public class Studentaction extends Actionsupport {
Private Studentssh studentssh;
@Resource (name = "Studentservice")
Private Istudentservice Studentservice;
/**
* NEW
* @return
*/
@Action (value = "add", results = {@Result (name = "Success", location = "/jsp/index.jsp")})
Public String Add () {
Studentservice.add (STUDENTSSH);
return SUCCESS;
}
Public Studentssh Getstudentssh () {
return studentssh;
}
public void Setstudentssh (Studentssh studentssh) {
This.studentssh = Studentssh;
}
Public Istudentservice Getstudentservice () {
return studentservice;
}
public void Setstudentservice (Istudentservice studentservice) {
This.studentservice = Studentservice;
}
}
Three: Writing the configuration file
applicationcontext.xml configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--identification Notes--
<context:component-scan base-package= "CN.SJL" ></context:component-scan>
<!--data Source-
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name= "Driverclass" value= "${jdbc.driverclass}" ></property>
<property name= "Jdbcurl" value= "${jdbc.jdbcurl}" ></property>
<property name= "user" value= "${jdbc.user}" ></property>
<property name= "Password" value= "${jdbc.password}" ></property>
</bean>
<!--identification jdbc.properties-->
<context:property-placeholder location= "Classpath:jdbc.properties" ></context:property-placeholder>
<!--sessionfactory Factory--
<bean id= "Factory" class= "Org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource" ></property>
<property name= "Hibernateproperties" >
<props>
< print SQL statements!--Console--
<prop key= "Hibernate.show_sql" >true</prop>
<!--formatting sql-->
<prop key= "Hibernate.format_sql" >true</prop>
<!--dialect---
<prop key= "Hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</prop>
<!--bound Thread--
<prop key= "Hibernate.current_session_context_class" > Org.springframework.orm.hibernate5.springsessioncontext</prop>
</props>
</property>
<!--scan small configuration files--
<!--<property name= "mappingdirectorylocations" value= "Classpath:cn/sjl/beans" ></property>-->
<property name= "Packagestoscan" value= "CN.SJL" ></property>
</bean>
<!--transaction Manager--
<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Factory" ></property>
</bean>
<!--Affairs--
<tx:annotation-driven transaction-manager= "TransactionManager" ></tx:annotation-driven>
</beans>
jdbc.properties File
Jdbc.driverclass=oracle.jdbc.driver.oracledriver
Jdbc.jdbcurl=jdbc:oracle:thin: @localhost: 1521:ORCL
Jdbc.user=root //database user name
Jdbc.password=root //Database Password
IV: Web. xml file
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value >classpath:applicationcontext.xml</param-value>
</context-param>
<filter>
< Filter-name>struts</filter-name>
<!--core Controller-->
<filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</ Url-pattern>
</filter-mapping>
<!--listener-->
<listener>
<listener-class> Org.springframework.web.context.contextloaderlistener</listener-class>
</listener>
Five: JSP page
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
<title> Add </title>
<body>
<H2>SSH Annotation Edition Integration <s:debug></s:debug>
<form method= "POST" action= "/add" >
Name: <input type= "text" name= "Studentssh.name" >
<input type= "Submit" value= "Add" >
</form>
</body>
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
<title> Add Success </title>
<body>
</body>
Six: Deploy Tomcat and start
SSH (spring+struts2+hibernate) integration based on annotation development