1, the first integrated struts and hibernate
Import the relevant jar package
Write hibernate configuration file
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property> <mapping class="com.blb.entity.User"/> </session-factory> </hibernate-configuration>
Configure the filter to struts in the web.xml file
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Write log4j2 profile
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> </Console> </Appenders> <Loggers> <Logger name="com.opensymphony.xwork2" level="info"/> <Logger name="org.apache.struts2" level="debug"/> <Root level="warn"> <AppenderRef ref="STDOUT"/> </Root> </Loggers> </Configuration>
Write struts configuration file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="basicstruts2" extends="struts-default,json-default" strict-method-invocation="false"> <action name="test" class="com.blb.action.TestAction"> <result name="success">/index.jsp</result> </action> </package> </struts>
Write a User entity class used to test
Write hibernate entity class mapping file
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.blb.entity.User" table="t_user"> <id name="id"> <generator class="uuid"></generator> </id> <property name="username"></property> <property name="password"></property> </class> </hibernate-mapping>
2, integrated spring
Import the relevant jar package
Because struts2-spring-plugin the jar and the bag containing the spring struts jar package, then there is no need to import spring jar package, introduced in front struts jar package may be deleted.
Written in the spring configuration file applicationContext.xml
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://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 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.blb" /> <!-- Hikari Datasource 数据源--> <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" > <-! <Property name = " driverClassName" value = "$ {db.driverClass}" /> -> <-! Without specifying, unless the system does not automatically identify -> <Property name = "the jdbcUrl" value = "JDBC: MySQL: // localhost:? 3306 / MENU useUnicode = to true & amp; characterEncoding = UTF-. 8" /> <Property name = "username" value = "the root" /> <Property name = "password" value = "the root "/> <! - read-only database configured to connect to true, ensure safety -> <Property name =" readOnly "value =" to false "/> <! - the maximum length of waiting connection pool assigned connection (ms) , that time has not yet available connections SQLException, default occurs: 30 seconds -> <Property name = "connectionTimeout" value = "30000" /> <!- a connection in the idle state when the maximum length (ms), were released overtime (Retired), default: 10 minutes -> <Property name = "the idleTimeout" value = "600000" /> <! - connected to a life-long (milliseconds), and the timeout is not in use were released (Retired), default: 30 minutes, set a database than 30-second timeout less, MySQL wait_timeout reference parameters (show variables like ' timeout%% ';) -> <Property name = "maxlifetime" value = "1800000" /> ! <- the maximum number of connections allowed in the pool. Default: 10; Recommended formula: ((2 * core_count) effective_spindle_count +) -> <Property name = "maximumPoolSize" value = "15" /> </ the bean> <= the bean ID "of the mySessionFactory" class = " org.springframework.orm.hibernate5.LocalSessionFactoryBean "> <Property name =" the dataSource "REF =" the dataSource "/> <Property name =" configLocations "value =" the hibernate.cfg.xml "/> </ the bean>
Hikari Datasource using the data source, import the relevant jar package
Then configure the spring listener in the web.xml file
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Finally, write test code
public String select(){ Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); Query<User> query = session.createQuery("from User"); List<User> list = query.list(); for (User user : list) { System.out.println(user); } return SUCCESS; }
After a successful run will print out the data in the database menu t_user table