项目中持久层封装了两套,一个hibernate,一个是mybatis。hibernate中封装了一些通用的方法,但是mybatis中没有,基于这个需求开始使用mybatis的通用mapper。

       通用mapper有什么好处呢?以往我们使用mapper文件都是自己写sql语句,针对的是单个实体,也就是每个实体都有其对应的mapper文件。使用通用mapper给我们带来了极大的方便,它不需要mapper.xml文件,只需我们调用相应的接口,引入jar包再进行简单的配置就好了。下面是使用中的一些总结。
首先我们要创建自己的接口来继承Mapper<T>,其中的T必须实现,也就是必须指定类型:
import com.github.abel533.mapper.Mapper;
@Repository("courseDao")
public interface CourseDao extends Mapper<Course>{}
这样我们就拥有了Mapper中的通用方法
如果是maven项目,在pom文件中添加如下依赖即可:
 
<!-- 通用Mapper -->
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>2.3.4</version>
</dependency>
接下来就是修改mybatis配置文件,我们的配置文件都是通过spring来管理的,在网上查到一些资料,如果使用spring管理,直接在配置文件中按照如下配置即可:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/*.xml</value>
</array>
</property>
<property name="typeAliasesPackage" value="com.dmsd.itoo.video.entity"/>
<property name="plugins">
<array>
<-- 主要看这里 -->
<bean class="com.isea533.mybatis.mapperhelper.MapperInterceptor"/>
</array>
</property>
</bean>
注意typeAliasesPackage的value对应的是你自己的实体的类名。
按照上述配置mybatis配置文件总是报如下错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (com.github.abel533.mapper.MapperProvider.dynamicSQL). Cause: java.lang.InstantiationException: com.github.abel533.mapper.MapperProvider
查询无果,换另一种方式,将mybatis的配置文件单独抽出,在spring中进行引用,如下
spring-mybatis配置文件:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<property name="typeAliasesPackage" value="com.dmsd.itoo.video.entity" />
<!-- mybatis全局配置文件 -->
<property name="configLocation" value="classpath:spring/mybatis-config.xml" />
</bean>
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>
<plugins>
<!-- 通用Maper -->
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!-- 主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
<property name="IDENTITY" value="MYSQL" />
<!-- 通用Mapper接口,多个通用接口用逗号隔开 -->
<property name="mappers" value="com.github.abel533.mapper.Mapper" />
</plugin>
</plugins>
 
</configuration>
运行,ok
 

注意

泛型(实体类)<T>的类型必须符合要求
实体类按照如下规则和数据库表进行转换,注解全部是JPA中的注解:
  1. 表名默认使用类名,驼峰转下划线,如UserInfo默认对应的表名为user_info.
  2. 表名可以使用@Table(name = "tableName")进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.
  3. 字段默认和@Column一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式.
  4. 可以使用@Column(name = "fieldName")指定不符合第3条规则的字段名
  5. 使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用.
  6. 建议一定是有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键.
  7. 默认情况下,实体类中如果不存在包含@Id注解的字段,所有的字段都会作为主键字段进行使用(这种效率极低).
  8. 实体类可以继承使用,可以参考测试代码中的com.github.abel533.model.UserLogin2类.
  9. 由于基本类型,如int作为实体类字段时会有默认值0,而且无法消除,所以实体类中建议不要使用基本类型.
除了上面提到的这些,Mapper还提供了序列(支持Oracle)、UUID(任意数据库,字段长度32)、主键自增(类似Mysql,Hsqldb)三种方式,其中序列和UUID可以配置多个,主键自增只能配置一个。
 
 
补充:
后来找到了通过spring管理mybatis的方法,不需要单独抽出mybatis的配置文件,修改spring-mybatis.xml如下:
  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  2. <property name="dataSource" ref="dataSource" />
  3. <property name="mapperLocations" value="classpath:mapper/*.xml" />
  4. <property name="typeAliasesPackage" value="com.dmsd.itoo.video.entity" />
  5. <property name="plugins">
  6. <array>
  7. <bean class="com.github.abel533.mapperhelper.MapperInterceptor">
  8. <property name="properties">
  9. <value>
  10. mappers=com.github.abel533.mapper.Mapper
  11. IDENTITY=MYSQL
  12. notEmpty=true
  13. </value>
  14. </property>
  15. </bean>
  16. </array>
  17. </property>
  18. <!-- mybatis全局配置文件 -->
  19. <!-- <property name="configLocation" value="classpath:spring/mybatis-config.xml"></property> -->
  20. </bean>

http://blog.csdn.net/xfz0330/article/details/52275192

mybatis通用mapper的使用的更多相关文章

  1. 值得收藏的Mybatis通用Mapper使用大全。

    引言 由于小编的记性不太好,每次在写代码的时候总是把通用mapper的方法记错,所以今天把通用mapper的常用方法做一下总结,方便以后直接查看.好了,不废话啦. 引包 <!-- 通用Mappe ...

  2. SpringBoot 3.SpringBoot 整合 MyBatis 逆向工程以及 MyBatis 通用 Mapper

    一.添加所需依赖,当前完整的pom文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...

  3. Spring boot集成 MyBatis 通用Mapper

    配置 POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  4. (一 、上)搭建简单的SpringBoot + java + maven + mysql + Mybatis+通用Mapper 《附项目源码》

    最近公司一直使用 springBoot 作为后端项目框架, 也负责搭建了几个新项目的后端框架.在使用了一段时间springBoot 后,感觉写代码 比spring 更加简洁了(是非常简洁),整合工具也 ...

  5. spring boot集成MyBatis 通用Mapper 使用总结

    spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...

  6. springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui

    前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...

  7. springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源

    本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...

  8. springboot学习笔记:11.springboot+shiro+mysql+mybatis(通用mapper)+freemarker+ztree+layui实现通用的java后台管理系统(权限管理+用户管理+菜单管理)

    一.前言 经过前10篇文章,我们已经可以快速搭建一个springboot的web项目: 今天,我们在上一节基础上继续集成shiro框架,实现一个可以通用的后台管理系统:包括用户管理,角色管理,菜单管理 ...

  9. Mybatis通用Mapper介绍和使用

    Mybatis通用Mapper介绍与使用 前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL. ...

  10. 【SSM 8】spring集成Mybatis通用Mapper

    上篇博客中介绍了关于Mybatis底层封装的思路问题,那么这篇博客,就介绍一下怎么引入通用的mapper插件. 备注:本项目通过maven管理 关键版本说明: spring:4.1.3.RELEASE ...

随机推荐

  1. LeetCode之“动态规划”:Decode Ways

    题目链接 题目要求: A message containing letters from A-Z is being encoded to numbers using the following map ...

  2. 如果去掉UITableView上的section的headerView和footerView的悬浮效果

    项目需要cell的间距,又不需要悬浮效果,百度之后找到这个方法,记录一下,备忘. 用UIScrollView的代理方法实现 - (void)scrollViewDidScroll:(UIScrollV ...

  3. Mina源码阅读笔记(一)-整体解读

    今天的这一节,将从整体上对mina的源代码进行把握,网上已经有好多关于mina源码的阅读笔记,但好多都是列举了一下每个接口或者类的方法.我倒是想从mina源码的结构和功能上对这个框架进行剖析.源码的阅 ...

  4. LeetCode(33)-Pascal's Triangle II

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  5. Spring Cloud 入门教程 - 搭建配置中心服务

    简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...

  6. ExtJS中xtype 概览

    基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...

  7. Spring3 MVC

    一.前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring3 MVC结构简单,应了那句话简单就是美,而 ...

  8. R实战 第七篇:网格(grid)

    grid包是R底层的图形系统,可以绘制几乎所有的图形.除了绘制图形之外,grid包还能对图形进行布局.在绘图时,有时候会遇到这样一种情景,客户想把多个代表不同KPI的图形分布到同一个画布(Page)上 ...

  9. 全屏slider--swiper

    这两年,这种滑动器技术在互联网产品介绍页设计上非常常用,最近某个项目需要这种设计,在网上找了一下,有个老外产的这种设计组件swiper已经非常成熟,原来这个东西设计初衷是pad之类的移动触摸交互设备, ...

  10. Nginx日志配置及配置调试

    防火墙内的内网服务器,因为网关传过来的remot_addr都一样,不得不对Nginx的日志格式做了配置 配置语法如下: log_format  myformat  '$http_x_forwarded ...