本节介绍Spring和ORM集成框架。尽管Hibernate在开源ORM 社区很受欢迎。但是,本文将MyBatis案例解说。也MyBatis和Hibernate好坏是没有意义的,主要看实际需求,有兴趣的可以百度、歌查看。

首先配置环境。你得有mybatis和mybatis-spring在Springproject的build path里,假设你使用的是Maven,仅仅需加入以下的依赖:(都是眼下最新版本号)

		<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>

这里主要介绍的是Mybatis和Spring的整合,不会着重解说Mybatis的详细使用方法。

我们知道。MyBatis应用的核心是SqlSessionFactory,在Spring里也须要定义这样一个bean,

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>

使用的是SqlSessionFactoryBean创建SqlSessionFactory。并须要注入数据源dataSource,这里的数据源就是Spring里面定义的随意数据源,为了演示方便,使用的是上一篇文章介绍的H2内嵌数据源,

<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:data.sql" />
</jdbc:embedded-database>

在MyBatis里面,为实现SQL的映射。使用的是XML配置文件或mapper接口。上一篇我们的SQL的模式例如以下:

create table spitter (
id identity,
username varchar(25) not null,
password varchar(25) not null,
fullname varchar(100) not null,
email varchar(50) not null,
update_by_email boolean not null
);

新建一个接口,

package org.chen.mybatis.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.chen.domain.Spitter;
public interface SpitterMapper { @Select("SELECT * from spitter where email = #{email}")
Spitter getSpitter(@Param("email") String email);
}

实际上命名并无要求。但约定是domain+Mapper。在一个方法上面加入注解,这里是 @Select。内容是一个SQL语句, #{email}是參数,用于PrepraredStatement使用。

然后使用MapperFactoryBean将该接口注冊到Spring,

  <bean id="spitterMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.chen.mybatis.mapper.SpitterMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

如今使用上面这个bean就能够完毕关系对象映射了,我们一般把spitterMapper注入到service里面。

如,

public class TestService {

	private SpitterMapper spitterMapper;

	public void setSpitterMapper(SpitterMapper spitterMapper) {
this.spitterMapper = spitterMapper;
} public void getSpitterByEmail(String email){ Spitter spitter = spitterMapper.getSpitter(email); System.out.println("spitter is " + spitter.getFullName());
}
}

至此,基本操作根据可以完毕。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Spring : 征服数据库 (两)的更多相关文章

  1. Spring征服数据库

    一.spring的数据访问哲学 1. Srping的目标之一就是允许我们在开发应用程序的时候,能够遵循面向对象(Object Oriented,OO)原则中的"针对接口式编程"; ...

  2. Spring : 征服数据库(一)

    严格的说.这里征服的是关系型数据库.之后笔者会以MongoDB为例,给出非关系型数据库的解决方式,敬请期待. 获取连接,操作,关闭,不知所云的异常...是的,你受够了.在使用纯JDBC时你訪问数据库时 ...

  3. 第五章 征服数据库(Spring对DB的使用)——开发持久层

    本章内容: 定义Spring对数据库访问的支持 配置数据库资源 使用Spring的JDBC模板 在几乎所有的企业级应用中,都需要构建数据持久层.现在意义上的数据持久层是指把对象或者数据保存到数据库中, ...

  4. 黑马-Spring与数据库

    Spring与数据库 Spring与jdbc 引入dataSource 在客户端 模板编程 类的结构图, 真正干活的是JdbcTemplate(底层实现,操作 excute方法) JdbcTempla ...

  5. Spring管理事物两种方式

    Spring管理事物两种方式 1. 编程式事物管理(在开发中不经常使用) 使用步骤 1. 配置数据库事物管理 DataSourceTransactionManager <!--配置事物管理器-- ...

  6. Spring(四)Spring与数据库编程

    Spring最重要的功能毫无疑问就是操作数据.数据库的百年城是互联网编程的基础,Spring为开发者提供了JDBC模板模式,那就是它自身的JdbcTemplate.Spring还提供了Transact ...

  7. 【Spring】Spring的数据库开发 - 2、Spring JdbcTemplate的常用方法(execute、update、query)

    Spring JdbcTemplate的常用方法 文章目录 Spring JdbcTemplate的常用方法 execute() update() query() 简单记录-Java EE企业级应用开 ...

  8. Spring 链接数据库

    一.前言 Spring 现在是我们在做 JavaWeb 开发中,用的最主流的框架.以后是不是我们暂时不知道,但现在是.废话不多我就介绍 Spring 中.链接数据库的三种方式: git源码地址 需要的 ...

  9. spring 定时任务执行两次解决办法

    在web.xml中同时配置了ContextLoaderListener和DispatcherServlet?假如真是这样的话,需要删掉一个配置,因为你相当于配置了两个spring容器,两个容器分别都执 ...

随机推荐

  1. ubuntu 12.04英文版设置成中文版

    适用于ubuntu 12.04英文版的系统,其他版本号的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support ...

  2. 存读Blob Oracle

  3. WPF点滴

    1 设置窗体的最大化,而且无边框 <Style x:Key="WindowsStyle" TargetType="Window"> <Sett ...

  4. CSS选项卡

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org ...

  5. 设计模式之迭代器模式(Iterator)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  6. POJ1163 The Triangle 【DP】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36918   Accepted: 22117 De ...

  7. Windows7在自由的虚拟机(微软官方虚拟机)

    Windows7在自由的虚拟机(微软官方虚拟机) 前言: 不是说windows7自带的虚拟机最好用,但他的正式版.免费.只是希望你能windows7用户.它将能够自由使用: 还是Vmware. 微软为 ...

  8. AES加密 C++调用Crypto++加密库 样例

    这阵子写了一些数据加密的小程序,对照了好几种算法后,选择了AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES).听这名字就非常厉害的样子 预计会搜索到这 ...

  9. python基础课程_学习笔记15:标准库:有些收藏夹——fileinput

    标准库:有些收藏夹 fileinput 重要功能 性能 叙述性说明 input([files[,inplace[,backup]]) 便于遍历多个输入流中的行 filename() 返回当前文件的名称 ...

  10. 【牛刀小试2】password保

    ]password保 主要知识: 1.        while循环 2.        do-while循环 3.        if-else 4.        strcmp()函数 [充电一下 ...