springboot整合tkmapper

1.导入pom依赖

1.1 导入springboot的parent依赖

<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.9.RELEASE</version>
</parent>
  • 1
  • 2
  • 3
  • 4
  • 5

1.2 导入具体依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency> <!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- tkmapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- pagehelper分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency> <!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
</dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

2. 添加tkmapper数据库连接配置

  • 创建application.yml配置类
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 连接池指定 springboot2.02版本默认使用HikariCP 此处要替换成Druid
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///pethome?serverTimezone=Asia/Shanghai&characterEncoding=utf-8
username: root
password: qwe123
druid:
initial-size: 5 # 初始化时建立物理连接的个数
min-idle: 5 # 最小连接池连接数量,最小空闲数量
max-active: 20 # 最大连接池连接数量,最大活跃连接数
max-wait: 60000 # 配置获取连接等待超时的时间
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
stat-view-servlet:
allow: 0.0.0.0 # 允许哪些IP访问druid监控界面,多个IP以逗号分隔
login-username: admin # 设置登录帐号
login-password: 123456 # 设置登录密码
reset-enable: false # 是否允许重置数据
# url-pattern: /database/* # 默认访问根路径是:/druid/;也可以自定义设置
# mybatis配置
mybatis:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 设置控制台输入执行的sql语句
type-aliases-package: org.example.model # tkmapper配置
mapper:
not-empty: false
identity: mysql #指定tkmapper加载的数据库
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

3. 在启动类上添加扫描注解

  • MainApp.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication
