1.先创建一个maven项目
2.在pom.xml里引入以下相关jar包:
struts2-core , struts2-json-plugin , struts2-spring-plugin;
log4j-api , log4j-core
spring-context , spring-orm , HikariCP(数据源的包);
hibernate-core
3.在web.xml里输入相关过滤器
<!-- spring --> <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>
<!-- struts2 --> <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>
4.创建一个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: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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 启动bean的扫描 --> <context:component-scan base-package="com.blb"></context:component-scan> <bean class="com.blb.test.Test" id="test"></bean> <!-- 连接池 --> <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="readOnly" value="false" /> <property name="connectionTimeout" value="30000" /> <property name="idleTimeout" value="600000" /> <property name="maxLifetime" value="1800000" /> <property name="maximumPoolSize" value="15" /> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean> </beans>
5.创建一个struts.xml配置如下:
<?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="aaa" class="test"> <result name="success">/login.jsp</result> </action> </package> </struts>
6.创建一个hibernate.cfg.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!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> <!-- 配置映射文件 --> <mapping class="com.blb.entity.user" /> </session-factory> </hibernate-configuration>
7.创建一个log4j2.xml配置如下:
<?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>
8.创建一个User实体类:
package com.blb.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="s_student") public class user { @Id private String sid; @Column(name = "name") private String usermane; public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getUsermane() { return usermane; } public void setUsermane(String usermane) { this.usermane = usermane; } @Override public String toString() { return "user [sid=" + sid + ", usermane=" + usermane + "]"; } }
9.最后就可以写测试类开始测试了。
(以上全是个人写法,如有不足请下方留言)