>> 在spring中使用myBatis
mybatis没有特殊的事务策略,除了JDBC Connection外,也没有特殊的事务资源。它和spring JDBC事务管理方式完全一致,采用和spring JDBC相同的DataSourceTransactionManager事务管理器。
第一种方式:
先看一下myBatisConfig.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>
<settings>
<setting name="lazyLoadingEnabled" value="false" />
</settings>
<typeAliases>
<typeAlias alias="Forum" type="com.baobaotao.domain.Forum" />
<typeAlias alias="Topic" type="com.baobaotao.domain.Topic" />
<typeAlias alias="Post" type="com.baobaotao.domain.Post" />
</typeAliases>
<mappers>
<mapper resource="com/baobaotao/domain/mybatis/Forum.xml" />
<mapper resource="com/baobaotao/domain/mybatis/Topic.xml" />
<mapper resource="com/baobaotao/domain/mybatis/Post.xml" />
</mappers>
</configuration>
再看下单个DAO的sql语句映射文件:
<?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="com.baobaotao.dao.mybatis.ForumMybatisDao">
<select id="getForum" resultType="Forum" parameterType="int" >
SELECT
forum_id forumId,
forum_name forumName,
forum_desc forumDesc
FROM t_forum
WHERE forum_id = #{forumId}
</select>
<select id="getForumNum" resultType="int">
SELECT COUNT(forum_id) FROM t_forum f
</select>
<select id="findForumByName" resultType="Forum" parameterType="string">
SELECT
forum_id forumId,
forum_name forumName,
forum_desc forumDesc
FROM t_forum f
WHERE f.forum_name LIKE #{forumName}
</select>
<insert id="addForum" parameterType="Forum">
INSERT INTO t_forum(forum_id,forum_name,forum_desc)
VALUES(#{forumId},#{forumName}, #{forumDesc})
</insert>
<update id="updateForum" parameterType="Forum">
UPDATE t_forum f
SET forum_name=#{forumName},forum_desc=#{forumDesc}
WHERE f.forum_id = #{forumId}
</update>
</mapper>
接下来引入mabatis-spring的依赖jar包,然后配置spring的配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.baobaotao.dao.mybatis" />
<context:component-scan base-package="com.baobaotao.service.mybatis" />
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/sampledb"
p:username="root"
p:password="123456" />
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:myBatisConfig.xml"
p:mapperLocations="classpath:com/baobaotao/domain/mybatis/*.xml"/>
<bean class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:sqlSessionFactory-ref="sqlSessionFactory"
p:basePackage="com.baobaotao.dao.mybatis"/>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
使用SqlSessionTemplate模板类编写mybatis的DAO:
第一步是在spring中配置好SqlSessionTemplate 这个Bean:
<bean class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
接下来编写DAO:
package com.baobaotao.dao.mybatis;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.baobaotao.domain.Forum;
@Repository
public class ForumMybatisTemplateDao{
@Autowired
private SqlSessionTemplate sessionTemplate;
public Forum getForum(int forumId){
return (Forum)sessionTemplate.selectOne(
"com.baobaotao.dao.mybatis.ForumMybatisDao.getForum",
forumId);
}
}
第二种方式:
实际上,在上面使用SqlSessionTemplate直接去调用SQL映射项的时候必须要使用com.baobaotao.dao.mybatis.ForumMybatisDao.getFrum这个字符串去定位statement。这个字符串是很容易出错的,而且编译器不会报错。
下面使用映射接口,非常强大,非常方便:
mybatis特别提供了一种可将SQL映射文件中的映射项通过名称匹配接口进行调用的方法:接口的名称和映射命名空间相同,接口方法和映射的ID相同。
下面我们为Forum.xml定义一个调用接口:
package com.baobaotao.dao.mybatis;
import java.util.List;
import com.baobaotao.domain.Forum;
public interface ForumMybatisDao{
void addForum(Forum forum);
void updateForum(Forum forum) ;
Forum getForum(int forumId) ;
long getForumNum() ;
List<Forum> findForumByName(String forumName);
}
类名为:com.baobaotao.dao.mybatis.ForumMybatisDao,Forum.xml中的每个映射项对应一个接口方法,接口方法的签名和映射项的声明匹配。
定义好了这个DAO接口后,下面通过SqlSessionTemplate获取接口实例:
public Forum getForum2(int forumId){
ForumMybatisDao forumMybatisDao =
sessionTemplate.getMapper(ForumMybatisDao.class);
return forumMybatisDao.getForum(forumId);
}
这种方法也有缺点,就是必须要比之前直接用字符串多写几个接口,每个Sql Mapper的xml文件都要写个接口。比较安全,不过通过getMapper方法(Class<T> type)这个还不是最优的方法。对于spring应用来讲,我们更希望在Service类中通过@Autowired的方法直接注入接口实例,而非每次都通过getMapper(Class<T> type)来获取DAO的实例。
第三种方式:
mybatis-spring提供了一个神奇的转换器MapperScannerConfigurer,它可以将映射接口直接装换为spring容器中的bean,这样你就可以在service类中直接注入映射接口的bean了。
假设我们已经为三个SQL映射文件分别定义了对应的接口类,使用如下配置可以将接口装换为Bean:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:sqlSessionFactoryBeanName="sqlSessionFactory"
p:basePackage="com.baobaotao.dao.mybatis"/>
然后我们就可以在service里面直接注入DAO了,尼玛整个DAO层就几个SQL Mapper配置文件夹外加几个接口类了。
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baobaotao.dao.mybatis.ForumMybatisDao;
import com.baobaotao.dao.mybatis.PostMybatisDao;
import com.baobaotao.dao.mybatis.TopicMybatisDao;
import com.baobaotao.domain.Forum;
import com.baobaotao.domain.Post;
import com.baobaotao.domain.Topic;
@Transactional
@Service
public class BbtForumSerive{
@Autowired
private ForumMybatisDao forumDao;
@Autowired
private TopicMybatisDao topicDao;
@Autowired
private PostMybatisDao postDao;
public void addForum(Forum forum) {
forumDao.addForum(forum);
}
public void addTopic(Topic topic) {
topicDao.addTopic(topic);
}
......
}
注意上面的@Transactional注解,可以在spring配置文件中声明式事务,直接扫描注解,非常强大,这个之前的spring事务章节已经讲过了,不多说鸟 –
总结:以上三个使用方式,一个比一个方便安全,所以第三种spring方式的最优,优先使用第三种方式。
>> 谈谈DAO层的设计:
对于使用Hibernate的应用来讲,可以定义一个BaseDao的泛型类,在其中实现泛型参数化查找更新操作等,可以极大的提高效率:
package com.baobaotao.dao;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class BaseDao<T> {
@Autowired
private HibernateTemplate hibernateTemplate;
private Class entityClass;
public BaseDao(){
Type genType = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
entityClass = (Class) params[0];
}
public T get(Serializable id){
return (T)hibernateTemplate.get(entityClass, id);
}
public void save(T entity){
hibernateTemplate.save(entity);
}
public void update(T entity){
hibernateTemplate.update(entity);
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
}
最后,关于参数传递的方法:最佳实践是使用Map parms的形式:
List<Order> findOrder(String sql, Map params)
>> 分页查询接口设计
分页技术大致可以分成三种:
* 客户端分页:直接将全部多页结果一次性返回客户端,客户端通过展览细件前台js实现分页
* 数据库分页:每次查询数据只有一页数据
* 服务器端分页:从数据库返回全部数据,在服务器端缓冲起来,但只返回一页给客户端
第三种服务器分页既能够提高系统交互性,又能有效减小数据库访问次数,因此最优,但是又要涉及到数据缓冲、同步等问题,因此复杂性比较高。但俗话说的好,不入虎穴不得虎子(⊙o⊙),最终的王道还是钻研技术,才能做出高可靠高可用高性能的应用了来。
本人博客已搬家,新地址为:http://yidao620c.github.io/
相关推荐
Spring整合其他ORM框架:Spring整合其他ORM框架整合的时候所需要的jar包spring-orm-3.2.0.RELEASE.jar
赠送jar包:spring-orm-5.0.8.RELEASE.jar; 赠送原API文档:spring-orm-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-orm-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-orm-5.0.8.RELEASE.pom;...
赠送jar包:spring-orm-4.2.2.RELEASE.jar; 赠送原API文档:spring-orm-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-orm-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-orm-4.2.2.RELEASE.pom;...
赠送jar包:spring-orm-5.0.8.RELEASE.jar; 赠送原API文档:spring-orm-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-orm-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-orm-5.0.8.RELEASE.pom;...
赠送jar包:spring-orm-4.2.2.RELEASE.jar; 赠送原API文档:spring-orm-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-orm-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-orm-4.2.2.RELEASE.pom;...
本文主要介绍Python使用ORM框架SQLAlchemy操作Oracle数据库。 1. 安装Oracle Instant Client 2. 安装依赖库 使用以下命令来安装SQLAlchemy和cx_Oracle库: pip install SQLAlchemy pip install cx_Oracle 3.创建...
Ebean:开源ORM框架 Ebean是一个Java实现的开源ORM框架,具有数据访问快速和易于学习、使用等特点。
能实现基本的数据库操作能实现基本的数据库操作
//1、加载驱动类//2、建立连接//3、创建语句集//4、执行语句集Member instance = new Member();//5、获取结果集//6、关
IOC容器:Spring Web框架:SpringMVC ORM框架:Mybatis数据源:C3P0日志:log4j前端框架:Bootstrap运行环境jdk8 + tomcat8 + mysql + Eclipse + maven项目技术:spring + spring mvc + mybatis + bootstrap + ...
noear::微型ORM框架(支持:java sql,xml sql,annotation sql;事务;缓存;监控;等...)可以嵌入到JVM脚本引擎(js, groovy, lua, python, ruby)及GraalVM支持的部分语言。0.2Mb(且是功能完整,方案丰富;可极...
手写ORM框架笔记
简单高效的ORM框架
使用运算符重载,实现 ORM 框架里的 INSERT、UPDATE、DELETE 和 SELECT 语句动态生成。SELECT 语句支持 INNER JOIN、LEFT OUTER JOIN 和RIGHT OUTER JOIN 多表联合查询,但不支持同一表的联合查询。 代码中只实现了 ...
SBORM只是针对spring jdbc的一些不方便的地方,做了一些封装,更加简化日常的开发工作,基于spring jdbc的RowMapper自动实现对象映射,也勉强算的上叫ORM,只是大部分功能已经由spring jdbc实现了。 平时不太...
数据访问:介绍了Spring框架对数据库访问的支持,包括JDBC、ORM框架、事务管理等。 测试和调试:介绍了如何使用Spring进行单元测试和集成测试,以及调试技巧和工具的使用。 这本资源适合已经具备一定Spring基础知识...
Sqlite ORM 是一个简单的C#类,对Sqlite的操作进行了封装,主要功能包括:表定义、生成,访问,更新等,其中,支持,多表的连接操作,语法类似Linq语法,使用非常方便,附加了使用说明文档。 例如,添加记录操作为...
spring-orm.jar
薪资管理系统分为三个角色,分别为普通用户、财务人员及领导。... 核心框架:Spring Boot 视图框架:Spring MVC ORM框架:MyBatis 数据库连接池:Druid 1.1 安全框架:Apache Shiro 1.4 前端框架:jQury,bootStrap
项目标题:深入剖析Scree:C#基架下的ORM框架源码解析 项目概述: 本项目采用C#为主要开发语言,构建了一个纯净的ORM框架,不含任何具体业务逻辑。框架以对象版本控制为核心,集成了事务处理、缓存机制、同步与锁...