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 ...
随机推荐
- Bootstrap(10) 进度条媒体对象和 Well 组件
一.Well 组件这个组件可以实现简单的嵌入效果. <!-- //嵌入效果 --> <div class="well">Bootstrap</div& ...
- python requests的content和text方法的区别(转)
原文地址: http://blog.csdn.net/xie_0723/article/details/51361006 问题: 一直在想requests的content和text属性的区别,从pri ...
- vm参数配置的理解
-denv=dev表示将服务器的级别 设置为开发环境 所有错误的内容都会打印在控制台上 //The-Denv = dev statement creates a system property nam ...
- wget: command not found
支持yum yum -y install wget 不支持yum a.查看当前linux版本 cat /etc/issue && uname -a b.根据版本下载相应的rpm htt ...
- sap 给集团分配一个逻辑系统
1.进入事务代码 SALE定义一个新的逻辑系统 2.通过事务代码RZ10 进入之后, 将参数login/no_automatic_user_sapstar 修改为“0”, 然后重启SAP服务生效 3. ...
- hdu 1059 (多重背包) Dividing
这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...
- HDU_1142(最短路 + dfs)
Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...
- iOS.Library.Architecture
在用file查看library的architechture时有以下输出: $ file WebPWebP: Mach-O universal binary with 3 architecturesWe ...
- spring读取properties的几种方式
参考链接:http://www.cnblogs.com/zxf330301/p/6184139.html
- BZOJ4033或洛谷3177 [HAOI2015]树上染色
BZOJ原题链接 洛谷原题链接 很明显的树形\(DP\). 因为记录每个点的贡献很难,所以我们可以统计每条边的贡献. 对于每一条边,设边一侧的黑点有\(B_x\)个,白点有\(W_x\),另一侧黑点有 ...