[size=large]头一篇博文说了整合了spring boot和mybatis大家都可以对后台数据进行操作但是如何才能进行高效的操作数据库节省代码,下面来一起谈论哈mybatis的通用mapper。

第一章:pom文件解释

maven的pom文件里面需要引进什么依赖呢?

<dependency>
<!--工具接口类用来实体mapper继承 -->
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>

引进com.github.jsqlparser是对tk.mybatis的支撑:例如我们一些简单的complainMapper.selectAll()的查询所有操作不用再去mybatis的xml写一个查询直接用mapper点就会有很多的方法出来,这里的com.github.jsqlparser会帮我们把这些方法解析为sql,当然这里的mapper必须继承一些东西下面仔细讲解,怎么继承,怎么使用。

第二步:基类mapper和子类mapper

1.首先我们创建一个类用来继承通用mapper

public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {

}

2.实体mapper再来继承我们创建的BaseMapper接口

public interface ComplainMapper extends BaseMapper<Complain>{
}

3.我把实体类给出来

@Table(name="complain")
public class Complain {
@Id
private Integer id;
@Column
private String openId;//用户名
@Column
private Date complainTime;//投诉时间
@Column
private Date replyTime;//回复时间
@Column
private Integer replyName;//回复名字
@Column
private Date createTime;//创建时间
@Column
private String complainContent;//投诉内容
@Column
private String replyContent;//回复内容
@Column
private Date updateTime;//更新时间

get  set我就不给出来了

4.操作ComplainMapper

我们现在不用写xml直接来看ComplainMapper里面给我们提供了那些方法







我们现在可以明确看到已经有了很多的方法了,这些方法可以够我们进行一些简单的操作也方便了我们少写一些xml配置和接口代码,相信大家只要按照这个步骤来都可以实现简单的crud

5.实现模糊查询

 public List<Complain> selectComplainNoReplyContent() {
// tk -mybatis
Example example = new Example(Complain.class);
// replyContent对应数据库字段hha则是查询是否有hha相等于select *from complian where reply_complian="hha"
example.createCriteria().andEqualTo("replyContent", "hha");
return complainMapper.selectByExample(example);
}

之只是一个例子还有很多模糊查询的样式大家可以去慢慢探索

6.我这个环境是在头一个博文的环境下搭建的,我下面也把代码给出来。。。。大家可以进行一些cuud了,最近也是有点忙跟新很慢希望大家谅解

[/size]

mybatis spring boot深入的更多相关文章

  1. mybatis+spring boot, mapper 提示Could not autowire. No beans of … type found

    工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql. 业务逻辑关系:controlle ...

  2. spring boot:配置shardingsphere(sharding jdbc)使用druid数据源(druid 1.1.23 / sharding-jdbc 4.1.1 / mybatis / spring boot 2.3.3)

    一,为什么要使用druid数据源? 1,druid的优点 Druid是阿里巴巴开发的号称为监控而生的数据库连接池 它的优点包括: 可以监控数据库访问性能 SQL执行日志 SQL防火墙 但spring ...

  3. spring boot:使用分布式事务seata(druid 1.1.23 / seata 1.3.0 / mybatis / spring boot 2.3.2)

    一,什么是seata? Seata:Simpe Extensible Autonomous Transcaction Architecture, 是阿里中间件开源的分布式事务解决方案. 前身是阿里的F ...

  4. Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:

    Parameter 0 of method orderSqlSessionFactory in com.config.MultipleDBConfig required a single bean, ...

  5. mybatis+spring boot+vue

    参考https://www.cnblogs.com/wlovet/p/8317282.html

  6. spring boot(六):如何优雅的使用mybatis

    *:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...

  7. 超简单,spring boot 添加mybatis

    看了很多人写的博客,要么太复杂,要么没有添加xml的方式,自己亲自配置了一下,供各位参考. 项目截图 1.添加pom文件 <!-- 设置mybatis --> <dependency ...

  8. Spring Boot 4 MyBatis

    SpringBoot内使用MyBatis,可以不使用xml映射配置,通过注解方式映射. pom.xml添加依赖 <dependency> <groupId>org.mybati ...

  9. Spring Boot MyBatis 连接数据库

    最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...

随机推荐

  1. jquery滚动事件

    滚动到一定高度: $(window).scroll(function(){ var scrollTop = $(document).scrollTop(); && scrollTop ...

  2. 团队作业之Rookie also want to fly

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 在时代的浪潮下,单人编程,结对编程已经无法满 ...

  3. 利用Swoole编写一个TCP服务器,顺带测试下Swoole的4层生命周期

    1首先我们写一个入口脚本,这里简单点的功能就是开启服务和关闭服务 <?php //CLI命令 if(isset($argv[1]) && in_array($argv[1], [ ...

  4. 开源项目CircleImageView

    1.在自己MainActivity所在包下创建CircleImageView.class文件 package 自己包名; import android.content.Context; import ...

  5. 探索未知种族之osg类生物---呼吸分解之事件循环三

    那我们就开始处理这些事件中得到的所有的交互事件,首先我们要判断这些事件是否包含osg的退出事件,那什么情况下会触发这个退出事件呢?如果您运行过osg中example中的小例子的,聪明的你一定就会发现当 ...

  6. 错误:在非结构或联合中请求成员‘next’

    #include <stdio.h> #include <stdlib.h> #define ElemType int #define Status int #define O ...

  7. BZOJ1228或洛谷2148 [SDOI2009]E&D

    BZOJ原题链接 洛谷原题链接 完全不会呀.. 写了这题才知道\(SG\)函数原来也能打表找规律... 题解请看大佬的博客 #include<cstdio> using namespace ...

  8. Tinyos学习笔记(二)

    1.TinyOS communication tools java serialApp -comm serial@/dev/ttyUSB0:telosb java net.tinyos.tools.L ...

  9. tableView的cell之间间隔显示空白区域

    //再要创建的cell中修改frame - (void)setFrame:(CGRect)frame{ frame.origin.x += ; frame.origin.y += ; frame.si ...

  10. ES线程池

    每个Elasticsearch节点内部都维护着多个线程池,如index.search.get.bulk等,用户可以修改线程池的类型和大小,线程池默认大小跟CPU逻辑一致 一.查看当前线程组状态 cur ...