Spring : 征服数据库 (两)
本节介绍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 : 征服数据库 (两)的更多相关文章
- Spring征服数据库
一.spring的数据访问哲学 1. Srping的目标之一就是允许我们在开发应用程序的时候,能够遵循面向对象(Object Oriented,OO)原则中的"针对接口式编程"; ...
- Spring : 征服数据库(一)
严格的说.这里征服的是关系型数据库.之后笔者会以MongoDB为例,给出非关系型数据库的解决方式,敬请期待. 获取连接,操作,关闭,不知所云的异常...是的,你受够了.在使用纯JDBC时你訪问数据库时 ...
- 第五章 征服数据库(Spring对DB的使用)——开发持久层
本章内容: 定义Spring对数据库访问的支持 配置数据库资源 使用Spring的JDBC模板 在几乎所有的企业级应用中,都需要构建数据持久层.现在意义上的数据持久层是指把对象或者数据保存到数据库中, ...
- 黑马-Spring与数据库
Spring与数据库 Spring与jdbc 引入dataSource 在客户端 模板编程 类的结构图, 真正干活的是JdbcTemplate(底层实现,操作 excute方法) JdbcTempla ...
- Spring管理事物两种方式
Spring管理事物两种方式 1. 编程式事物管理(在开发中不经常使用) 使用步骤 1. 配置数据库事物管理 DataSourceTransactionManager <!--配置事物管理器-- ...
- Spring(四)Spring与数据库编程
Spring最重要的功能毫无疑问就是操作数据.数据库的百年城是互联网编程的基础,Spring为开发者提供了JDBC模板模式,那就是它自身的JdbcTemplate.Spring还提供了Transact ...
- 【Spring】Spring的数据库开发 - 2、Spring JdbcTemplate的常用方法(execute、update、query)
Spring JdbcTemplate的常用方法 文章目录 Spring JdbcTemplate的常用方法 execute() update() query() 简单记录-Java EE企业级应用开 ...
- Spring 链接数据库
一.前言 Spring 现在是我们在做 JavaWeb 开发中,用的最主流的框架.以后是不是我们暂时不知道,但现在是.废话不多我就介绍 Spring 中.链接数据库的三种方式: git源码地址 需要的 ...
- spring 定时任务执行两次解决办法
在web.xml中同时配置了ContextLoaderListener和DispatcherServlet?假如真是这样的话,需要删掉一个配置,因为你相当于配置了两个spring容器,两个容器分别都执 ...
随机推荐
- Oracle SQL Developer使用
原文 Oracle SQL Developer使用 比较: Plsqldev:第三方的,启动软件时快,执行sql查询时很慢 Sqldeveloper:oracle公司的,启动软件时慢,执行sql查 ...
- windows phone 加速计(5)
原文:windows phone 加速计(5) 在windows phone 中存在着加速计,我们可以利用加速计获得用户手机的状态,根据手机状态调整我们的程序,这样会更人性化:windows phon ...
- Windows Phone开发(44):推送通知第二集——磁贴通知
原文:Windows Phone开发(44):推送通知第二集--磁贴通知 前面我们说了第一个类型--Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧? 其实这东西绝对 ...
- python学习之print输出不换行
print的即时打印会导致换行,要使得print的输出不换行,可以在字符串或者变量后面加个逗号(“,”),如下: s = "A bird in the hand..." for c ...
- gc overhead limit exceeded eclipse错误解决方式
在Eclipse打包的时候报错:gc overhead limit exceeded eclipse 原因是Eclipse默认配置内存太小须要更改安装Eclipse目录下的eclipse.ini文件. ...
- C#中的关键字
abstract event new struct as explicit null switch base extern object this bool false operator throw ...
- SharePoint 2013 创建web应用程序报错"This page can’t be displayed"
错误描写叙述 This page can't be displayed •Make sure the web address http://centeradmin is correct. •Look ...
- ADO.NET连接方式
使用Command.DataReader和DataSet两种方法实现数据绑定 方法1:使用Command和DataReader SqlConnection con = new SqlConnectio ...
- play framework2.5.
play framework2 的学习笔记 https://github.com/playframework/playframework https://github.com/playframewor ...
- mapreduce程序来实现分类
文件的内容例如以下所看到的: 5 45 8 876 6 45 要求最后的输出格式: 1 5 2 6 3 8 4 45 5 45 5 876 首先,这个题目是须要对文 ...