接口:
public interface StuDao { public void insert();
public void delete();
}
public interface StuSer { public void update();
}
实现:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.test.spring.interf.StuDao;
public class DaoImpl extends JdbcDaoSupport implements StuDao {
public void delete() {
JdbcTemplate jt= this.getJdbcTemplate();
String sql1 = "delete from student1 where id=1";
jt.update(sql1);
}
public void insert() {
JdbcTemplate jt= this.getJdbcTemplate();
String sql = "insert into student2 values (1,'jack','dd')";//这句话会出错
jt.update(sql);
}
}
public class StuSerImpl {
private DaoImpl dao;
public void update() {
try{
this.dao.delete();
this.dao.insert();
}catch(Exception e){
System.out.println("stuSerImpl-----"+e);
}
}
public DaoImpl getDao() {
return dao;
}
public void setDao(DaoImpl dao) {
this.dao = dao;
}
}
action:
public void setStuSerImpl(StuSerImpl stuSerImpl) {
this.stuSerImpl = stuSerImpl;
}
public StuSerImpl getStuSerImpl() {
return this.stuSerImpl;
}
private StuSerImpl stuSerImpl;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DForm dForm = (DForm) form; // TODO Auto-generated method stub
try{
stuSerImpl.update();
}catch(Exception e){
System.out.println("-------------"+e);
}
return null;
}
}
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://struts.apache.org/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="dForm" type="com.test.spring.struts.form.DForm" />
</form-beans>
<action-mappings>
<action
attribute="dForm"
name="dForm"
path="/d"
scope="request" type="org.springframework.web.struts.DelegatingActionProxy" />
</action-mappings>
<message-resources parameter="ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>
</struts-config>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>**</value>
</property>
</bean>-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" singleton="true">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@****:**</value>
</property>
<property name="username">
<value>**</value>
</property>
<property name="password">
<value>**</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="sdao" class="com.test.spring.impl.DaoImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="stuimp" class="com.test.spring.impl.StuSerImpl">
<property name="dao">
<ref local="sdao"/>
</property>
</bean>
<bean name="/d" class="com.test.spring.struts.action.DAction" singleton="false">
<property name="stuSerImpl">
<ref local="stuimp"/>
</property>
</bean>
<bean id="baseTxProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="sdao"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="de*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
public interface StuDao { public void insert();
public void delete();
}
public interface StuSer { public void update();
}
实现:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.test.spring.interf.StuDao;
public class DaoImpl extends JdbcDaoSupport implements StuDao {
public void delete() {
JdbcTemplate jt= this.getJdbcTemplate();
String sql1 = "delete from student1 where id=1";
jt.update(sql1);
}
public void insert() {
JdbcTemplate jt= this.getJdbcTemplate();
String sql = "insert into student2 values (1,'jack','dd')";//这句话会出错
jt.update(sql);
}
}
public class StuSerImpl {
private DaoImpl dao;
public void update() {
try{
this.dao.delete();
this.dao.insert();
}catch(Exception e){
System.out.println("stuSerImpl-----"+e);
}
}
public DaoImpl getDao() {
return dao;
}
public void setDao(DaoImpl dao) {
this.dao = dao;
}
}
action:
public void setStuSerImpl(StuSerImpl stuSerImpl) {
this.stuSerImpl = stuSerImpl;
}
public StuSerImpl getStuSerImpl() {
return this.stuSerImpl;
}
private StuSerImpl stuSerImpl;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DForm dForm = (DForm) form; // TODO Auto-generated method stub
try{
stuSerImpl.update();
}catch(Exception e){
System.out.println("-------------"+e);
}
return null;
}
}
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://struts.apache.org/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="dForm" type="com.test.spring.struts.form.DForm" />
</form-beans>
<action-mappings>
<action
attribute="dForm"
name="dForm"
path="/d"
scope="request" type="org.springframework.web.struts.DelegatingActionProxy" />
</action-mappings>
<message-resources parameter="ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>
</struts-config>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>**</value>
</property>
</bean>-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" singleton="true">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@****:**</value>
</property>
<property name="username">
<value>**</value>
</property>
<property name="password">
<value>**</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="sdao" class="com.test.spring.impl.DaoImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="stuimp" class="com.test.spring.impl.StuSerImpl">
<property name="dao">
<ref local="sdao"/>
</property>
</bean>
<bean name="/d" class="com.test.spring.struts.action.DAction" singleton="false">
<property name="stuSerImpl">
<ref local="stuimp"/>
</property>
</bean>
<bean id="baseTxProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="sdao"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="de*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货