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

  1. #datasource properties
  2. jdbc.url=jdbc:mysql://localhost:3306/world
  3. jdbc.username=root
  4. jdbc.password=root

spring-dataSource.xml

  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. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  14. <property name="url" value="${jdbc.url}"></property>
  15. <property name="username" value="${jdbc.username}"></property>
  16. <property name="password" value="${jdbc.password}"></property>
  17. </bean>
  18. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  19. <property name="dataSource" ref="dataSource" />
  20. </bean>
  21. <bean id="transactionManager"
  22. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  23. <property name="dataSource" ref="dataSource" />
  24. </bean>
  25. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  26. <tx:attributes>
  27. <tx:method name="*" />
  28. </tx:attributes>
  29. </tx:advice>
  30. <aop:config>
  31. <aop:pointcut expression="execution(* com.xx.demo.bsh.*.*.*(..))"
  32. id="myPointcut" />
  33. <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
  34. </aop:config>
  35. </beans>

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

查看命令:show create table city;

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

spring-applicationContext.xml

  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

  1. package com.xx.demo.dao.test;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.Delete;
  4. import org.apache.ibatis.annotations.Select;
  5. import org.springframework.stereotype.Repository;
  6. import com.xx.demo.entity.test.CityEO;
  7. @Repository("cityDao")
  8. public interface ICityDao {
  9. @Select(value = "select count(1) as count from city")
  10. public long countAll();
  11. @Delete(value="delete from city where id=#{id}")
  12. public void deleteCityById(long id);
  13. @Select(value="select * from city")
  14. public List<CityEO> getAllCitys();
  15. }

CityEO.java

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

  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

  1. package com.xx.demo.bsh.test;
  2. import java.util.List;
  3. import javax.annotation.Resource;
  4. import org.springframework.stereotype.Service;
  5. import com.xx.demo.dao.test.ICityDao;
  6. import com.xx.demo.entity.test.CityEO;
  7. @Service("testService")
  8. public class TestService {
  9. @Resource
  10. private ICityDao cityDao;
  11. public void print(){
  12. System.out.println("这是服务层方法");
  13. }
  14. public long getCityCount(){
  15. return cityDao.countAll();
  16. }
  17. public long deleteCityById(long id) {
  18. cityDao.deleteCityById(id);
  19. return id;
  20. }
  21. public List<CityEO> getAllCitys(){
  22. return cityDao.getAllCitys();
  23. }
  24. }

TestController.java

  1. package com.xx.demo.web.test;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.annotation.Resource;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.ModelMap;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import com.xx.demo.bsh.test.TestService;
  12. import com.xx.demo.entity.test.CityEO;
  13. @Controller
  14. public class TestController {
  15. @Resource
  16. private TestService testService;
  17. @RequestMapping("/firstPage")
  18. public String testMethod(ModelMap model){
  19. testService.print();
  20. model.put("msg", "velocity 测试");
  21. return "test";
  22. }
  23. @RequestMapping("/getCityCount")
  24. @ResponseBody
  25. public String getCityCount(){
  26. Map<String,Object> result = new HashMap<String,Object>();
  27. long count = testService.getCityCount();
  28. return String.valueOf(count);
  29. }
  30. @RequestMapping("/deleteCityById")
  31. @ResponseBody
  32. public String deleteCityById(HttpServletRequest request){
  33. long id = Long.valueOf(request.getParameter("id"));
  34. long result = testService.deleteCityById(id);
  35. return "delete--OK--"+result;
  36. }
  37. @RequestMapping("/getAllCitys")
  38. public String getAllCitys(HttpServletRequest request,ModelMap model){
  39. List<CityEO> citys = testService.getAllCitys();
  40. model.put("citys", citys);
  41. return "showCitys";
  42. }
  43. }

运行结果:

只贴 了 getAllCitys

spring4 学习4 spring MVC+mybatis+Mysql的更多相关文章

  1. Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)

    通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...

  2. Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)

    当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...

  3. (转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)

    http://blog.csdn.net/u010648555/article/details/60767633 当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的 ...

  4. Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境【转】

    项目建设完成之后的结构: 数据库的表结构如下: 环境建设:搭建Maven环境.Tomcat环境.需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备): 开始创建工程: 1.创建 ...

  5. Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境

    项目建设完成之后的结构: 数据库的表结构如下: 环境建设:搭建Maven环境.Tomcat环境.需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备): 开始创建工程: 1.创建 ...

  6. maven Spring+Spring MVC+Mybatis+mysql轻量级Java web开发环境搭建

    之前一直在做的一个GIS系统项目,采用了jsp+servlet框架,数据传输框架采用了apache的thrift框架,短时多传的风格还不错,但是较其他的java web项目显得有点太臃肿了,现在给大家 ...

  7. SSM后台管理系统(Spring SpringMVC Mybatis Mysql EasyUI)

    非常简单的一个后台管理系统,功能不多,框架也不复杂, 源码下载(附数据库)-ssm后台管理系统框架(Spring mvc + mybatis + mysql + easyui ) 实例图片

  8. Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)

    Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...

  9. Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

随机推荐

  1. ceph分布式存储

    存储分类: DAS:直连存储    ide线  sata线   usd线   sas线 NAS:网络附加存储   nfs   samba   ftp SAN:存储区域网络      issci SDS ...

  2. luogu P1908 逆序对 |树状数组

    题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为"逆序对"的 ...

  3. luogu P2640 神秘磁石

    题目描述 1.若给他一个一维坐标系,那么他的磁力一定要在素数坐标的位置上才能发挥的最大(不管位置坐标的大小,只要是素数那么磁力就一样大) 2.若两个磁石相距为k,那么磁石间的破坏力将会达到当前磁力的峰 ...

  4. 牛客剑指offer(持续更新~)

    第一题:二维数组的查找 题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数, ...

  5. Codeforces-527c

    Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glas ...

  6. Vue-兄弟组件传值

    学习博客:https://www.jianshu.com/p/b10b3ac3729a

  7. Cmder介绍和配置

    一.命令行神器cmder介绍 windows上做开发,不管是cmd还是powershell,似乎都不够美观,不够强大.今天就来介绍一款可以替代cmd的神器"Cmder",话不多说, ...

  8. PAT1057 Stack(树状数组+倍增)

    目录 题目大意 题目分析 题目大意 要求维护一个栈,提供压栈.弹栈以及求栈内中位数的操作(当栈内元素\(n\)为偶数时,只是求第\(n/2\)个元素而非中间两数的平均值).最多操作100000次,压栈 ...

  9. pt-online-schema-change工具使用教程(在线修改大表结构)

    percona-toolkit中pt-online-schema-change工具安装和使用 pt-online-schema-change介绍 使用场景:在线修改大表结构 在线数据库的维护中,总会涉 ...

  10. 2019年全国高校计算机能力挑战赛初赛java语言解答

    1:题目1 16.某星球存在两种生物,A种生物有1个头6条腿,B种生物有3个头4条腿.来自地球的太空船刚刚在该星球降落,突然发现一大群这两种生物组成的队伍,由于时间紧,只数了头的数量和腿的数量,请帮助 ...