在前面搭建的基础上,引入新的jar包如下:
aopalliance-1.0.jar
aspectjweaver-1.8.8.jar
mybatis-3.3.0.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.31.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-jdbc-4.2.4.RELEASE.jar
spring-orm-4.2.4.RELEASE.jar
spring-oxm-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar
代码结构如下:
localConfig.properties
#datasource properties jdbc.url=jdbc:mysql://localhost:3306/world jdbc.username=root jdbc.password=root
spring-dataSource.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: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-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/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.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" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.xx.demo.bsh.*.*.*(..))" id="myPointcut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /> </aop:config> </beans>
配置玩事务后先检查一下mysql中的表的存储引擎是否是innoDB。若是MyISAM,要改成InnoDB,因为MyISAM是事务不安全的。
查看命令:show create table city;
修改命令:alter table city engine = InnoDB;
spring-applicationContext.xml
<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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config /> <context:component-scan base-package="com.xx.demo.dao"/> <context:component-scan base-package="com.xx.demo.bsh" /> <context:property-placeholder location="classpath:config/env/localConfig.properties" /> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xx.demo.dao" /> </bean> <import resource="classpath:config/spring-dataSource.xml"/> </beans>
ICityDao.java
package com.xx.demo.dao.test; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import com.xx.demo.entity.test.CityEO; @Repository("cityDao") public interface ICityDao { @Select(value = "select count(1) as count from city") public long countAll(); @Delete(value="delete from city where id=#{id}") public void deleteCityById(long id); @Select(value="select * from city") public List<CityEO> getAllCitys(); }
CityEO.java
EO类属性有数据库列名一致
public class CityEO { private int id; private String name; private String countryCode; private String district; private long population; ... }
TestService.java
package com.xx.demo.bsh.test; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.xx.demo.dao.test.ICityDao; import com.xx.demo.entity.test.CityEO; @Service("testService") public class TestService { @Resource private ICityDao cityDao; public void print(){ System.out.println("这是服务层方法"); } public long getCityCount(){ return cityDao.countAll(); } public long deleteCityById(long id) { cityDao.deleteCityById(id); return id; } public List<CityEO> getAllCitys(){ return cityDao.getAllCitys(); } }
TestController.java
package com.xx.demo.web.test; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.xx.demo.bsh.test.TestService; import com.xx.demo.entity.test.CityEO; @Controller public class TestController { @Resource private TestService testService; @RequestMapping("/firstPage") public String testMethod(ModelMap model){ testService.print(); model.put("msg", "velocity 测试"); return "test"; } @RequestMapping("/getCityCount") @ResponseBody public String getCityCount(){ Map<String,Object> result = new HashMap<String,Object>(); long count = testService.getCityCount(); return String.valueOf(count); } @RequestMapping("/deleteCityById") @ResponseBody public String deleteCityById(HttpServletRequest request){ long id = Long.valueOf(request.getParameter("id")); long result = testService.deleteCityById(id); return "delete--OK--"+result; } @RequestMapping("/getAllCitys") public String getAllCitys(HttpServletRequest request,ModelMap model){ List<CityEO> citys = testService.getAllCitys(); model.put("citys", citys); return "showCitys"; } }
运行结果:
只贴 了 getAllCitys