第一步:添加所需要的依赖包
<dependencies>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope> <!--编译的时候不发布 -->
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<!--Druid连接池包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.12</version>
</dependency>
<!--aspectjweaver包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.16.3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.7.Final</version>
</dependency>
<!-- Struts2的核心包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16</version>
<!--
这里的 exclusions 是排除包,因为 Struts2中有javassist,Hibernate中也有javassist,
所以如果要整合Hibernate,一定要排除掉Struts2中的javassist,否则就冲突了。
-->
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- convention-plugin插件,使用了这个插件之后,就可以采用注解的方式配置Action -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.20</version>
</dependency>
<!--config-browser-plugin插件,使用了这个插件之后,就可以很方便的浏览项目中的所有action及其与 jsp view的映射 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>2.3.20</version>
</dependency>
<!-- Struts2和Spring整合插件 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
<!-- spring3 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
</dependencies>
第二步:在resources目录下添加Spring.xml文件
第四步:在resource目录下添加一个文件命名为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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- 引入属性文件 --> <context:property-placeholder location="classpath:config.properties" /> <!-- 自动扫描dao和service包(自动注入) --> <context:component-scan base-package="com.seecen.ssh.dao,com.seecen.ssh.service" /> </beans> 第三步:resource目录下添加一个文件命名Struts.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> <!-- 指定由spring负责action对象的创建 --> <constant name="struts.objectFactory" value="spring" /> <!-- 所有匹配*.action的请求都由struts2处理 --> <constant name="struts.action.extension" value="action" /> <!-- 是否启用开发模式(开发时设置为true,发布到生产环境后设置为false) --> <constant name="struts.devMode" value="true" /> <!-- struts配置文件改动后,是否重新加载(开发时设置为true,发布到生产环境后设置为false) --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 设置浏览器是否缓存静态内容(开发时设置为false,发布到生产环境后设置为true) --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 请求参数的编码方式 --> <constant name="struts.i18n.encoding" value="utf-8" /> <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发(开发时设置为true,发布到生产环境后设置为false) --> <constant name="struts.i18n.reload" value="true" /> <!-- 文件上传最大值 --> <constant name="struts.multipart.maxSize" value="104857600" /> <!-- 让struts2支持动态方法调用,使用叹号访问方法 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- Action名称中是否还是用斜线 --> <constant name="struts.enable.SlashesInActionNames" value="false" /> <!-- 允许标签中使用表达式语法 --> <constant name="struts.tag.altSyntax" value="true" /> <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true --> <constant name="struts.dispatcher.parametersWorkaround" value="false" /> <!--系统--> <package name="basePackage" namespace="/" extends="struts-default,json-default"> <action name="user" class="com.seecen.ssh.action.systemAction.UsersAction"> <result name="list">/WEB-INF/views/system/user/list.jsp</result> <result name="addPage">/WEB-INF/views/system/user/add.jsp</result> <result name="success" type="redirect">user!list.action</result> <result name="editPage">/WEB-INF/views/system/user/userEdit.jsp</result> <result name="login">/WEB-INF/views/home/login.jsp</result> <result name="home">/WEB-INF/views/home/list.jsp</result> </action> <action name="roleAction" class="com.seecen.ssh.action.systemAction.RoleAction"> <result name="roleList">/WEB-INF/views/system/role/roleList.jsp</result> <result name="addrole">/WEB-INF/views/system/role/roleAdd.jsp</result> <result name="success" type="redirect">roleAction!list.action</result> <result name="editPage">/WEB-INF/views/system/role/roleEdit.jsp</result> </action> <action name="moduleAction" class="com.seecen.ssh.action.systemAction.ModuleAction"> <result name="moduleList">/WEB-INF/views/system/module/moduleList.jsp</result> <result name="edit">/WEB-INF/views/system/module/moduleEdit.jsp</result> <result name="addModule">/WEB-INF/views/system/module/moduleAdd.jsp</result> <result name="success" type="redirect">moduleAction!list.action</result> </action> <action name="noticeAction" class="com.seecen.ssh.action.systemAction.NoticeAction"> <result name="list">/WEB-INF/views/system/notice/noticeList.jsp</result> <result name="success" type="redirect">noticeAction!list.action</result> <result name="addpage">/WEB-INF/views/system/notice/menu.jsp</result> <result name="findpage">/WEB-INF/views/system/notice/find.jsp</result> <result name="editPage">/WEB-INF/views/system/notice/noticeEdit.jsp</result> </action> <action name="home" class="com.seecen.ssh.action.homeAction.loginAction"> <result name="login">/WEB-INF/views/home/login.jsp</result> <result name="success">/WEB-INF/views/home/main.jsp</result> <result name="error">/WEB-INF/views/home/login.jsp</result> </action> </package> </struts>
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- JNDI方式配置数据源 --> <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> --> <!-- 配置数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="20" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <!-- 监控数据库 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean> <!-- 配置hibernate session工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.connection.url">${jdbc_url}</prop> <prop key="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prop> </props> </property> <!-- 自动扫描注解方式配置的hibernate实体类文件 --> <property name="packagesToScan"> <list> <value>com.seecen.ssh.model</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 注解方式配置事务 --> <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 拦截器方式配置事务 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--service层以这些name开头的方法都会被加入spring事务管理中,调用这些方法时会自动创建事务--> <tx:method name="add*" /> <tx:method name="save*" /> <tx:method name="update*" /> <tx:method name="modify*" /> <tx:method name="edit*" /> <tx:method name="delete*" /> <tx:method name="remove*" /> <tx:method name="repair" /> <tx:method name="deleteAndRepair" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- spring的事务控制是放在service层的 --> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.seecen.ssh.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> </beans>
第五步:和上面一样,添加一个文件hibernate.cfg.xml,把下面代码拷进去
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">four</property>
<property name="hibernate.connection.password">four</property>
<!-- 数据库方案 -->
<property name="hibernate.default_schema">four</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 调试 -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>
<!-- 自动建表方式 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.seecen.ssh.model.jobModel.Interview"/>
<mapping class="com.seecen.ssh.model.jobModel.Entry"/>
<mapping class="com.seecen.ssh.model.jobModel.Business"/>
<mapping class="com.seecen.ssh.model.memberModel.Student"/>
<mapping class="com.seecen.ssh.model.memberModel.Message"/>
<mapping class="com.seecen.ssh.model.memberModel.Checking"/>
<mapping class="com.seecen.ssh.model.memberModel.Status"/>
<mapping class="com.seecen.ssh.model.classModel.ClassRoom"/>
<!--<mapping class="com.seecen.ssh.model.classModel.ClassActive"/>-->
<!--<mapping class="com.seecen.ssh.model.classModel.Review"/>-->
<!--<mapping class="com.seecen.ssh.model.systemModel.Role"/>-->
<!--<mapping class="com.seecen.ssh.model.systemModel.Users"/>-->
<!--<mapping class="com.seecen.ssh.model.systemModel.Notice"/>-->
<!--<mapping class="com.seecen.ssh.model.systemModel.SonModule"/>-->
<!--<mapping class="com.seecen.ssh.model.systemModel.FatherModule"/>-->
<!--<mapping class="com.seecen.ssh.model.marketModel.Channel"/>-->
<!--<mapping class="com.seecen.ssh.model.marketModel.Customer"/>-->
<!--<mapping class="com.seecen.ssh.model.marketModel.Matter"/>-->
<!--<mapping class="com.seecen.ssh.model.marketModel.Center"/>-->
<!--<mapping class="com.seecen.ssh.model.Teacher"/>-->
<!--<mapping class="com.seecen.ssh.model.memberModel.Student"/>-->
<!--<mapping class="com.seecen.ssh.model.memberModel.Message"/>-->
<!--<mapping class="com.seecen.ssh.model.memberModel.Checking"/>-->
<!-- <property name="connection.username"/> -->
<!-- <property name="connection.password"/> -->
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
第六步:在加一个配置文件config.properties
#hibernate.dialect=org.hibernate.dialect.OracleDialect
#driverClassName=oracle.jdbc.driver.OracleDriver
#validationQuery=SELECT 1 FROM DUAL
#jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:xe
#jdbc_username=four
#jdbc_password=four
#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#driverClassName=com.mysql.jdbc.Driver
#validationQuery=SELECT 1
#jdbc_url=jdbc:mysql://localhost:3306/sy?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
#jdbc_username=sypro
#jdbc_password=sypro
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
driverClassName=oracle.jdbc.driver.OracleDriver
validationQuery=SELECT 1 from dual
jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:xe
jdbc_username=four
jdbc_password=four
#jndiName=java:comp/env/dataSourceName
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
sessionInfoName=sessionInfo
uploadFieldName=filedata
uploadFileMaxSize=20971520
uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid
uploadDirectory=attached
第七步:添加log4j文件 log4j.properties
log4j.rootLogger=INFO,A1,R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Target=System.out
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%c]%m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=ssh.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.Threshold=ALL
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
所有配置文件搭建好,就可以开始做项目了