0717作业
一、作业1
第一种:使用SqlSessionTemplate
1.创建工程并导入相关的jar包
2.创建pojo包
package com.openlab.pojo;
import java.util.Date;
public class Bill {
private Integer id;
private String billCode;
private String productName;
private String productDesc;
private String productUnit;
private Double productCount;
private Double totalPrice;
private Integer isPayment;
private Integer createdBy;
private Date creationDate;
private Integer modifyBy;
private Date modifyDate;
private Integer providerId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBillCode() {
return billCode;
}
public void setBillCode(String billCode) {
this.billCode = billCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductUnit() {
return productUnit;
}
public void setProductUnit(String productUnit) {
this.productUnit = productUnit;
}
public Double getProductCount() {
return productCount;
}
public void setProductCount(Double productCount) {
this.productCount = productCount;
}
public Double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(Double totalPrice) {
this.totalPrice = totalPrice;
}
public Integer getIsPayment() {
return isPayment;
}
public void setIsPayment(Integer isPayment) {
this.isPayment = isPayment;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public Integer getProviderId() {
return providerId;
}
public void setProviderId(Integer providerId) {
this.providerId = providerId;
}
}
3.创建mybatis-config配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.openlab.pojo"></package>
</typeAliases>
</configuration>
4.创建db.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
5.创建dao包
package com.openlab.dao;
import com.openlab.pojo.Bill;
import org.apache.ibatis.annotations.Param;
public interface BillDao {
int insertBill(Bill bill);
int updateBillById(Bill bill);
int deleteBillById(@Param("id") Integer id);
}
映射文件
<?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.openlab.dao.BillDao">
<insert id="insertBill" parameterType="Bill">
insert into smbms_bill (billCode,productName,productDesc,productUnit,productCount,totalPrice,isPayment,createdBy,creationDate,modifyBy,modifyDate,providerId)
values (#{billCode},#{productName},#{productDesc},#{productUnit},#{productCount},#{totalPrice},#{isPayment},#{createdBy},#{creationDate},#{modifyBy},#{modifyDate},#{providerId})
</insert>
<update id="updateBillById" parameterType="Bill">
update smbms_bill
<set>
<if test="billCode!=null and billCode!=''">
billCode = #{billCode},
</if>
<if test="productName!=null and productName!=''">
productName = #{productName},
</if>
<if test="productDesc!=null and productDesc!=''">
productDesc = #{productDesc},
</if>
<if test="productUnit!=null and productUnit!=''">
productUnit = #{productUnit},
</if>
<if test="productCount!=null">
productCount = #{productCount},
</if>
<if test="totalPrice!=null">
totalPrice = #{totalPrice},
</if>
<if test="isPayment!=null">
isPayment = #{isPayment},
</if>
<if test="createdBy!=null">
createdBy = #{createdBy},
</if>
<if test="creationDate!=null">
creationDate = #{creationDate},
</if>
<if test="modifyBy!=null">
modifyBy = #{modifyBy},
</if>
<if test="modifyDate!=null">
modifyDate = #{modifyDate},
</if>
<if test="providerId!=null">
providerId = #{providerId}
</if>
</set>
where id = #{id}
</update>
<delete id="deleteBillById">
delete from smbms_bill where id = #{id}
</delete>
</mapper>
6.创建dao.impl
package com.openlab.dao.impl;
import com.openlab.dao.BillDao;
import com.openlab.pojo.Bill;
import org.mybatis.spring.SqlSessionTemplate;
public class BillDaoImpl implements BillDao {
private SqlSessionTemplate sqlSessionTemplate;
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public int insertBill(Bill bill) {
return sqlSessionTemplate.insert("com.openlab.dao.BillDao.insertBill",bill);
}
@Override
public int updateBillById(Bill bill) {
return sqlSessionTemplate.update("com.openlab.dao.BillDao.updateBillById",bill);
}
@Override
public int deleteBillById(Integer id) {
return sqlSessionTemplate.delete("com.openlab.dao.BillDao.deleteBillById",id);
}
}
7.创建service包
package com.openlab.service;
import com.openlab.pojo.Bill;
public interface BillService {
int addBill(Bill bill);
int alterBill(Bill bill);
int deleteBillById(Integer id);
}
8.创建service.impl包
package com.openlab.service.impl;
import com.openlab.dao.BillDao;
import com.openlab.pojo.Bill;
import com.openlab.service.BillService;
public class BillServiceImpl implements BillService {
private BillDao billDao;
public BillDao getBillDao() {
return billDao;
}
public void setBillDao(BillDao billDao) {
this.billDao = billDao;
}
@Override
public int addBill(Bill bill) {
return billDao.insertBill(bill);
}
@Override
public int alterBill(Bill bill) {
return billDao.updateBillById(bill);
}
@Override
public int deleteBillById(Integer id) {
return billDao.deleteBillById(id);
}
}
9.配置applicationConte.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<bean id="billDao" class="com.openlab.dao.impl.BillDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<bean id="billService" class="com.openlab.service.impl.BillServiceImpl">
<property name="billDao" ref="billDao"></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>
<tx:method name="add*" propagation="REQUIRES_NEW"/>
<tx:method name="alter*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="billServicePointcut" expression="execution(* com.openlab.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="billServicePointcut"></aop:advisor>
</aop:config>
</beans>
10.测试
package com.openlab.test;
import com.openlab.pojo.Bill;
import com.openlab.service.BillService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
public class Test01 {
@Test
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BillService billService = (BillService) ac.getBean("billService");
Bill bill = new Bill();
bill.setProductName("土豆");
bill.setProviderId(5);
bill.setCreationDate(new Date());
bill.setTotalPrice(300.00);
int i = billService.addBill(bill);
if(i==1){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
}
@Test
public void test02(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BillService billService = (BillService) ac.getBean("billService");
Bill bill = new Bill();
bill.setId(19);
bill.setProductName("黄瓜");
bill.setModifyDate(new Date());
int i = billService.alterBill(bill);
if(i==1){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
}
@Test
public void test03(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BillService billService = (BillService) ac.getBean("billService");
int i = billService.deleteBillById(19);
if(i==1){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
}
第二种:使用MapperFactoryBean
1.修改applicationConte.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"></property>
</bean>
<bean id="billDaoProxy" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.openlab.dao.BillDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="billService" class="com.openlab.service.impl.BillServiceImpl">
<property name="billDao" ref="billDaoProxy"></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>
<tx:method name="add*" propagation="REQUIRES_NEW"/>
<tx:method name="alter*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="billServicePointcut" expression="execution(* com.openlab.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="billServicePointcut"></aop:advisor>
</aop:config>
</beans>
2.测试
第三种:使用MapperScannerConfigurer
1.修改applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<bean id="billDao" class="com.openlab.dao.impl.BillDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<context:component-scan base-package="com.openlab.dao,com.openlab.service"></context:component-scan>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.openlab.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></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>
<tx:method name="add*" propagation="REQUIRES_NEW"/>
<tx:method name="alter*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="billServicePointcut" expression="execution(* com.openlab.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="billServicePointcut"></aop:advisor>
</aop:config>
</beans>
2.修改dao层
package com.openlab.dao.impl;
import com.openlab.dao.BillDao;
import com.openlab.pojo.Bill;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
@Repository("billDao")
public class BillDaoImpl implements BillDao {
@Resource(name = "sqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate;
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public int insertBill(Bill bill) {
return sqlSessionTemplate.insert("com.openlab.dao.BillDao.insertBill",bill);
}
@Override
public int updateBillById(Bill bill) {
return sqlSessionTemplate.update("com.openlab.dao.BillDao.updateBillById",bill);
}
@Override
public int deleteBillById(Integer id) {
return sqlSessionTemplate.delete("com.openlab.dao.BillDao.deleteBillById",id);
}
}
3.修改service层
package com.openlab.service.impl;
import com.openlab.dao.BillDao;
import com.openlab.pojo.Bill;
import com.openlab.service.BillService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("billService")
public class BillServiceImpl implements BillService {
@Resource(name = "billDao")
private BillDao billDao;
public BillDao getBillDao() {
return billDao;
}
public void setBillDao(BillDao billDao) {
this.billDao = billDao;
}
@Override
public int addBill(Bill bill) {
return billDao.insertBill(bill);
}
@Override
public int alterBill(Bill bill) {
return billDao.updateBillById(bill);
}
@Override
public int deleteBillById(Integer id) {
return billDao.deleteBillById(id);
}
}
4.测试
二、作业2
第一种:使用SqlSessionTemplate
1.创建工程并导入相关的jar包
2.创建pojo包
package com.openlab.pojo;
import java.util.Date;
public class User {
private Integer id;
private String userCode;
private String userName;
private String userPassword;
private Integer gender;
private Date birthday;
private String phone;
private String address;
private Integer userRole;
private Integer createdBy;
private Date creationDate;
private Integer modifyBy;
private Date modifyDate;
private String idPicPath;
private String workPicPath;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public String getIdPicPath() {
return idPicPath;
}
public void setIdPicPath(String idPicPath) {
this.idPicPath = idPicPath;
}
public String getWorkPicPath() {
return workPicPath;
}
public void setWorkPicPath(String workPicPath) {
this.workPicPath = workPicPath;
}
}
3…创建mybatis-config配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.openlab.pojo"></package>
</typeAliases>
</configuration>
4.创建db.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
5.创建dao包
package com.openlab.dao;
import com.openlab.pojo.User;
import org.apache.ibatis.annotations.Param;
public interface UserDao {
int updateUserById(User user);
int deleteUserById(@Param("id") Integer id);
int updatePassword(User user);
}
映射文件
<?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.openlab.dao.UserDao">
<update id="updateUserById" parameterType="User">
update smbms_user
<set>
<if test="userCode!=null and userCode!=''">
userCode = #{userCode},
</if>
<if test="userName!=null and userName!=''">
userName = #{userName},
</if>
<if test="userPassword!=null and userPassword!=''">
userPassword = #{userPassword},
</if>
<if test="gender!=null">
gender = #{gender},
</if>
<if test="birthday!=null">
birthday = #{birthday},
</if>
<if test="phone!=null and phone!=''">
phone = #{phone},
</if>
<if test="address!=null and address!=''">
address = #{address},
</if>
<if test="userRole!=null">
userRole = #{userRole},
</if>
<if test="createdBy!=null">
createdBy = #{createdBy},
</if>
<if test="creationDate!=null">
creationDate = #{creationDate},
</if>
<if test="modifyBy!=null">
modifyBy = #{modifyBy},
</if>
<if test="modifyDate!=null">
modifyDate = #{modifyDate},
</if>
<if test="idPicPath!=null and idPicPath!=''">
idPicPath = #{idPicPath},
</if>
<if test="workPicPath!=null and workPicPath!=''">
workPicPath = #{workPicPath}
</if>
</set>
where id = #{id}
</update>
<delete id="deleteUserById">
delete from smbms_user where id = #{id}
</delete>
<update id="updatePassword" parameterType="User">
update smbms_user set userPassword = #{userPassword} where id = #{id}
</update>
</mapper>
6.创建dao.impl包
package com.openlab.dao.impl;
import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
public class UserDaoImpl implements UserDao {
private SqlSessionTemplate sqlSessionTemplate;
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public int updateUserById(User user) {
return sqlSessionTemplate.update("com.openlab.dao.UserDao.updateUserById",user);
}
@Override
public int deleteUserById(Integer id) {
return sqlSessionTemplate.delete("com.openlab.dao.UserDao.deleteUserById",id);
}
@Override
public int updatePassword(User user) {
return sqlSessionTemplate.update("com.openlab.dao.UserDao.updatePassword",user);
}
}
7.创建service包
package com.openlab.service;
import com.openlab.pojo.User;
public interface UserService {
int alterUser(User user);
int deleteUser(Integer id);
int alterPassword(User user);
}
8.创建service.impl包
package com.openlab.service.impl;
import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public int alterUser(User user) {
return userDao.updateUserById(user);
}
@Override
public int deleteUser(Integer id) {
return userDao.deleteUserById(id);
}
@Override
public int alterPassword(User user) {
return userDao.updatePassword(user);
}
}
9.配置applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<bean id="userDao" class="com.openlab.dao.impl.UserDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<bean id="userService" class="com.openlab.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="alter*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userServicePointcut" expression="execution(* com.openlab.service.*.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointcut"></aop:advisor>
</aop:config>
</beans>
10.测试
package com.openlab.test;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
public class Test02 {
@Test
public void test1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
User user = new User();
user.setId(27);
user.setUserName("张三");
user.setUserCode("0000000");
user.setModifyDate(new Date());
int i = userService.alterUser(user);
if(i==1){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
}
@Test
public void test2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
int i = userService.deleteUser(27);
if(i==1){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
@Test
public void test3(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
User user = new User();
user.setId(27);
user.setUserPassword("123456");
user.setModifyDate(new Date());
int i = userService.alterPassword(user);
if(i==1){
System.out.println("修改密码成功");
}else {
System.out.println("修改密码失败");
}
}
}
第二种:使用MapperFactoryBean
1.修改applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"></property>
</bean>
<bean id="userDaoProxy" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.openlab.dao.UserDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="userService" class="com.openlab.service.impl.UserServiceImpl">
<property name="userDao" ref="userDaoProxy"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="alter*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userServicePointcut" expression="execution(* com.openlab.service.*.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointcut"></aop:advisor>
</aop:config>
</beans>
2.测试
第三种:使用MapperScannerConfigurer
1.修改applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/openlab/dao/**/*.xml"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<bean id="userDao" class="com.openlab.dao.impl.UserDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<context:component-scan base-package="com.openlab.dao,com.openlab.service"></context:component-scan>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.openlab.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="alter*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userServicePointcut" expression="execution(* com.openlab.service.*.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointcut"></aop:advisor>
</aop:config>
</beans>
2.修改dao层
package com.openlab.dao.impl;
import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Resource(name = "sqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate;
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public int updateUserById(User user) {
return sqlSessionTemplate.update("com.openlab.dao.UserDao.updateUserById",user);
}
@Override
public int deleteUserById(Integer id) {
return sqlSessionTemplate.delete("com.openlab.dao.UserDao.deleteUserById",id);
}
@Override
public int updatePassword(User user) {
return sqlSessionTemplate.update("com.openlab.dao.UserDao.updatePassword",user);
}
}
3.修改service层
package com.openlab.service.impl;
import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public int alterUser(User user) {
return userDao.updateUserById(user);
}
@Override
public int deleteUser(Integer id) {
return userDao.deleteUserById(id);
}
@Override
public int alterPassword(User user) {
return userDao.updatePassword(user);
}
}
4.测试