mybatis通用mapper的使用
项目中持久层封装了两套,一个hibernate,一个是mybatis。hibernate中封装了一些通用的方法,但是mybatis中没有,基于这个需求开始使用mybatis的通用mapper。
import com.github.abel533.mapper.Mapper;@Repository("courseDao")public interface CourseDao extends Mapper<Course>{}<!-- 通用Mapper --><dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>2.3.4</version></dependency><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>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<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><?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>注意
- 表名默认使用类名,驼峰转下划线,如UserInfo默认对应的表名为user_info.
- 表名可以使用@Table(name = "tableName")进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.
- 字段默认和@Column一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式.
- 可以使用@Column(name = "fieldName")指定不符合第3条规则的字段名
- 使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用.
- 建议一定是有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键.
- 默认情况下,实体类中如果不存在包含@Id注解的字段,所有的字段都会作为主键字段进行使用(这种效率极低).
- 实体类可以继承使用,可以参考测试代码中的com.github.abel533.model.UserLogin2类.
- 由于基本类型,如int作为实体类字段时会有默认值0,而且无法消除,所以实体类中建议不要使用基本类型.
- <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" />
- <property name="plugins">
- <array>
- <bean class="com.github.abel533.mapperhelper.MapperInterceptor">
- <property name="properties">
- <value>
- mappers=com.github.abel533.mapper.Mapper
- IDENTITY=MYSQL
- notEmpty=true
- </value>
- </property>
- </bean>
- </array>
- </property>
- <!-- mybatis全局配置文件 -->
- <!-- <property name="configLocation" value="classpath:spring/mybatis-config.xml"></property> -->
- </bean>
http://blog.csdn.net/xfz0330/article/details/52275192
mybatis通用mapper的使用的更多相关文章
- 值得收藏的Mybatis通用Mapper使用大全。
引言 由于小编的记性不太好,每次在写代码的时候总是把通用mapper的方法记错,所以今天把通用mapper的常用方法做一下总结,方便以后直接查看.好了,不废话啦. 引包 <!-- 通用Mappe ...
- SpringBoot 3.SpringBoot 整合 MyBatis 逆向工程以及 MyBatis 通用 Mapper
一.添加所需依赖,当前完整的pom文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...
- Spring boot集成 MyBatis 通用Mapper
配置 POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- (一 、上)搭建简单的SpringBoot + java + maven + mysql + Mybatis+通用Mapper 《附项目源码》
最近公司一直使用 springBoot 作为后端项目框架, 也负责搭建了几个新项目的后端框架.在使用了一段时间springBoot 后,感觉写代码 比spring 更加简洁了(是非常简洁),整合工具也 ...
- spring boot集成MyBatis 通用Mapper 使用总结
spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...
- springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui
前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...
- springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源
本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...
- springboot学习笔记:11.springboot+shiro+mysql+mybatis(通用mapper)+freemarker+ztree+layui实现通用的java后台管理系统(权限管理+用户管理+菜单管理)
一.前言 经过前10篇文章,我们已经可以快速搭建一个springboot的web项目: 今天,我们在上一节基础上继续集成shiro框架,实现一个可以通用的后台管理系统:包括用户管理,角色管理,菜单管理 ...
- Mybatis通用Mapper介绍和使用
Mybatis通用Mapper介绍与使用 前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL. ...
- 【SSM 8】spring集成Mybatis通用Mapper
上篇博客中介绍了关于Mybatis底层封装的思路问题,那么这篇博客,就介绍一下怎么引入通用的mapper插件. 备注:本项目通过maven管理 关键版本说明: spring:4.1.3.RELEASE ...
随机推荐
- LeetCode之“动态规划”:Decode Ways
题目链接 题目要求: A message containing letters from A-Z is being encoded to numbers using the following map ...
- 如果去掉UITableView上的section的headerView和footerView的悬浮效果
项目需要cell的间距,又不需要悬浮效果,百度之后找到这个方法,记录一下,备忘. 用UIScrollView的代理方法实现 - (void)scrollViewDidScroll:(UIScrollV ...
- Mina源码阅读笔记(一)-整体解读
今天的这一节,将从整体上对mina的源代码进行把握,网上已经有好多关于mina源码的阅读笔记,但好多都是列举了一下每个接口或者类的方法.我倒是想从mina源码的结构和功能上对这个框架进行剖析.源码的阅 ...
- 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 ...
- Spring Cloud 入门教程 - 搭建配置中心服务
简介 Spring Cloud 提供了一个部署微服务的平台,包括了微服务中常见的组件:配置中心服务, API网关,断路器,服务注册与发现,分布式追溯,OAuth2,消费者驱动合约等.我们不必先知道每个 ...
- ExtJS中xtype 概览
基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...
- Spring3 MVC
一.前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring3 MVC结构简单,应了那句话简单就是美,而 ...
- R实战 第七篇:网格(grid)
grid包是R底层的图形系统,可以绘制几乎所有的图形.除了绘制图形之外,grid包还能对图形进行布局.在绘图时,有时候会遇到这样一种情景,客户想把多个代表不同KPI的图形分布到同一个画布(Page)上 ...
- 全屏slider--swiper
这两年,这种滑动器技术在互联网产品介绍页设计上非常常用,最近某个项目需要这种设计,在网上找了一下,有个老外产的这种设计组件swiper已经非常成熟,原来这个东西设计初衷是pad之类的移动触摸交互设备, ...
- Nginx日志配置及配置调试
防火墙内的内网服务器,因为网关传过来的remot_addr都一样,不得不对Nginx的日志格式做了配置 配置语法如下: log_format myformat '$http_x_forwarded ...