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
- #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

spring4 学习4 spring MVC+mybatis+Mysql的更多相关文章
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...
- (转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
http://blog.csdn.net/u010648555/article/details/60767633 当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的 ...
- Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境【转】
项目建设完成之后的结构: 数据库的表结构如下: 环境建设:搭建Maven环境.Tomcat环境.需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备): 开始创建工程: 1.创建 ...
- Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境
项目建设完成之后的结构: 数据库的表结构如下: 环境建设:搭建Maven环境.Tomcat环境.需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备): 开始创建工程: 1.创建 ...
- maven Spring+Spring MVC+Mybatis+mysql轻量级Java web开发环境搭建
之前一直在做的一个GIS系统项目,采用了jsp+servlet框架,数据传输框架采用了apache的thrift框架,短时多传的风格还不错,但是较其他的java web项目显得有点太臃肿了,现在给大家 ...
- SSM后台管理系统(Spring SpringMVC Mybatis Mysql EasyUI)
非常简单的一个后台管理系统,功能不多,框架也不复杂, 源码下载(附数据库)-ssm后台管理系统框架(Spring mvc + mybatis + mysql + easyui ) 实例图片
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)
Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...
- Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)
与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...
随机推荐
- Python一秒搭建ftp服务器,帮助你在局域网共享文件【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...
- 转:SSL 握手协议详解
SSL 握手协议详解 RSA作为身份认证,ECDHE来交换加密密钥,AES/DES等作为加密. 如果RSA来加解密,那么身份认证后,直接用认证后的RSA公钥解密.不需要再额外交换加密密钥了. 相关报文 ...
- enumerate用法总结
enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值 enumerat ...
- Ceph分布式存储-总
Ceph分布式存储-总 目录: Ceph基本组成及原理 Ceph之块存储 Ceph之文件存储 Ceph之对象存储 Ceph之实际应用 Ceph之总结 一.Ceph基本组成及原理 1.块存储.文件存储. ...
- 正则去掉html标签之间的空格、换行符、tab符,但是保留html标签内部的属性空格
今天遇到一个比较少见的去空格: 正则去掉html标签之间的空格.换行符.tab符,但是保留html标签内部的属性空格 JS 举例: "<a href='baidu.com' name= ...
- JAVA中SPI机制
之前研究dubbo的时候就很好奇,里面各种扩展机制,期间也看过很多关于SPI的机制,今日有缘再度看到有文章总结,故记录一下, 首先了解一下 JAVA中SPI简单的用法 可参考这篇文章,https:// ...
- 【ZooKeeper系列】2.用Java实现ZooKeeper API的调用
温馨提示:在这里我再次提个小要求,希望大家能习惯看官方文档,文档虽然是英文但用词都比较简单,基本都能看懂文档表达的意思.授之以鱼不如授之以渔的道理相信大家都明白,也希望通过猿人谷的这个ZooKeepe ...
- 深入探索Java设计模式(二)之策略模式
策略设计模式是Java API库中常见的模式之一.这与另一个设计模式(称为状态设计模式)非常相似.本文是在学习完优锐课JAVA架构VIP课程—[框架源码专题]中<学习源码中的优秀设计模式> ...
- 记一次 Kafka 集群线上扩容
前段时间收到某个 Kafka 集群的生产客户端反馈发送消息耗时很高,于是花了一段时间去排查这个问题,最后该集群进行扩容,由于某些主题的当前数据量实在太大,在对这些主题迁移过程中话费了很长一段时间,不过 ...
- 【Python进阶】来谈谈几个常用的内置函数
匿名函数(lambda表达式) 在Python中,函数可以算的上是“一等公民”了,我们先回顾下函数的优点: 减少代码重复量 模块化代码 但是我们有没有想过,如果我们需要一个函数,比较简短,而且只需要使 ...