目录

MyBatis学习系列一之环境搭建

MyBatis学习系列二——增删改查

MyBatis学习系列三——结合Spring

MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBatis结合到Spring框架中。

  数据库环境:MySQL

1、源码结构图

  

  引用Jar包:

  

  说明:Spring的Jar包,这里不再详细说明。

  MyBatis相关的Jar包:mybatis-3.2.8.jar、mybatis-spring-1.1.1.jar

  MySQL相关的Jar包:mysql-connector-java-5.0.8.jar

2、MyBatis和Spring的结合

  DAO和PO中的文件(DoctorMapper.java、DoctorMapper.xml、Doctor.java)我们在前两期中已经详细的说明过。

  MyBatis的映射配置文件mybatis-config.xml 关联到 applicationContext.xml 中,获取sqlSessionFactory,

  applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="nankang/service" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/childrendb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"
p:username="root"
p:password="world" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean> </beans>

  mybatis-config.xml进行了简化,去掉了连接数据库的部分,源码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="nankang/dao/DoctorMapper.xml" />
</mappers>
</configuration>

3、代码中使用SqlSessionFactory

  在后台提供数据服务的DoctorService中,直接使用SqlSessionFactory即可,源码如下:

  

package nankang.service;

import java.util.List;

import nankang.dao.DoctorMapper;
import nankang.po.Doctor; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class DoctorService { @Autowired
private SqlSessionFactory sqlSessionFactory; public Doctor getAllDoctors() { SqlSession sqlSession = sqlSessionFactory.openSession();
DoctorMapper doctorMapper = sqlSession.getMapper(DoctorMapper.class); List<Doctor> doctorList = doctorMapper.selectAllDoctors();
return doctorList.get(0);
}
}

4、Spring结构相关

  Spring相关的,获取结果并展示,可以参考源码。

5、结果展示

  浏览器中输入:http://localhost:8080/myBatis3/index.html

  展示结果:

  

6、源码下载:http://pan.baidu.com/s/1pJmeYpX (Fish的分享>MyBatis>myBatis3.rar)

MyBatis学习系列三——结合Spring的更多相关文章

  1. mybatis学习系列三(部分)

    1 forearch_oracle下批量保存(47) oracle批量插入 不支持values(),(),()方式 1.多个insert放在begin-end里面 begin insert into ...

  2. MyBatis学习系列二——增删改查

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...

  3. MyBatis学习系列一之环境搭建

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 学习一个新的知识,首先做一个简单的例子使用一下,然后再逐步深入.MyBat ...

  4. MyBatis学习 之 三、动态SQL语句

    目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...

  5. 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置

    [转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...

  6. mybatis入门系列三之类型转换器

    mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...

  7. scrapy爬虫学习系列三:scrapy部署到scrapyhub上

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  8. DocX开源WORD操作组件的学习系列三

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  9. RabbitMQ学习系列三-C#代码接收处理消息

    RabbitMQ学习系列三:.net 环境下 C#代码订阅 RabbitMQ 消息并处理 http://www.80iter.com/blog/1438251320680361 http://www. ...

随机推荐

  1. [物理学与PDEs]第4章习题参考解答

    [物理学与PDEs]第4章习题1 反应力学方程组形式的化约 - 动量方程与未燃流体质量平衡方程 [物理学与PDEs]第4章习题2 反应力学方程组形式的化约 - 能量守恒方程 [物理学与PDEs]第4章 ...

  2. FileSystemWatcher

    转:http://www.cnblogs.com/zhaojingjing/archive/2011/01/21/1941586.html 注意:用FileWatcher的Created监控文件时,是 ...

  3. ylbtech-Unitity-CS:Generics

    ylbtech-Unitity-CS:Generics 1.A,效果图返回顶部 Unsorted List: Raul:35 Alessandro:30 Maria:72 Hiroyuki:108 A ...

  4. Git的撤消操作 - 重置, 签出 和 撤消(转载)

    From:http://gitbook.liuhui998.com/4_9.html http://ihower.tw/blog/archives/2622 相较于SVN这种commit就推送到远端伺 ...

  5. View的缩放操作--CGAffineTransformMakeScale:

    __weak UIImageView *weekImage = imageView; imageView.transform = CGAffineTransformMakeScale(0.1, 0.1 ...

  6. URL编码CFURLCreateStringByAddingPercentEscapes使用(ARC)

    URL 编码:CFURLCreateStringByAddingPercentEscapes If you have tried to send any information using a GET ...

  7. 认识与学习BASH

    应用程序在最外面,就如同鸡蛋的外壳一样,因此被称呼为shell(壳程序).其实壳程序的功能只是提供操作系统的一个接口. 应用程序 ↓ 操作系统(系统呼叫+核心) ↓ 硬件 linux预设的shell就 ...

  8. [Flex] as3xls读取excel,修改保存单表(二)

    这个方法仅用了as3xls读取excel的功能,修改保存独立出来了. <?xml version="1.0" encoding="utf-8"?> ...

  9. Python 的 List 要印出 中文 編碼

    Python 的 List 如果有中文的話, 會印出 \xe4\xb8… 等等的編碼, 要如何印出中文呢(如下範例)? (Debug 方便查看) View Raw Code? >>> ...

  10. Python的安装与基本语法

    一,Python简介      Python是一种计算机程序设计语言,都是使用C语言实现,但是比C语言容易学习,易于阅读.Python可以应用于众多领域,整体呈上升趋势,广泛使用Python来做的事一 ...