mybatis spring boot深入
[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深入的更多相关文章
- mybatis+spring boot, mapper 提示Could not autowire. No beans of … type found
工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql. 业务逻辑关系:controlle ...
- 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 ...
- 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 ...
- 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, ...
- mybatis+spring boot+vue
参考https://www.cnblogs.com/wlovet/p/8317282.html
- spring boot(六):如何优雅的使用mybatis
*:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...
- 超简单,spring boot 添加mybatis
看了很多人写的博客,要么太复杂,要么没有添加xml的方式,自己亲自配置了一下,供各位参考. 项目截图 1.添加pom文件 <!-- 设置mybatis --> <dependency ...
- Spring Boot 4 MyBatis
SpringBoot内使用MyBatis,可以不使用xml映射配置,通过注解方式映射. pom.xml添加依赖 <dependency> <groupId>org.mybati ...
- Spring Boot MyBatis 连接数据库
最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...
随机推荐
- eval详解
eval()的作用 把字符串参数解析成js代码并运行,并返回执行的结果: eval的作用域 作用域在它所有的范围内容有效 IE8及以下指向window 解决方法: functiona(){ if(wi ...
- bean生命周期_Junit测试使用factory模式
在面试某互联网_保险 公司, 被问到了spring中bean的生命周期,不禁联想到我们之前作 junit测试时,使用了Factory模式,而没有用Mock.
- [Z]haproxy+keepalived高可用群集
http://blog.51cto.com/13555423/2067131 Haproxy是目前比较流行的一种集群调度工具Haproxy 与LVS.Nginx的比较LVS性能最好,但是搭建相对复杂N ...
- [z]nativeSql
http://blog.csdn.net/chenallen1025/article/details/9169543 EntityManager API 提供了创建 Query 实例以执行原生 SQL ...
- 使用clear来清除localStorage保存对象的全部数据
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- c# 子线程打开子窗体
下边是在子线程打开子窗口,结果跑到else 里边了跨线程操作窗体控件InvokeRequired失效,无法用于打开子窗体,addonetwo.InvokeRequired,访问不了呢? 大神知道帮忙回 ...
- IntelliJ idea 的破解
·1.破解的jar包下载链接: https://pan.baidu.com/s/1JV6GwguGQNs5pNQtst29Hw 提取码: u2jd 2.安装和破解地址:https://www.cnb ...
- FoxMail提示:请求的名称有效,但是找不到请求的类型的数据
FoxMail发送或者接收邮件的时候,提示如下信息: <错误信息:请求的名称有效,但是找不到请求的类型的数据> 一,DNS解析不稳定 解决办法:修改本地电脑上面本地连接中的DNS地址< ...
- [Groovy] 创建Excel,追加Excel
package ScriptLibrary import java.awt.Color import java.awt.GraphicsConfiguration.DefaultBufferCapab ...
- c sharp dll
1. generate dll building .cs file, for example: myDll.cs using System; using System.Collections.Gene ...