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 ...
随机推荐
- window 常用MySQL数据库命令总结
登录:cmd - mysql -uroot -p 创建数据库:CREATE DATABASE `tpcms` DEFAULT CHARACTER SET utf8 COLLATE utf8_gener ...
- python数据类型(数字\字符串\列表)
一.基本数据类型——数字 1.布尔型 bool型只有两个值:True和False 之所以将bool值归类为数字,是因为我们也习惯用1表示True,0表示False. (1)布尔值是False的各种情况 ...
- [转]chrome developer tool 调试技巧
这篇文章是根据目前 chrome 稳定版(19.0.1084.52 m)写的, 因为 google 也在不断完善chrome developer tool, 所以 chrome 版本不同可能稍有差别. ...
- 03_CronTrigger
[Cron表达式] Quartz使用类似于Linux下的Cron表达式定义的时间规则,Cron表达式由6到7个空格分隔的时间字段组成. [ 字符说明 ] * :可以用在所有字段中,表示对应时间域内的每 ...
- 【Linux】GCC编译
GCC简介 GCC基本用法 GCC程序产生过程 GCC编译选项 一.GCC简介 1.1 GCC特点 Gcc(GNU C Compiler)是GNU推出的功能强大.性能优越的多平台编译器,是GNU的 代 ...
- 2017年Nature文章“Millions of online book co-purchases reveal partisan differences in the consumption of science”阅读笔记
论文: Millions of online book co-purchases reveal partisan differences in the consumption of scie ...
- JS中的继承方式总结
1. 原型链继承(又称类继承) Child.prototype = new Parent(); function Parent (name, age) { this.name = name; this ...
- ORACLE中一个字符占多少字节?
问题描述 或许你会说一个中文字符占2个字节,这是一定的?如何计算一个字符串的字节数? 解决方案 在oracle中一个字符特别是中文占几个字节是不同的. 比如我创立一个表create table tes ...
- 使用embeded tomcat进行嵌入式javaee开发-启动tomcat
昨天在网上研究了下关于将tomcat嵌入到主程序中进行运行,而不是像以前将一个web项目copy到tomcat中进行运行.之所以这样做的原因,即是因为项目部署到客户方,在进行更新的时候,需要手动地进行 ...
- C# 64位win7下DllImport LoadLibrary函数失败 z
[DllImport["kernel32.dll"]] static extern IntPtr LoadLibrary(string lpFileName); public vo ...