在前面搭建的基础上,引入新的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

Java代码  

  1. #datasource properties  

  2. jdbc.url=jdbc:mysql://localhost:3306/world  

  3. jdbc.username=root  

  4. jdbc.password=root  

 

spring-dataSource.xml

 

Java代码  

  1. "1.0" encoding="UTF-8"?>  

  2. "http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

  4.     xmlns:tx="http://www.springframework.org/schema/tx"  

  5.     xmlns:aop="http://www.springframework.org/schema/aop"  

  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   

  7.     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  

  8.     http://www.springframework.org/schema/aop   

  9.     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd    

  10.     http://www.springframework.org/schema/tx   

  11.     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">  

  12.   

  13.     "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  

  14.         "driverClassName" value="com.mysql.jdbc.Driver">  

  15.         "url" value="${jdbc.url}">  

  16.         "username" value="${jdbc.username}">  

  17.         "password" value="${jdbc.password}">  

  18.       

  19.       

  20.     "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

  21.         "dataSource" ref="dataSource" />  

  22.       

  23.   

  24.     "transactionManager"  

  25.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  

  26.         "dataSource" ref="dataSource" />  

  27.       

  28.     "txAdvice" transaction-manager="transactionManager">  

  29.           

  30.             "*" />  

  31.           

  32.       

  33.       

  34.         "execution(* com.xx.demo.bsh.*.*.*(..))"  

  35.             id="myPointcut" />  

  36.         "txAdvice" pointcut-ref="myPointcut" />  

  37.       

  38.   

           配置玩事务后先检查一下mysql中的表的存储引擎是否是innoDB。若是MyISAM,要改成InnoDB,因为MyISAM是事务不安全的。【下载地址】 

          查看命令:show create table city;

          修改命令:alter table city engine = InnoDB;

spring-applicationContext.xml

Java代码  

  1. "http://www.springframework.org/schema/beans"  

  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  

  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  

  4.              http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  

  5.              http://www.springframework.org/schema/context  

  6.              http://www.springframework.org/schema/context/spring-context-4.1.xsd">  

  7.       

  8.     package="com.xx.demo.dao"/>  

  9.     package="com.xx.demo.bsh" />  

  10.     "classpath:config/env/localConfig.properties" />    

  11.     class="org.mybatis.spring.mapper.MapperScannerConfigurer">    

  12.         "basePackage" value="com.xx.demo.dao" />    

  13.        

  14.     <import resource="classpath:config/spring-dataSource.xml"/>  

  15.   

 ICityDao.java

Java代码  

  1. package com.xx.demo.dao.test;  

  2.   

  3. import java.util.List;  

  4.   

  5. import org.apache.ibatis.annotations.Delete;  

  6. import org.apache.ibatis.annotations.Select;  

  7. import org.springframework.stereotype.Repository;  

  8.   

  9. import com.xx.demo.entity.test.CityEO;  

  10.   

  11. @Repository("cityDao")   

  12. public interface ICityDao {  

  13.       

  14.     @Select(value = "select count(1) as count from city")   

  15.     public long countAll();  

  16.   

  17.     @Delete(value="delete from city where id=#{id}")  

  18.     public void deleteCityById(long id);  

  19.       

  20.     @Select(value="select * from city")  

  21.     public List getAllCitys();  

  22.       

  23. }  

CityEO.java

   EO类属性有数据库列名一致

Java代码  

  1. public class CityEO {  

  2.     private int id;  

  3.     private String name;  

  4.     private String countryCode;  

  5.     private String district;  

  6.     private long population;  

  7. ...  

  8. }  

  TestService.java

Java代码  

  1. package com.xx.demo.bsh.test;  

  2.   

  3. import java.util.List;  

  4.   

  5. import javax.annotation.Resource;  

  6.   

  7. import org.springframework.stereotype.Service;  

  8.   

  9. import com.xx.demo.dao.test.ICityDao;  

  10. import com.xx.demo.entity.test.CityEO;  

  11.   

  12. @Service("testService")  

  13. public class TestService {  

  14.       

  15.     @Resource  

  16.     private ICityDao cityDao;  

  17.       

  18.     public void print(){  

  19.         System.out.println("这是服务层方法");  

  20.     }  

  21.       

  22.     public long getCityCount(){  

  23.         return cityDao.countAll();  

  24.     }  

  25.   

  26.     public long deleteCityById(long id) {  

  27.         cityDao.deleteCityById(id);  

  28.         return id;  

  29.     }  

  30.       

  31.     public List getAllCitys(){  

  32.         return cityDao.getAllCitys();  

  33.     }  

  34. }  

 TestController.java

Java代码  

  1. package com.xx.demo.web.test;  

  2.   

  3. import java.util.HashMap;  

  4. import java.util.List;  

  5. import java.util.Map;  

  6.   

  7. import javax.annotation.Resource;  

  8. import javax.servlet.http.HttpServletRequest;  

  9.   

  10. import org.springframework.stereotype.Controller;  

  11. import org.springframework.ui.ModelMap;  

  12. import org.springframework.web.bind.annotation.RequestMapping;  

  13. import org.springframework.web.bind.annotation.ResponseBody;  

  14.   

  15. import com.xx.demo.bsh.test.TestService;  

  16. import com.xx.demo.entity.test.CityEO;  

  17.   

  18. @Controller  

  19. public class TestController {  

  20.     @Resource  

  21.     private TestService testService;  

  22.       

  23.     @RequestMapping("/firstPage")  

  24.     public String testMethod(ModelMap model){  

  25.         testService.print();  

  26.         model.put("msg""velocity 测试");  

  27.         return "test";  

  28.     }  

  29.       

  30.     @RequestMapping("/getCityCount")  

  31.     @ResponseBody  

  32.     public String getCityCount(){  

  33.         Map result = new HashMap();  

  34.         long count = testService.getCityCount();  

  35.         return String.valueOf(count);  

  36.     }  

  37.       

  38.     @RequestMapping("/deleteCityById")  

  39.     @ResponseBody  

  40.     public String deleteCityById(HttpServletRequest request){  

  41.         long id = Long.valueOf(request.getParameter("id"));  

  42.         long result = testService.deleteCityById(id);  

  43.         return "delete--OK--"+result;  

  44.     }  

  45.       

  46.     @RequestMapping("/getAllCitys")  

  47.     public String getAllCitys(HttpServletRequest request,ModelMap model){  

  48.         List citys = testService.getAllCitys();  

  49.         model.put("citys", citys);  

  50.         return "showCitys";  

  51.     }  

  52. }  

 

运行结果:

只贴 了 getAllCitys