所用到的jar包:http://download.csdn.net/detail/askycat/9753812
我这里只贴出配置文件部分,要查看完整例子请下载到本地查看
目录结构
MySQL数据库中新建一个ssh数据库,创建一张t_user表
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`upwd` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`umessage` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`utime` datetime DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('10', 'hello', '123', '未知', '2017-02-14 08:47:59');
INSERT INTO `t_user` VALUES ('12', 'crazy', '123', '不详', '2017-02-14 08:52:19');
在config目录下创建spring-context.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 自动注入dao -->
<bean id="userDao" class="com.crazy.dao.Impl.TUserDaoImpl" autowire="byType"></bean>
<!-- 自动注入service -->
<bean id="userService" class="com.crazy.service.Impl.TUserServiceImpl" autowire="byType"></bean>
</beans>
struts的配置文件struts-main.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 当struts 2的配置文件修改后,系统是否自动重新加载该文件 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 是否为开发模式,可支持struts提供的debug功能 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 指定需要Struts2处理的请求后缀 -->
<constant name="struts.action.extension" value=",action,do"></constant>
<!-- 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!-- 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 -->
<constant name="struts.multipart.maxSize" value="20971520"></constant>
<package name="user_struts" extends="struts-default" namespace="/user">
<!-- *号代表action中的方法名(方法需为public) 访问时直接 (namespace/方法名)-->
<action name="*" class="com.crazy.action.UserAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
jdbc.properties文件根据自己的数据库情况进行修改
connection.driverClassName=com.mysql.jdbc.Driver
connection.url=jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=utf-8
connection.username=root
connection.password=123456
proxool.houseKeepingTestSql=select CURRENT_DATE
proxool.prototypeCount=5
proxool.hourseKeepingSleepTime=30
proxool.maximumActiveTime=300
proxool.maximumConnectionLifetime=18000000
proxool.minimumConnectionCount=10
proxool.maximumConnectionCount=30
proxool.simultaneousBuildThrottle=20
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true
hibernate.current_session_context_class=thread
hibernate配置文件spring-hibernate.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 加载外部的properties配置文件 -->
<context:property-placeholder location="WEB-INF/config/jdbc.properties"></context:property-placeholder>
<!-- 配置数据源,该数据源实际上是交给了spring管理 -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<!-- 配置数据库方言 -->
<property name="driver" value="${connection.driverClassName}"></property>
<property name="driverUrl" value="${connection.url}"></property>
<property name="user" value="${connection.username}"></property>
<property name="password" value="${connection.password}"></property>
<property name="houseKeepingTestSql" value="${proxool.houseKeepingTestSql}"/><!-- 测试连接也是否正常工作的sql语句 -->
<!-- 最少保持的空闲连接数 (默认2个) -->
<property name="prototypeCount" value="${proxool.prototypeCount}"/>
<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 默认30秒) -->
<property name="houseKeepingSleepTime" value="${proxool.hourseKeepingSleepTime}"/>
<!-- 最大活动时间(超过此时间线程将被kill,默认为5分钟) -->
<property name="maximumActiveTime" value="${proxool.maximumActiveTime}"/>
<!-- 连接最长时间(默认为4个小时) -->
<property name="maximumConnectionLifetime" value="${proxool.maximumConnectionLifetime}"/>
<!-- 最小连接数 (默认2个) -->
<property name="minimumConnectionCount" value="${proxool.minimumConnectionCount}"/>
<!-- 最大连接数 (默认5个) -->
<property name="maximumConnectionCount" value="${proxool.maximumConnectionCount}"/>
<property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- 是否在控制台显示数据库语句 -->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
</props>
</property>
<!-- javaBean的映射文件 -->
<property name="mappingResources">
<value>com/crazy/bean/TUser.hbm.xml</value>
</property>
</bean>
</beans>
日志配置文件log4j.properties
log4j.rootLogger=INFO,stdout,info,warn,error
#\u63a7\u5236\u53f0\u8f93\u51fa
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss}] %l %m %n
#INFO\u65e5\u5fd7
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File = D://logs//ssh//info.log
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss}] %l %m %n
#WARN\u65e5\u5fd7
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.File = D://logs//ssh//warn.log
log4j.appender.warn.datePattern='.'yyyy-MM-dd
log4j.appender.warn.append=true
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss}] %l %m %n
#ERROR\u65e5\u5fd7
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = D://logs//ssh//error.log
log4j.appender.error.datePattern='.'yyyy-MM-dd
log4j.appender.error.append=true
log4j.appender.error.Threshold=ERROR
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss}] %l %m %n
#\u6267\u884c\u6162\u7684SQL
log4j.logger.com.alibaba.druid.filter.stat.StatFilter=ERROR,slowsql
log4j.appender.slowsql=org.apache.log4j.DailyRollingFileAppender
log4j.appender.slowsql.File = D://logs//ssh//slow_sql.log
log4j.appender.slowsql.datePattern='.'yyyy-MM-dd
log4j.appender.slowsql.append=true
log4j.appender.slowsql.layout=org.apache.log4j.PatternLayout
log4j.appender.slowsql.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] %m %n
#\u63a7\u5236\u53f0\u8f93\u51fa\u6240\u6709SQL
log4j.logger.com.crazy.dao=DEBUG,sql
log4j.appender.sql=org.apache.log4j.ConsoleAppender
log4j.appender.sql.Target=System.out
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=%m %n
最后再配置一下web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SSH</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<description>加载日志文件</description>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<description>引入spring的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-*.xml</param-value>
</context-param>
<listener>
<description>自动装配ApplicationContext的配置信息</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<description>Struts2的配置</description>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../config/struts-main.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
运行效果如下:
可能我的代码还有很多的不足之处,如发现有什么问题请多多指正,O(∩_∩)O谢谢