SpringBoot之集成通用Mapper
第一种:
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的更多相关文章
- springboot集成通用mapper详细配置
通常,我们利用mybatis写持久层方法.要么按照传统定义mapper方法,定义xml文件的方式,全部手写.要么需要通过mybatis-generator逆向工程插件生成大量的xxxExample文件 ...
- springboot整合mybatis通用Mapper
参考: https://blog.csdn.net/x18707731829/article/details/82814095 https://www.jianshu.com/p/6d2103451d ...
- 集成通用Mapper
通用Mapper集成 1.引入jar包 <mapper.version>3.0.1</mapper.version><persistence-api.version> ...
- SpringBoot快速整合通用Mapper
前言 后端业务开发,每个表都要用到单表的增删改查等通用方法,而配置了通用Mapper可以极大的方便使用Mybatis单表的增删改查操作. 通用mapper配置 1.添加maven: <depen ...
- springboot 使用mybatis 通用Mapper,pagehelper
首先需要maven导入需要的包,这里用的是sqlserver,druid,jtds连接数据库 <dependency> <groupId>com.alibaba</gro ...
- Mybatis 代码生成器(集成通用Mapper)
0.确保通用Mapper被正确配置 1.pom.xml追加 <properties> <targetJavaProject>${basedir}/src/main/java&l ...
- SpringBoot框架之通用mapper插件(tk.mybatis)
一.Tkmybatis的好处 Tkmybatis是在mybatis框架的基础上提供了很多工具,让开发更加高效.这个插件里面封装好了我们需要用到的很多sql语句,不过这个插件是通过我们去调用它封装的各种 ...
- 关于集成通用mapper的Mybatis代码生成器产生的model类注解
主要是@Table.@Id.@GeneratedValue.@Column 4个注解 这四个注解都来自javax.persistence包,是Java持久层规范,单纯的Mybatis并不认识这四个注解 ...
- Spring Boot MyBatis 通用Mapper插件集成 good
看本文之前,请确保你已经在SpringBoot中集成MyBatis,并能正常使用.如果没有,那么请先移步 http://blog.csdn.net/catoop/article/details/505 ...
随机推荐
- 「题解」:07.16NOIP模拟T1:礼物
问题 A: 礼物 时间限制: 1 Sec 内存限制: 256 MB 题面 题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生 日礼物. 商店里一共有种礼物.夏川每得到一种礼 ...
- NX二次开发-UFUN点收集器UF_UI_select_point_collection
#include <uf.h> #include <uf_ui.h> UF_initialize(); //点收集器 char sMessage[] = "点收集器& ...
- linux下mysql权限配置
先登入mysql mysql -u root -p 然后回车键入密码! 1.2 赋予主机B操作数据库的权限 mysql> grant usage on *.* to username@192.1 ...
- Android 防止多次点击提交数据
package com.test1.test; import android.app.Activity; import android.os.Bundle; import android.view.V ...
- 暴力”注入Explorer
暴力"注入Explorer pjf(jfpan20000@sina.com) 向一个运行中的进程注入自己的代码,最自然莫过于使用Cr ...
- jeecg下实现自动默认模糊查询
也许jeecg的作者深受SAP毒害吧,没考虑到广大使用JEECG的人群为SAP用户,及所开发的项目均为中小项目,无惧大数据模糊查询带来的功能影响. 经网友“&&康&&& ...
- 案例 查询QC(query_cache)
案例: 数据库版本 5.7.23 16c 64g 按月分区表,做压力测试 开启QC之前: select * from a where aa=xxx; QPS:每秒处理查询的数量 3000+ 开启之后 ...
- 17-MySQL-Ubuntu-数据表的查询-分页(六)
分页(limit) 注: (1)limit位于SQL语句的最后面; (2)limit 2; 2表示查询前两条数据; (3)limit 0,2; 0表示查询第1页的起始数据的下标,2表示每页有两条数据 ...
- **JLink Warning: Mis-aligned memory write: Address: 0x20000000, NumBytes: 2, Alignment: 2 (Halfword-aligned)
网上也有同学遇到这个问题,http://www.openedv.com/thread-113049-1-3.html 根据他的经验我也重新安装了Jlink驱动: 顺便注意Dialog DLL:TARM ...
- EXE 和 SYS 信息交互
操了,分发函数少发一个,让我白调了两个多小时.