[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. Git下的.DS_Store文件

    .DS_Store 是什么 使用 Mac 的用户可能会注意到,系统经常会自动在每个目录生成一个隐藏的 .DS_Store 文件..DS_Store(英文全称 Desktop Services Stor ...

  2. listView 解决焦点冲突问题 item项和子控件之间的冲突

    listView 在item布局的顶级布局中设置 android:descendantFocusability="blocksDescendants"可以阻止子控件获取焦点 这样使 ...

  3. Vue 安装脚手架 工具 vue-cli (最新)

    假如您安装过旧版脚手架工具(vue-cli),您可以通过 npm uninstall vue-cli -g 或 yarn global remove vue-cli卸载. Vue CLI 需要Node ...

  4. 对于读txt文件一点总结

    txt 内容 中间有比如如空格,制表符(tab)在txt为空格符(Spaces).回车符.换行符,有空字符串等情况,在读取过滤中要充分考虑到 1:打开文件 var sr=new StreamReade ...

  5. istio promethus收集不到数据

    1.重新apply istio.yaml kubectl apply -f install/kubernetes/istio.yaml kubectl get metrics.config.istio ...

  6. C++中stl的map

    总结: 在map中插入数据有三种方法: 1.用insert插入pair数据: mapstudent.insert(pair<int,string>(1,"studentone&q ...

  7. poj 1321 (简单DFS) 棋盘问题

    题目:http://poj.org/problem?id=1321 最近状态有点down, 练练手 #include<cstdio> using namespace std; ][]; ] ...

  8. Python.SQLAlchemy.0

    1. SQLAlchemy and You http://lucumr.pocoo.org/2011/7/19/sqlachemy-and-you/ 2. Overview http://docs.s ...

  9. 用vs2015 编译 web app ionic

    1.要下载https://git-for-windows.github.io/ 2.下载ionic模版

  10. 通过代理上网时,qq等应用程序连网出错

    虽然现在基本上都用无线,有线宽带等,但是有时候还是避免不了通过代理上网时,于是就发生浏览器可以正常浏览网页,qq等应用程序连接出错等问题,上网搜了好长时间,    都没解决问题,后来慢慢琢磨(其实是乱 ...