@MapperScan(basePackages = "org.example.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4.tkmapper的使用

4.1 创建mapper.java


public interface ProductMapper extends Mapper<TProduct> {
}
  • 1
  • 2
  • 3

4.2 创建表对应的实体类TProduct

@Data
public class TProduct {
@Id //指定主键的注解
private Long id;
private String name;
private String resources;
private Double saleprice;
private java.util.Date offsaletime;
private java.util.Date onsaletime;
private Long state;
private String costprice;
private java.util.Date createtime;
private Long salecount;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.3 添加测试类,进行单表的CRUD操作

@SpringBootTest
@RunWith(SpringRunner.class)
public class AppTest { @Autowired
private ProductMapper productMapper; @Test//查询所有
public void findAll(){
List<TProduct> tProducts = productMapper.selectAll();
for (TProduct tProduct : tProducts) {
System.out.println(tProduct);
}
} @Test
public void insert(){
TProduct product = new TProduct();
product.setName("我是测试的");
product.setCreatetime(new Date());
product.setState(1L); productMapper.insert(product);
} @Test
public void updateById(){
TProduct product = new TProduct();
product.setId(174L);
product.setName("我是测试");
//如果修改时,只想改变更新的name值,其他值不改
//下面这个方法,是无论修改的值是否为空,将全部修改
// productMapper.updateByPrimaryKey(product);
//下面的方法,只改非空的字段.
//注意:tkmapper中,凡是方法名以Selective结尾的,就是在拼接动态sql
//即,不更新非空的字段
product.setCreatetime(new Date());
productMapper.updateByPrimaryKeySelective(product);
} @Test//删除操作
public void delete(){
productMapper.deleteByPrimaryKey(174L);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

4.4 多条件查询和分页查询

@SpringBootTest
@RunWith(SpringRunner.class)
public class QueryTest {
@Autowired
private ProductMapper productMapper; @Test //根据多条件动态查询
public void queryByParam(){
//多条件查询
Example example = new Example(TProduct.class);
// //添加第1个条件 name:模糊查询
// example.and().andLike("name","%洗澡8%");
//
// //添加第2个条件 :价格在100以内
// example.and()
// .andGreaterThanOrEqualTo("saleprice",0).andLessThanOrEqualTo("saleprice",100);
//
// //添加第3个条件:状态 state =1
// example.and().andEqualTo("state",1);
//优化Sql中的括号 : 当多个条件如果是 平级,则不用example.and()去追加条件
Example.Criteria and = example.and();//Criteria对象:就是用于拼接查询条件,每次执行example.and()或者example.or()将都会创建一个新的查询条件的拼接对象(意味着多一组())
and.andLike("name","%洗澡8%").orEqualTo("state",1);
//再创建一组新的区间查询条件,这个条件它要加(),所以你要重新通过 example对象获取
example.and().andGreaterThanOrEqualTo("saleprice",0).andLessThanOrEqualTo("saleprice",100); List<TProduct> tProducts = productMapper.selectByExample(example); for (TProduct tProduct : tProducts) {
System.out.println(tProduct);
}
} @Test //分页查询
public void queryByPage(){
//不带条件的分页查询 //如果要进行分页查询,只需在调用查询的方法前,设置分页参数即可
//特点注意:当前设置的分页参数,只适用于离它最近的这条查询
PageHelper.startPage(1,3);
//List<TProduct> tProducts = productMapper.selectAll();
PageInfo<TProduct> pageInfo = new PageInfo<>(productMapper.selectAll());
/*
pageInfo中的常用的方法:
总记录数:pageInfo.getTotal()
总页数:pageInfo.getPages()
每页的数据列表:pageInfo.getList() */ System.out.println(pageInfo);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

4.5 添加数据后,立马得到添加数据的主键

  • 当前这个主键是由数据库进行【自增长】设置的
  1. 在实体类的主键ID上添加如下配置
public class TProduct {
@Id //指定主键的注解
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
  • 1
  • 2
  • 3
  • 4
  1. 在需要获取的地方,直接调用get方法即可
    @Test //添加新数据后,获取 自增长主键
public void insertAndGetId(){
TProduct product = new TProduct();
product.setName("我是测试的");
product.setCreatetime(new Date());
product.setState(1L);
productMapper.insert(product); System.out.println(product.getId()); }

原文章:https://blog.csdn.net/x286129277/article/details/112095082

springboot2.x整合tkmapper的更多相关文章

  1. 【SpringBoot】息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ

    ========================13.消息队列介绍和SpringBoot2.x整合RockketMQ.ActiveMQ ======================= 1.JMS介绍和 ...

  2. 第二篇:SpringBoot2.0整合ActiveMQ

    本篇开始将具体介绍SpringBoot如何整合其它项目. 如何创建SpringBoot项目 访问https://start.spring.io/. 依次选择构建工具Maven Project.语言ja ...

  3. 消息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ 9节课

    1.JMS介绍和使用场景及基础编程模型     简介:讲解什么是小写队列,JMS的基础知识和使用场景     1.什么是JMS: Java消息服务(Java Message Service),Java ...

  4. SpringBoot2.x整合Redis实战 4节课

    1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download          2.新手 ...

  5. SpringBoot2.0 整合 QuartJob ,实现定时器实时管理

    一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...

  6. SpringBoot2.0 整合 Swagger2 ,构建接口管理界面

    一.Swagger2简介 1.Swagger2优点 整合到Spring Boot中,构建强大RESTful API文档.省去接口文档管理工作,修改代码,自动更新,Swagger2也提供了强大的页面测试 ...

  7. SpringBoot2.x 整合Spring-Session实现Session共享

    SpringBoot2.x 整合Spring-Session实现Session共享 1.前言 发展至今,已经很少还存在单服务的应用架构,不说都使用分布式架构部署, 至少也是多点高可用服务.在多个服务器 ...

  8. SpringBoot2.0 整合 Dubbo框架 ,实现RPC服务远程调用

    一.Dubbo框架简介 1.框架依赖 图例说明: 1)图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代表层 ...

  9. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

随机推荐

  1. DVWA之CSRF(跨站请求伪造攻击)

    目录 Low Middle High Impossible Low 源代码: <?php if( isset( $_GET[ 'Change' ] ) ) { // Get input $pas ...

  2. Linux-鸟菜-2-主机规划与磁盘分区

    Linux-鸟菜-2-主机规划与磁盘分区 开机流程: 1. BIOS:開機主動執行的韌體,會認識第一個可開機的裝置: 2. MBR:第一個可開機裝置的第一個磁區內的主要開機記錄區塊,內含開機管理程式: ...

  3. json的解析和生成

    相比于xml,json的主要特点是数据小,解析速度快,但是描述性差. java中常见的json解析方法有Gson.Jackson.JSON.simple. 从解析速度上来看,Gson适合解析小数据量, ...

  4. 【哲学角度看软件测试】要想软件“一想之美”,UI 测试少不了

    摘要:软件测试的最高层次需求是:UI测试,也就是这个软件"长得好不好看". 为了让读者更好地理解测试,我们从最基础的概念开始介绍.以一个软件的"轮回"为例,下图 ...

  5. 更好的滚动体验>better-scroll

    认识better-scroll better-scroll是一款重点用于解决移动端(已支持PC)各种滚动场景需求的插件,可使页面滚动效果更加流畅且富有弹性 better-scroll是用纯JavaSc ...

  6. Java 给Word添加数字签名

    本文以Java程序代码为例,介绍如何给Word文档添加数字签名. 程序运行环境 IntedliJ IDEA JDK 1.8.0 Jar包:spire.doc.jar 4.5.1 Word文档:.doc ...

  7. 自定义WPF分页控件

    一.分页控件功能说明 实现如上图所示的分页控件,需要实现一下几个功能: 可以设置每页能够展示的最大列数(例如每页8列.每页16列等等). 加载的数组总数量超过设置的每页列数后,需分页展示. 可以直接点 ...

  8. 【BUAA软工】结对编程作业

    项目 内容 课程:2020春季软件工程课程博客作业(罗杰,任健) 博客园班级链接 作业:BUAA软件工程结对编程项目作业 作业要求 课程目标 学习大规模软件开发的技巧与方法,锻炼开发能力 作业目标 完 ...

  9. java基础——初识面向对象

    面向对象 面向过程&面向对象 面向过程思想 步骤请简单:第一步做什么,第一步做什么 面向过程适合处理一些较为简单的东西 面向对象思想 物以类聚,分类的思维模式,思考的问题首先会解决问题需要哪些 ...

  10. [bug] kibana:prevMsg":"Request Timeout after 3000ms

    ES启动问题,内存不足 https://blog.csdn.net/qq_40907977/article/details/104499178 修改ES启动内存 https://blog.csdn.n ...