spring4 学习4 spring MVC+mybatis+Mysql

在前面搭建的基础上,引入新的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. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="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.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  14.         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  15.         <property name="url" value="${jdbc.url}"></property>  
  16.         <property name="username" value="${jdbc.username}"></property>  
  17.         <property name="password" value="${jdbc.password}"></property>  
  18.     </bean>  
  19.       
  20.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  21.         <property name="dataSource" ref="dataSource" />  
  22.     </bean>  
  23.   
  24.     <bean id="transactionManager"  
  25.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  26.         <property name="dataSource" ref="dataSource" />  
  27.     </bean>  
  28.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  29.         <tx:attributes>  
  30.             <tx:method name="*" />  
  31.         </tx:attributes>  
  32.     </tx:advice>  
  33.     <aop:config>  
  34.         <aop:pointcut expression="execution(* com.xx.demo.bsh.*.*.*(..))"  
  35.             id="myPointcut" />  
  36.         <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />  
  37.     </aop:config>  
  38. </beans>  

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

          查看命令:show create table city;

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

spring-applicationContext.xml【下载地址】 

Java代码   收藏代码
  1. <beans xmlns="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.     <context:annotation-config />  
  8.     <context:component-scan base-package="com.xx.demo.dao"/>  
  9.     <context:component-scan base-package="com.xx.demo.bsh" />  
  10.     <context:property-placeholder location="classpath:config/env/localConfig.properties" />    
  11.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
  12.         <property name="basePackage" value="com.xx.demo.dao" />    
  13.     </bean>   
  14.     <import resource="classpath:config/spring-dataSource.xml"/>  
  15. </beans>  

 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<CityEO> 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<CityEO> 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<String,Object> result = new HashMap<String,Object>();  
  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<CityEO> citys = testService.getAllCitys();  
  49.         model.put("citys", citys);  
  50.         return "showCitys";  
  51.     }  
  52. }  

 

运行结果:

只贴 了 getAllCitys

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值