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 ...
随机推荐
- NX二次开发-UDO用户自定义对象(UFUN)【持续完善】
每当提起UDO总是会让我想起大专毕业那会失业找工作,后来有个宝贵机会去了软件公司上班,拿到了我人生中的第一个NX二次开发项目,一个关于测量汽车前后左右摄像头的项目.当时那个项目就用到了UDO,对于只看 ...
- NSDateFormatter 今年日期格式化成字符串是明年日期问题?
在项目里我要是把NSDate格式化成字符串 我的format是@"YYYY年MM月dd日 HH:mm" 传入日期2013-12-30 15:00:00后,返回给我的字符串是 201 ...
- Spring-Security (学习记录七)--实现FilterInvocationSecurityMetadataSource的类将无法切入声明式事物
目录 1 查看继承关系 2 说明 3 查看源码: 实现了FilterInvocationSecurityMetadataSource 的类将无法切入声明式事物. 原因: 1 查看继承关系 先查看Fil ...
- 创建 Angular 8.0 项目
创建 Angular 8.0 项目,首先确保已经安装了 nodejs,如果没有安装,请看这篇文章安装:node.js 安装 1.新建一个空文件夹 angularproject,作为工作区 2.安装 A ...
- (转)sql的group by应用
转载于:http://www.studyofnet.com/news/247.html 本文导读:在实际SQL应用中,经常需要进行分组聚合,即将查询对象按一定条件分组,然后对每一个组进行聚合分析.创建 ...
- Portainer Exec Container 失败解决方案
近日,将portainer服务挂了个域名,然后用Nginx代理的时候发现不能attach容器了,经过搜索在issue 找到解决方案 1.修改Nginx config server { listen 8 ...
- 27. USART, Universal synchronous asynchronous receiver transmitter
27.1 USART introduction 通用同步异步接收发射机(USART)对需要NRZ异步串行数据格式行业标准的外部设备,提供了一个灵活的全双工数据交换的方法.USART使用分数波特率生成器 ...
- 2.2_springboot2.x消息RabbitMQ整合&amqpAdmin管理组件的使用
5.1.1.基本测试 1.引 spring-boot-starter-amqp** <dependencies> <dependency> <groupId>org ...
- Python匹马行天下之python之父
龟叔和他的python 经过了漫长的旅程,终于要看到主角Python了.Python是现在非常非常流行的编程语言,在我们能看到的大部分编程语言排行榜中,Python都能在前三甲中拥有一席之地 ,并且发 ...
- jenkins深入浅出
安装: 1. 从官网上下载新版本的Jenkins,https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.89.4/jenkins.war ...