第一种:

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. Delphi 与SQL编程

    Delphi 与SQL编程 SQL语言作为关系数据库管理系统中的一种通用的结构查询语言, 已经被众多的数据库管理系统所采用,如Oracle.Sybase.Informix等数据库管理系统,它们都支持S ...

  2. NX二次开发-创建功能区工具栏UF_UI_create_ribbon

    NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...

  3. sqlserver中常用的windows命令行的操作

    1.删除指定目录下指定时间之前的文件: ), ), @sqltxtdel varchar(max) --指定的删除时间 set @deldate= '-8' --指定的删除路径 set @bakpat ...

  4. beaglebone black 与电脑互传文件(夹)

    1. PSCP方法 PSCP,是putty的一个组件. 1.1 下载PSCP 先给连接http://www.chiark.greenend.org.uk/~sgtatham/putty/downloa ...

  5. CodeForces 1152E Neko and Flashback

    题目链接:http://codeforces.com/problemset/problem/1152/E 题目大意 有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下: b:b ...

  6. jquery网页定位导航特效

    <!DOCTYPE html> <html lang="en"> <head> <script src="http://code ...

  7. HDU—4046 Panda (线段树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4046   题意:给出一个字符串,统计这个字符串任意区间中"wbw"出现的次数. 规定两 ...

  8. angularJS ng-model与wdatapicker问题记录

    代码: <input type="text" placeholder="开始日期" ng-model="data_start" onF ...

  9. 简单的GridView分业,后台不需要写

    1前台代码: <asp:GridView ID="GridView1" runat="server" AllowPaging="True&quo ...

  10. mysql-connetor-c 自动创建数据库、数据库表的命令

    1.首先连接MySQL默认的数据库mysql // 参数说明: // strIP: MySQL数据库的IP地址 // nPort: MySQL数据库的端口号 // strDBName: 要连接的数据库 ...