Mybatis(基于SqlSessionTemplate的实现) + Spring 练习实战
mybatis学习篇:上次使用映射接口实现Mybatis,有不方便指出就是需要接口,且需要保证接口上不能存在其他的代理。这次通过SqlSessionTemplate基于模板类实现Mybatis,总的来说就是1.建立pojo类,sql映射文件,2.spring中装配,3.调用SqlSessionTemplate类访问数据库。这三个步骤:
一:sql映射文件
City.java
- package com.suning.schema.mabatisInterface;
- import java.io.Serializable;
- public class City implements Serializable{
- /**
- */
- private static final long serialVersionUID = 1L;
- private String provinceCode;
- private String cityCode;
- private String cityName;
- public String getProvinceCode() {
- return provinceCode;
- }
- public void setProvinceCode(String provinceCode) {
- this.provinceCode = provinceCode;
- }
- public String getCityCode() {
- return cityCode;
- }
- public void setCityCode(String cityCode) {
- this.cityCode = cityCode;
- }
- public String getCityName() {
- return cityName;
- }
- public void setCityName(String cityName) {
- this.cityName = cityName;
- }
- }
sqlMap_city.xml:
- <?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">
- <mapper namespace="city">
- <select id="selectCity" parameterType="java.lang.String" resultType="City">
- SELECT
- PROVINCE_CODE AS "provinceCode",
- CITY_CODE AS "cityCode",
- CITY_NAME AS "cityName"
- FROM
- PUMS_CITY C
- WHERE
- C.CITY_CODE = #{cityCode}
- </select>
- </mapper>
定义命名空间namespace为city,sql的ID为selectCity,其中resultType="city",可以写全路径,也可以通过配置文件简写。
二:Spring中装配
sample-mybatis.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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >
- <!-- autowised注解,注入Bean 等同 <context:component-scan base-package=”XX.XX”/>-->
- <context:annotation-config />
- <!-- 基于sqlSessionTemplate的mybatis配置 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:conf/spring/mybatisConfig.xml"/>
- <property name="mapperLocations" value="classpath:conf/sqlMapMybatis/sqlMap_city.xml"/>
- </bean>
- <!-- sqlSessionTemplate配置 -->
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="" ref="sqlSessionFactory" />
- </bean>
- <bean id="mybatisService" class="com.suning.mybatis.MybatisService">
- </bean>
- </beans>
定义一个核心的SqlSessionFactoryBean实例,mybatis的核心管理类,通过dataSource指定数据源,configLocation代表mybatis的配置文件,mapperLocations指sql文件地址。注入SqlSessionTemplate的实例,构造方法初始化sqlSessionFactory。定义业务的实现类mybatisService
三:调用sqlSessionTemplate
MybatisService.Java:
- package com.suning.mybatis;
- import org.mybatis.spring.SqlSessionTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- //mabitis测试的业务类
- public class MybatisService {
- @Autowired
- private SqlSessionTemplate sqlSessionTemplate;
- //获取市详情
- public City getCityDetail(String cityCode){
- return (City)sqlSessionTemplate.selectOne("city.selectCity", cityCode);
- }
- }
通过自动注解,注入sqlSessionTemplate。city对应sql中命名空间,selectCity对应sql的ID,如图:
测试类MybatisMain.java
- package com.suning.mybatis;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MybatisMain {
- /**
- * 功能描述: <br>
- * 〈功能详细描述〉
- *
- * @param args
- * @see [相关类/方法](可选)
- * @since [产品/模块版本](可选)
- */
- public static void main(String[] args) {
- BeanFactory factory = new ClassPathXmlApplicationContext(new String[] {
- "classpath:conf/spring/sample-mybatis.xml", "classpath:conf/spring/sample-ds.xml" });
- MybatisService test = (MybatisService) factory.getBean("mybatisService");
- City cityDetail = (City) test.getCityDetail("");
- System.out.println("cityCode:560代表的城市为" + cityDetail.getCityName());
- }
- }
Mybatis(基于SqlSessionTemplate的实现) + Spring 练习实战的更多相关文章
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
- Spring Cloud实战: 基于Spring Cloud Gateway + vue-element-admin 实现的RBAC权限管理系统,实现网关对RESTful接口方法权限和自定义Vue指令对按钮权限的细粒度控制
一. 前言 信我的哈,明天过年. 这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT ...
- spring实战六之使用基于java配置的Spring
之前接触的都是基于XML配置的Spring,Spring3.0开始可以几乎不使用XML而使用纯粹的java代码来配置Spring应用.使用基于java配置的Spring的步骤如下: 1. 创建基于ja ...
- spring mvc 实战化项目之三板斧
laravel实战化项目之三板斧 spring mvc 实战化项目之三板斧 asp.net mvc 实战化项目之三板斧 接上文希望从一张表(tb_role_info 用户角色表)的CRUD展开spri ...
- 《spring boot 实战》读书笔记
前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...
- Spring Boot实战系列-----------邮件发送
快速导航 添加Maven依赖 配置文件增加邮箱相关配置 Service.Test项目代码构建 五种邮件发送类型讲解 文本邮件 html邮件 附件邮件 html内嵌图片邮件 模板邮件 问题汇总 添加ma ...
- Spring Boot 实战与原理分析视频课程
Spring Boot 实战与原理分析视频课程 链接:https://pan.baidu.com/share/init?surl=PeykcoeqZtd1d9lN9V_F-A 提取码: 关注公众号[G ...
- Spring Security 实战干货:使用 JWT 认证访问接口
(转载)原文链接:https://my.oschina.net/10000000000/blog/3127268 1. 前言 欢迎阅读Spring Security 实战干货系列.之前我讲解了如何编写 ...
- 《Spring Boot实战》笔记(目录)
目录 目 录第一部分 点睛Spring 4.x第1 章 Spring 基础 .............................................................. ...
随机推荐
- 流式数据分析模型kafka+storm
http://www.cnblogs.com/panfeng412/archive/2012/07/29/storm-stream-model-analysis-and-discussion.html ...
- 某互联网后台自动化组合测试框架RobotFramework+Python+Sikuli
一.RobotFramework 1.工具介绍: Robotframework在测试中作为组织测试用例和BDD关键字的平台,主要使用RIDE进行管理,它不是一个工具,而仅仅是一个框架,使用Python ...
- shell脚本 expect 实现自动登陆
vi auto_ssh.exp #!/usr/bin/expect set ipaddress "123.227.159.159" set passwd "你的密码& ...
- 如何实现 Service 伸缩?- 每天5分钟玩转 Docker 容器技术(97)
上一节部署了只有一个副本的 Service,不过对于 web 服务,我们通常会运行多个实例.这样可以负载均衡,同时也能提供高可用. swarm 要实现这个目标非常简单,增加 service 的副本数就 ...
- String的Intern方法
jdk6 和 jdk7 下 intern 的区别 相信很多 JAVA 程序员都做做类似 String s = new String("abc")这个语句创建了几个对象的题目. 这种 ...
- JavaScript学习心得
javaScript十分的强大,所以自然而然学起来也是不易的,想要掌握它的核心,把它理解透更是不易的,但只要你能够静下心来,耐心的去钻研,学习,还是可以把它给学好的,加油吧! 下面是一些JavaScr ...
- Less 编译工具
Less 编译工具 虽然你可以选择在浏览器端使用Less,直接在页面中嵌入一个 Less.js 文件,你也可以选择在服务器端使用Less,使用命令行将Less文件编译成最终的CSS文件. 然而,这两种 ...
- Android自定义指示器时间轴
指示器时间轴在外卖.购物类的APP里会经常用到,效果大概就像下面这样,看了网上很多文章,大都是自己绘制,太麻烦,其实通过ListView就可以实现. 在Activity关联的布局文件activit ...
- Serializable 都这么牛逼了,Parcelable 还要你何用?
一些闲聊 距离上一篇文章似乎又是很久了,看起来也没有很多反馈,催更就更不用说了.哈哈,放弃了. 话说最近公司在招聘一批至少 5 年开发经验的 Android 开发工程师,我也是忙开了花,激动得不行呀. ...
- Linux 链接详解----静态链接实例分析
由Linux链接详解(1)中我们简单的分析了静态库的引用解析和重定位的内容, 下面我们结合实例来看一下静态链接重定位过程. /* * a.c */ ; void add(int c); int mai ...