spring入门(六) spring mvc+mybatis
1.引入依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency> <!--spring的版本是5.0.9,mybatis-spring的版本要是1.3+ -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
2.在springmvc-config.xml增加mybatis配置
<!-- 针对myBatis的配置项 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描classpath:mapper/*.xml -->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!-- 配置扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描com.ice.dao这个包以及它的子包下的所有映射接口类,省去了在每个mapper接口上注解 -->
<property name="basePackage" value="com.ice.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
3.在classpath:mapper/ 创建CustomerMapper.xml
注意创建的位置,是在上面设置的 mapperLocations.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace要跟后面该xml对应的mapper类的全名一致 -->
<mapper namespace="com.ice.dao.CustomerMapper">
<!-- id设置为mapper类对应的方法名, parameterType跟方法的参数类型一致,resultType跟方法的返回类型一致.
如果返回单个对象,mybatis会自动调用selectOne方法;如果返回的是List,mybatis会自动调用selectList方法.
-->
<select id="getUser" parameterType="java.lang.String"
resultType="com.ice.model.Customer">
select id,name from customer where name=#{name}
</select>
</mapper>
4.创建CustomerMapper.xml对应的CustomerMapper.java
注意创建的位置,是在上面设置的 basePackage.
package com.ice.dao; import com.ice.model.Customer;
//不用在xml里配置该bean,也不用注解.已经配置了MapperScanner
//<property name="basePackage" value="com.ice.dao" />
public interface CustomerMapper { Customer getUser(String name);
}
5.简单测试(在controller里测试,也可考虑springTest)
正式项目里,要有service层.
package com.ice.controller; import com.ice.dao.CustomerMapper;
import com.ice.model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/mybatis")
public class MybatisController { @Autowired
private CustomerMapper customerMapper; @RequestMapping("/query")
@ResponseBody
public String index() {
Customer user = customerMapper.getUser("hello");
return "顾客id" + user.getId();
}
}
spring入门(六) spring mvc+mybatis的更多相关文章
- Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
		
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...
 - Spring入门二:整合mybatis
		
一.SM思路分析 1.引入核心依赖及相关依赖: spring(略).mybatis.mysql.mybatis-spring(减少自己实现FactoryBean接口).druid <depen ...
 - Spring学习(六)-----Spring使用@Autowired注解自动装配
		
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
 - spring入门(七) spring mvc+mybatis+generator
		
1.Mybatis-Generator下载 地址:https://github.com/mybatis/generator/releases 我使用的是 mybatis-generator-core- ...
 - Spring(十六)之MVC框架
		
MVC 框架教程 Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活.松散耦合的 web 应用程序的组件.MVC 模式导致了应用程序的不同方面(输入逻辑.业 ...
 - Spring系列(六) Spring Web MVC 应用构建分析
		
DispatcherServlet DispatcherServlet 是Spring MVC的前端控制器名称, 用户的请求到达这里进行集中处理, 在Spring MVC中, 它的作用是为不同请求匹配 ...
 - Spring boot(六)优雅使用mybatis
		
orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句SQL的hibernate,一个是可以灵活调试动态sql的mybatis,两者各有特点,在企业级系统开 ...
 - spring学习 六 spring与mybatis整合
		
在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件.mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让sp ...
 - spring入门(四) spring mvc返回json结果
		
前提:已搭建好环境 1.建立Controller package com.ice.controller; import com.ice.model.Person; import org.springf ...
 
随机推荐
- 解决The current branch is not configured for pull No value for key branch.master.merge found in config
			
使用Git Pull项目的时候出现这个问题: The current branch is not configured for pull No value for key branch.master. ...
 - 安装VS2013时,如何避开IE10的限制
			
安装VS2013时,如何避开IE10的限制 VS就会告诉我们目前环境不适合安装VS2013,必须升级IE版本到IE10. 如果不想安装IE10,有没有办法呢? 答案肯定是有的. 将下面一段文字,储存为 ...
 - Python 动态加载 Extension Manager Classes
			
看着看着发现了一个库:stevedore(http://stevedore.readthedocs.org/en/latest/managers.html),但是感觉文档做得不行啊,都没个tutori ...
 - oracle之数据同步:Oracle Sql Loader使用说明(大批量快速插入数据库记录)
			
1.准备表数据 select * from emp10; create sequence seq_eseq increment start maxvalue ; --得到序列的SQL语句 select ...
 - 数据分析核心包——pandas
			
一.pandas简介 pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. 1.pandas的主要功能 (1)具备对其功能的数据结构DataFrame.Series (2)集 ...
 - 解决Tensorflow源码安装的之后TensorBoard 无法使用的问题
			
作者 cnblog 修雨轩陈 我是按照 Tensorflow 下 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3 ...
 - 新发布 | Azure镜像市场正式上线
			
由世纪互联运营的 Azure 镜像市场于2016年9月21日正式落地中国市场,在客户和软件开发商间搭建起了一站式门户.来自全球和本地领先软件开发商并基于 Azure 的云应用.云服务和解决方案在门户中 ...
 - Browser进程和浏览器内核(Renderer进程)的通信过程
			
看到这里,首先,应该对浏览器内的进程和线程都有一定理解了,那么接下来,再谈谈浏览器的Browser进程(控制进程)是如何和内核通信的, 这点也理解后,就可以将这部分的知识串联起来,从头到尾有一个完整的 ...
 - Windows装系统
			
这几天电脑频繁崩溃,自己尝试着装了几次系统,遇到一些问题.有的解决了,有的没解决.将其一一记录在这里,作为经验参考. 自己以前最常用的方式是直接通过ultraiso将IOS文件解压到到U盘,会将U盘做 ...
 - day3-基础 列表,元组,字典,模块
			
1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 Country = ['China','England','America'] 通过下标访问列 ...