第一种:

1.引入POM坐标,需要同时引入通用mapper和jpa

<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<!-- 建议使用最新版本,最新版本请从项目首页查找 -->
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>

2.将自己的mapper文件继承通用mapper的BaseMapper

@Repository
public interface RatWaiterHitRewardMapper extends BaseMapper<RatWaiterHitReward>{ }

3.编写JAVA BEAN配置类

@Configuration
public class TkMybatisConfig { @Bean(name="mapperHelper")
public MapperScannerConfigurer mapperHelper(){
Properties properties = new Properties();
properties.setProperty("mappers",BaseMapper.class.getName());
properties.setProperty("IDENTITY","MYSQL"); // 数据库方言(主要用于:取回主键的方式)
properties.setProperty("notEmpty","false"); // insert和update中,是否判断字符串类型!='',少数方法会用到
properties.setProperty("style", Style.camelhump.name()); MapperScannerConfigurer scan = new MapperScannerConfigurer();
scan.setSqlSessionFactoryBeanName("sqlSessionFactory"); // 多数据源时,必须配置
scan.setBasePackage("com.eparty.ccp.rate.mapper");//mapper.java文件的路径
scan.setMarkerInterface(BaseMapper.class); // 直接继承了BaseDao接口的才会被扫描,basePackage可以配置的范围更大。
scan.setProperties(properties); return scan;
}
}

第二种(推荐):

1、配置mybatis

application.properties(配置文件)

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=false&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 指向mapper的xml文件位置
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml

2、引入依赖(springboot专用)

 <dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>

3、配置启动类

import tk.mybatis.spring.annotation.MapperScan;     // 注意:这里是导入通用mapper的MapperScan
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan(basePackages = "cn.yujiago.springboot.mapper") // mapper接口的路径
public class BootApplication { public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
} }

4、model类的配置

@Table(name = "test_table")
public class TestTableVO implements Serializable { private static final long serialVersionUID = 1L; @Id
@GeneratedValue(generator = "JDBC")
private Long id; @Transient
private String userId; private String name; private Timestamp createTime; private String createUserId; private Timestamp updateTime; private String updateUserId; private Integer isDelete; // 省略get、set...
}

说明:

  • 表名默认使用类名,驼峰转下划线(只对大写字母进行处理),如UserInfo默认对应的表名为user_info
  • 表名可以使用@Table(name = “tableName”)进行指定,对不符合第一条默认规则的可以通过这种方式指定表名
  • 字段默认和@Column一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式
  • 可以使用@Column(name = “fieldName”)指定不符合第3条规则的字段名
  • 使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用
  • 建议一定是有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键
  • 如果是MySQL的自增字段,加上@GeneratedValue(generator = “JDBC”)即可

5、mapper接口的配置

import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper; @Repository
public interface UserMapper extends Mapper<User> { }
Select
方法:List<T> select(T record);
说明:根据实体中的属性值进行查询,查询条件使用等号 方法:T selectByPrimaryKey(Object key);
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号 方法:List<T> selectAll();
说明:查询全部结果,select(null)方法能达到同样的效果 方法:T selectOne(T record);
说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号 方法:int selectCount(T record);
说明:根据实体中的属性查询总数,查询条件使用等号 Insert
方法:int insert(T record);
说明:保存一个实体,null的属性也会保存,不会使用数据库默认值 方法:int insertSelective(T record);
说明:保存一个实体,null的属性不会保存,会使用数据库默认值 Update
方法:int updateByPrimaryKey(T record);
说明:根据主键更新实体全部字段,null值会被更新 方法:int updateByPrimaryKeySelective(T record);
说明:根据主键更新属性不为null的值 Delete
方法:int delete(T record);
说明:根据实体属性作为条件进行删除,查询条件使用等号 方法:int deleteByPrimaryKey(Object key);
说明:根据主键字段进行删除,方法参数必须包含完整的主键属性 Example方法
方法:List<T> selectByExample(Object example);
说明:根据Example条件进行查询
重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列 方法:int selectCountByExample(Object example);
说明:根据Example条件进行查询总数 方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
说明:根据Example条件更新实体record包含的全部属性,null值会被更新 方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
说明:根据Example条件更新实体record包含的不是null的属性值 方法:int deleteByExample(Object example);
说明:根据Example条件删除数据

下面演示大概的写法: 
新增

TestTableVO vo = new TestTableVO();
// 省略为vo设置属性...
int row = testTableDao.insertSelective(vo);

修改

TestTableVO vo = new TestTableVO();
// 省略为vo设置属性...
int row = testTableDao.updateByPrimaryKeySelective(vo);

