UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oupeng.mapper.user.UserMapper"><!--Mapper映射器-->
<!-- 根据用户id修改用户信息-->
<update id="updateUserInfo" parameterType="User">
update smbms_user set userCode=#{userCode},userName=#{userName},userPassword=#{userPassword},gender=#{gender},phone=#{phone},
address=#{address},userRole=#{userRole},creationDate=#{creationDate},userRoleName=#{userRoleName} where id=#{uid}
</update>
<!-- 根据用户id删除用户信息-->
<update id="deleteUserInfo" parameterType="User">
delete into smbms_user(userCode=#{userCode},userName=#{userName},userPassword=#{userPassword},gender=#{gender},phone=#{phone},
address=#{address},userRole=#{userRole},creationDate=#{creationDate},userRoleName=#{userRoleName}) where id=#{uid}
</update>
<!-- 根据用户id修改用户密码-->
<update id="updateUserPassword" parameterType="User">
update smbms_user set userPassword=#{userPassword} where id=#{uid}
</update>
</mapper>
applicationContext-mapper.xml
<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:p="http://www.springframework.org/schema/p"
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-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd" default-autowire="byType">
<bean id="userService" class="com.oupeng.service.impl.UserServiceImpl">
<!-- 完成数据访问层对象的注入-->
<property name="userMapper" ref="userMapper"></property>
<property name="userMapper" ref="userMapperProxy"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置声明式事务管理策略-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 配置具体的事务的策略-->
<tx:attributes>
<!-- method 建议动态的配置 propagation="用的策略"
timeout事务的超时时间(单位是秒) -1代表时间无限制
rollback-for遇到什么事务的时候进行回滚-->
<tx:method name="find*" propagation="NOT_SUPPORTED" timeout="-1"/>
<tx:method name="add*" propagation="REQUIRED" timeout="-1" rollback-for="SQLException"/>
<tx:method name="update*" propagation="REQUIRED" timeout="-1" rollback-for=""/>
</tx:attributes>
</tx:advice>
<!-- 配置 事务策略的应用(属于一种特殊的增强) -->
<aop:config>
<!-- 配置切入点-->
<aop:pointcut id="pointCut" expression="execution(* com.oupeng.service..*.*(..))"/>
<!-- 增强处理 事务用aop:advisor-->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointCut"/>
</aop:config>
<!-- 启用事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 自动扫描--><!--自动扫描指定的包 将包下的注解进行启用--><!-- 主要配置业务逻辑层-->
<context:component-scan base-package="com.oupeng.service.impl"></context:component-scan>
</beans>
applicationContext-service.xml
<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:p="http://www.springframework.org/schema/p"
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-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd" default-autowire="byType">
<bean id="userService" class="com.oupeng.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property> 有UserMapperImpl
<property name="userMapper" ref="userMapperProxy"></property> 没有UserMapperImp
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置声明式事务管理策略-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 配置具体的事务的策略-->
<tx:attributes>
<tx:method name="find*" propagation="NOT_SUPPORTED" timeout="-1"/>
<tx:method name="add*" propagation="REQUIRED" timeout="-1" rollback-for="SQLException"/>
<tx:method name="update*" propagation="REQUIRED" timeout="-1" rollback-for=""/>
</tx:attributes>
</tx:advice>
<!-- 配置 事务策略的应用(属于一种特殊的增强) -->
<aop:config>
<!-- 配置切入点-->
<aop:pointcut id="pointCut" expression="execution(* com.oupeng.service..*.*(..))"/>
<!-- 增强处理 事务用aop:advisor-->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointCut"/>
</aop:config>
<!-- 启用事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 自动扫描--><!--自动扫描指定的包 将包下的注解进行启用--><!-- 主要配置业务逻辑层-->
<context:component-scan base-package="com.oupeng.service.impl"></context:component-scan>
</bean>
UserServiceImpl
package com.oupeng.service.impl;
import com.oupeng.mapper.user.UserMapper;
import com.oupeng.pojo.User;
import com.oupeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
//@Transactional //标记给下面这个类使用事务策略
@Service("userService1")
public class UserServiceImpl implements UserService {
// 注入数据访问层
// 一定写的是接口名
@Autowired
private UserMapper userMapper;
@Transactional(propagation = Propagation.REQUIRED)
public void updateUserInfoService(User user) {
userMapper.updateUserInfo(user);
}
@Transactional(propagation = Propagation.REQUIRED)
public void deleteUserInfoService(User user) {
userMapper.deleteUserInfo(user);
}
@Transactional(propagation = Propagation.REQUIRED)
public void updateUserPasswordService(int id, String userPassword) {
userMapper.updateUserPassword(id, userPassword);
}
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
}
applicationContext-bill.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" >
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 加载MyBatis的配置文件 -->
<property name="configLocation" value="classpath:com/lu/bill/resource/mybatis-config.xml"></property>
<!--加载Mybatis的映射文件 -->
<property name="mapperLocations">
<!-- <value>classpath:com//dao/ProviderMapper.xml</value> -->
<!-- <value>classpath:com/lu/dao/ProviderMapper.xml</value> -->
<!-- 通配符加载 -->
<value>classpath:com/lu/bill/dao/BillMapper.xml</value>
</property>
</bean>
<!-- 配置sqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 构造注入sqlSessionFactory-->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 配置数据访问层组件 -->
<!-- <bean id="BillMapper" class="com.lu.bill.dao.Impl.BillMapperImpl"><!– 注入sqlSessionTemplate –>
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property> </bean>-->
<!-- 配置要生成实现类的接口自动生成-->
<bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.lu.bill.dao.BillMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- <!– 配置业务逻辑层组件 –>
<bean id="BillService" class="com.lu.bill.service.Impl.BillServiceImpl">
<!– 完成数据访问层对象的注入–>
<property name="billMapper" ref="billMapperProxy"/>
</bean>-->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lu.bill.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>-->
<!--配置让其自动扫码指定的包下的注解 -->
<context:component-scan base-package="com.lu.bill.service.Impl"></context:component-scan>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置声明式事务的规则-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 配置具体的策略 -->
<tx:attributes>
<tx:method name="service*" propagation="REQUIRED" timeout="10" rollback-for="SQLException"/>
<!-- <tx:method name="update*" propagation="REQUIRED" timeout="-1"/>
<tx:method name="delete*" propagation="REQUIRED" timeout="10"/>-->
</tx:attributes>
</tx:advice>
</beans>
BillMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lu.bill.dao.BillMapper">
<select id="getProviderName" resultType="Bill" parameterType="Bill" resultMap="ProName">
select b.*,p.proName from smbms_bill b,smbms_provider p where productName like concat('%',#{productName},'%')
and p.id = b.providerId
</select>
<resultMap id="ProName" type="Bill">
<id property="id" column="id"/>
<result property="billName" column="billName"/>
<result property="productName" column="productName"/>
<result property="proName" column="proName"/>
</resultMap>
<insert id="add" parameterType="Bill" >
insert into
smbms_bill(billCode,productName,isPayment)values(#{billCode},#{productName},#{isPayment})
</insert>
<update id="update" parameterType="Long">
update smbms_bill set billCode=#{bill.billCode},productName=#{bill.productName} where id=#{l_id}
</update>
<delete id="delete" parameterType="Long" >
delete from smbms_bill where id=#{id}
</delete>
</mapper>
BillServiceImpl
package com.lu.bill.service.Impl;
import com.lu.bill.dao.BillMapper;
import com.lu.bill.pojo.Bill;
import com.lu.bill.service.BillService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("BillService")
public class BillServiceImpl implements BillService {
@Resource
BillMapper billMapper;
public BillMapper getBillMapper() {
return billMapper;
}
public void setBillMapper(BillMapper billMapper) {
this.billMapper = billMapper;
}
@Override
public List<Bill> serviceGetProviderName(Bill bill) {
return billMapper.getProviderName(bill);
}
@Override
public boolean serviceAdd(Bill bill) {
return billMapper.add(bill);
}
@Override
public boolean serviceUpdate(Long id,Bill bill) {
return billMapper.update(id,bill);
}
@Override
public boolean serviceDelete(Long id) {
return billMapper.delete(id);
}
}