package demo.action; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.ActionSupport; import demo.service.DemoService; /** * QQ:30366040 * Demo测试类 * @author PC504 * */ public class DemoAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; @Autowired private DemoService demoService;//service 利用的annotation public DemoService getDemoService() { return demoService; } public void setDemoService(DemoService demoService) { this.demoService = demoService; } public String execute(){ demoService.say(); return SUCCESS; } }
package demo.service.impl;
import demo.dao.DemoDao; import demo.service.DemoService; /** * 接口略 * @author PC504 * */ public class DemoServiceImpl implements DemoService { DemoDao demoDao; public DemoDao getDemoDao() { return demoDao; } public void setDemoDao(DemoDao demoDao) { this.demoDao = demoDao; } public String say(){ return demoDao.save(); } }
package demo.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import demo.dao.DemoDao; import demo.dao.entity.Demo; /** * 接口略 * @author PC504 * */ public class DemoDaoImpl extends HibernateDaoSupport implements DemoDao { public String save(){ Demo demo = new Demo(); demo.setDemoName("say"); getHibernateTemplate().save(demo); return null; } }
一下是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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=demo"></property> <property name="username" value="sa"></property> <property name="password" value="root"></property> </bean> </beans>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="demoDao" class="demo.dao.impl.DemoDaoImpl"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="demoService" class="demo.service.impl.DemoServiceImpl"> <property name="demoDao"> <ref local="demoDao" /> </property> </bean> <!--Hibernate Annotation SessionFatory--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <!--常用数据库方言 MySQL5Dialect,SQLServerDialect,OracleDialect,SybaseDialect,DB2Dialect,HSQLDialect --> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.query.substitutions">true 1, false 0</prop> <prop key="hibernate.default_batch_fetch_size">4</prop> </props> </property> <property name="mappingResources" value="demo/dao/entity/Demo.hbm.xml"/> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" > <ref bean="sessionFactory"/> </property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> </beans>
<?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" default-autowire="byName" default-lazy-init="false"> <!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 --> <!-- 默认值如果适合,该项可以不用修改 --> <context:component-scan base-package="demo.service" /> </beans>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <action name="demoAction" class="demo.action.DemoAction" > <result name="success">/WEB-INF/jsp/demo/index.jsp</result> </action> </package> <!-- Add packages here --> </struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:/log4j.properties</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationConfig/applicationContext-*.xml</param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J. log4j.rootLogger=INFO,stdout # SqlMap logging configuration... log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.com.tpaic.tpfa.app.biz.dispatch.impl=ERROR log4j.logger.java.net.SocketException=FATAL # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n #log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n # Keep three backup files. # log4j.appender.logfile.MaxBackupIndex=3 # Pattern to output: date priority [category] - message log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n #A6 send log info(ERROR or Fatal) by Email #log4j.appender.A6 = com.tpaic.claim.util.SMTPExtAppender #log4j.appender.A6.Threshold=INFO #log4j.appender.A6.SMTPHost=tpaic-mailsrv.tpaicdom.com #[email protected] #[email protected] #log4j.appender.A6.SMTPAuth=true #log4j.appender.A6.SMTPUsername=oa #log4j.appender.A6.SMTPPassword=123@tpaic #log4j.appender.A6.Subject=AutoClaim System Error Log #log4j.appender.A6.layout=org.apache.log4j.PatternLayout #log4j.appender.A6.layout.ConversionPattern= [%d{HH:mm:ss}] [%t] %c - %-5p - %m%n
就弄到这里了 太晚了 该睡觉了 。终于测试成功了 。