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负责请求的转发和 ...
随机推荐
- 用C在GBA上写光线追踪(0)配置开发编译环境
前段时间用C#写了一个光线追踪程序,可以渲染圆球,平面这种基本图形,反射,光照,阴影,都大致尝试做了一下. ↑ C#实现的光线追踪 ↑ GBA上C实现的光线追踪 然而,在我打算继续深入优化的时 ...
- Springboot实现登录功能
SpringBoot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再 ...
- 551. 学生出勤纪录 I
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤纪录中不超过一个' ...
- 【JS】302- 回调地狱解决方案之Promise
为什么出现Promise 在javascript开发过程中,代码是单线程执行的,同步操作,彼此之间不会等待,这可以说是它的优势,但是也有它的弊端,如一些网络操作,浏览器事件,文件等操作等,都必须异步执 ...
- web开发中浏览器跨域问题
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...
- where和having区别
壹: where后面不能跟聚合函数(sum.avg.count.max.min) having后面可以跟 贰: where和having都能用: select goods_price,goods_na ...
- java基础集合简介Map(三)下
--Map接口简介 今天来看一看map集合,map映射接口,用于存放键值对,<key,value>,通过key来查找value,顾名思义key不能为空,唯一且不重复,不然底层怎么查呢! 可 ...
- 《Dotnet9》建站-本站Logo设计之路
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- 2019年全国高校计算机能力挑战赛初赛java语言解答
1:题目1 16.某星球存在两种生物,A种生物有1个头6条腿,B种生物有3个头4条腿.来自地球的太空船刚刚在该星球降落,突然发现一大群这两种生物组成的队伍,由于时间紧,只数了头的数量和腿的数量,请帮助 ...
- Flutter报错记录
1.Could not find an option named "androidx". Run 'flutter -h' (or 'flutter -h') for avai ...