Struts2 + Spring + Hibernate environment, struts2hibernate
Struts-2.3.20
Spring-1, 4.1.4
Hibernate-4.3.8
Slf4j-1.7.10
1. Create a database in MySQL
mysql> create database myoa default character set utf8
2. Create a Web Project in MyEclipse
Right-click on the project-Properties and set the code to UTF-8.
3. Configure Struts2
4. Configure Hibernate
5. Configure Spring
6. Integrate Spring and Struts2
Test:
- Access http: // localhost: 8080/MyOA/test. action.
7. Integrate Spring and Hibernate
- Create jdbc. properties:
jdbcUrl=jdbc:mysql:///myoadriverClass=com.mysql.jdbc.Driveruser=rootpassword=123456
- Delete the four repeated attributes in hibernate. cfg. xml.
- Configure applicationContext. xml:
<! -- Import the external properties file --> <context: property-placeholder location = "classpath: jdbc. properties"/> <! -- Configure SessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean"> <! -- Specify the location of the hibernate configuration file --> <property name = "configLocation" value = "classpath: hibernate. cfg. xml"> </property> <! -- Configure the c3p0 database connection pool --> <property name = "dataSource"> <bean class = "com. mchange. v2.c3p0. ComboPooledDataSource"> <! -- Data connection information --> <property name = "jdbcUrl" value = "$ {jdbcUrl}"> </property> <property name = "driverClass" value = "$ {driverClass} "> </property> <property name =" user "value =" $ {user} "> </property> <property name =" password "value =" $ {password} "> </property> <! -- Other configurations --> <! -- Three connections are obtained during initialization. The value must be between minPoolSize and maxPoolSize. Default: 3 --> <property name = "initialPoolSize" value = "3"> </property> <! -- The minimum number of connections retained in the connection pool. Default: 3 --> <property name = "minPoolSize" value = "3"> </property> <! -- The maximum number of connections retained in the connection pool. Default: 15 --> <property name = "maxPoolSize" value = "5"> </property> <! -- The number of connections that c3p0 obtains at the same time when connections in the connection pool are exhausted. Default: 3 --> <property name = "acquireIncrement" value = "3"> </property> <! -- Control the number of PreparedStatements loaded in the data source. If both maxStatements and maxStatementsPerConnection are 0, the cache is disabled. Default: 0 --> <property name = "maxStatements" value = "8"> </property> <! -- MaxStatementsPerConnection defines the maximum number of statements cached for a single connection in the connection pool. Default: 0 --> <property name = "maxStatementsPerConnection" value = "5"> </property> <! -- Maximum idle time. connections are discarded if they are not used within 1800 seconds. If it is 0, it will never be discarded. Default: 0 --> <property name = "maxIdleTime" value = "1800"> </property> </bean> <! -- Configure declarative Transaction Management (using annotation) --> <bean id = "txManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean> <tx: annotation-driven transaction-manager =" txManager "/>
The above <beans> to add the "xmlns: tx =" http://www.springframework.org/schema/tx ", and in" xsi: schemaLocation "specify the schema address" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ".
Test:
- Create a domain sub-package where User. java and User. hbm. xml.
- Compile User. java:
public class User {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;}}
- Modify User. hbm. xml:
<class name="User" table="test_user"><id name="id"><generator class="native" /></id></class>
- Create TestService. java:
@Servicepublic class TestService {@Resourceprivate SessionFactory sessionFactory;@Transactionalpublic void saveTwo() {Session session = sessionFactory.getCurrentSession();session.save(new User());// int a = 1 / 0;session.save(new User());}}
- Modify TestAction. java:
@Resourceprivate TestService testService;@Overridepublic String execute() throws Exception {testService.saveTwo();return SUCCESS;}
- Access http: // localhost: 8080/MyOA/test. action.
8. Sort resource folders
Source Folder: src, config, test.
Folder: Web Root/script, Web Root/style, Web Root/WEB-INF/jsp.
9. Configure slf4j