单个查询

TestTableVO vo = new TestTableVO();
vo.setId(123L);
TestTableVO result = testTableDao.selectOne(vo);

条件查询

// 创建Example
Example example = new Example(TestTableVO.class);
// 创建Criteria
Example.Criteria criteria = example.createCriteria();
// 添加条件
criteria.andEqualTo("isDelete", 0);
criteria.andLike("name", "%abc123%");
List<TestTableVO> list = testTableDao.selectByExample(example);

SpringBoot之集成通用Mapper的更多相关文章

  1. springboot集成通用mapper详细配置

    通常,我们利用mybatis写持久层方法.要么按照传统定义mapper方法,定义xml文件的方式,全部手写.要么需要通过mybatis-generator逆向工程插件生成大量的xxxExample文件 ...

  2. springboot整合mybatis通用Mapper

    参考: https://blog.csdn.net/x18707731829/article/details/82814095 https://www.jianshu.com/p/6d2103451d ...

  3. 集成通用Mapper

    通用Mapper集成 1.引入jar包 <mapper.version>3.0.1</mapper.version><persistence-api.version> ...

  4. SpringBoot快速整合通用Mapper

    前言 后端业务开发,每个表都要用到单表的增删改查等通用方法,而配置了通用Mapper可以极大的方便使用Mybatis单表的增删改查操作. 通用mapper配置 1.添加maven: <depen ...

  5. springboot 使用mybatis 通用Mapper,pagehelper

    首先需要maven导入需要的包,这里用的是sqlserver,druid,jtds连接数据库 <dependency> <groupId>com.alibaba</gro ...

  6. Mybatis 代码生成器(集成通用Mapper)

    0.确保通用Mapper被正确配置 1.pom.xml追加 <properties> <targetJavaProject>${basedir}/src/main/java&l ...

  7. SpringBoot框架之通用mapper插件(tk.mybatis)

    一.Tkmybatis的好处 Tkmybatis是在mybatis框架的基础上提供了很多工具,让开发更加高效.这个插件里面封装好了我们需要用到的很多sql语句,不过这个插件是通过我们去调用它封装的各种 ...

  8. 关于集成通用mapper的Mybatis代码生成器产生的model类注解

    主要是@Table.@Id.@GeneratedValue.@Column 4个注解 这四个注解都来自javax.persistence包,是Java持久层规范,单纯的Mybatis并不认识这四个注解 ...

  9. Spring Boot MyBatis 通用Mapper插件集成 good

    看本文之前,请确保你已经在SpringBoot中集成MyBatis,并能正常使用.如果没有,那么请先移步 http://blog.csdn.net/catoop/article/details/505 ...

随机推荐

  1. Python 让文件代码支持汉字

    默认使用ASCII编码,改成utf8 #!/usr/bin/env python # -*- coding:utf8 -*- #coding:utf-8

  2. NX二次开发-UFUN文件选择对话框UF_UI_create_filebox

    NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); //文件选择对话框 char sPromptSt ...

  3. 进程、线程、协程、CPU

    进程.线程.CPU 进程是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.或者说进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进 ...

  4. slot 的简单用法

    注:默认在父组件调用子组件时<SlotChild></SlotChild>中文字不会显示.但是在子组件加入<slot></slot>后,<Slot ...

  5. bash数组总结

    bash数组操作 bash支持两种数组,一种是索引数组,一种是关联数组 索引数组 数组的值类型是任意的,索引也未必一定要连续,当做列表理解更好 下面总结下索引数组,即列表: 1. 声明 declare ...

  6. How to Add Swap on CentOS

    About Linux Swapping Linux RAM is composed of chunks of memory called pages. To free up pages of RAM ...

  7. idea中选中了一个变量名,会高亮显示位于别的地方的这个变量名,那么怎么修改其他地方的高亮颜色

  8. boost库之pool编译错误

    1,编译错误截图 2,解决方法 这是由于没有链接对应的库导致的错误,在编译命令中加上 -lboost_system选项即可.

  9. PyQt5 安装及简单实例 -- 标签设置

    - 1 -    PyQt5安装,鉴于pip工具,安装其实很简单, 如下:(最好将pip索引配置成国内镜像,速度比默认的快得多) pip3 isntall PyQt5 - 2 -    利用Pycha ...

  10. iOS进阶五-RunLoop

    简介 RunLoop 运行循环.跑圈 RunLoop的作用主要体现在三方面: 1.保持程序持续运行 2.处理App中的各种事件(比如触摸事件.定时器事件.Selector事件) 3.节省CPU资源,提 ...