0.相关配置

pom.xml

<!--mysql依赖-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency><!--mybatis依赖-->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version>
</dependency><!-- 为简化set,get,toString代码引入lombok -->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.2</version><scope>provided</scope>
</dependency>

application.yml

spring:datasource:url: jdbc:mysql://localhost:3306/zoom?useSSL=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=trueusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver
使用的表
sword.png

1.剑的实体类:com.toly1994.toly_mybatis.entity.Sword
/*** 作者:张风捷特烈* 时间:2018/8/17 0017:8:23* 邮箱:1981462002@qq.com* 说明:剑的实体类*/
@Data//=@Getter +@Setter
public class Sword {private Integer id;private String name;private Integer atk;private Integer hit;private Integer crit;private Integer attr_id;private Integer type_id;
}
2.剑的数据映射类:com.toly1994.toly_mybatis.mapper.SwordMapper
/*** 作者:张风捷特烈* 时间:2018/8/17 0017:8:41* 邮箱:1981462002@qq.com* 说明:剑的数据映射类*/
public interface SwordMapper {/*** 查询** @param name* @return*/@Select("SELECT*FROM sword WHERE NAME=#{name}")Sword findByName(@Param("name") String name);/*** 添加** @param name* @param atk* @param hit* @param crit* @param attr_id* @param type_id* @return*/@Select("INSERT sword VALUES(DEFAULT,#{name},#{atk},#{hit},#{crit},#{attr_id},#{type_id})")void insert(@Param("name") String name,@Param("atk") Integer atk,@Param("hit") Integer hit,@Param("crit") Integer crit,@Param("attr_id") Integer attr_id,@Param("type_id") Integer type_id);
}
3.服务层:com.toly1994.toly_mybatis.service.SwordService
/*** 作者:张风捷特烈* 时间:2018/8/17 0017:8:48* 邮箱:1981462002@qq.com* 说明:服务层*/
@Service//坑点1别忘加Service注解
public class SwordService {@Autowiredprivate SwordMapper mSwordMapper;public void insertSword(String name, Integer atk,Integer hit,Integer crit,Integer attr_id,Integer type_id) {mSwordMapper.insert(name, atk, hit, crit, attr_id, type_id);}public Sword findByName(String name) {return mSwordMapper.findByName(name);}
}
4.剑的控制层:com.toly1994.toly_mybatis.controller.SwordController
/*** 作者:张风捷特烈* 时间:2018/8/17 0017:8:55* 邮箱:1981462002@qq.com* 说明:剑的控制层*/
@RestController
public class SwordController {@Autowiredprivate SwordService mSwordService;@GetMapping("/insert")public String insertSword(String name, Integer atk,Integer hit,Integer crit,Integer attr_id,Integer type_id) {mSwordService.insertSword(name, atk, hit, crit, attr_id, type_id);return "OK";}@GetMapping("/findByName")public Sword findByName(String name) {return mSwordService.findByName(name);}
}
5.启动类:com.toly1994.toly_mybatis.TolyMybatisApplication
@SpringBootApplication
//坑点2,别忘了扫包
@MapperScan(basePackages = {"com.toly1994.toly_mybatis.mapper"})
public class TolyMybatisApplication {public static void main(String[] args) {SpringApplication.run(TolyMybatisApplication.class, args);}
}
6.结果
插入接口:
http://localhost:8080/insert?name=赤凰&atk=3000&hit=100&crit=5&attr_id=1&type_id=2
查询接口:
http://localhost:8080/findByName?name=赤凰
mybatis测试.png

7.事务
故意造异常:com.toly1994.toly_mybatis.service.SwordService#insertSword
public void insertSword(String name, Integer atk,Integer hit,Integer crit,Integer attr_id,Integer type_id) {mSwordMapper.insert(name, atk, hit, crit, attr_id, type_id);int i = 1 / atk;//异常处
}
http://localhost:8080/insert?name=赤凰&atk=0&hit=100&crit=5&attr_id=1&type_id=2
no_transactional.png
使用事务:com.toly1994.toly_mybatis.service.SwordService#insertSword
@Transactional
public void insertSword(String name, Integer atk,Integer hit,Integer crit,Integer attr_id,Integer type_id) {mSwordMapper.insert(name, atk, hit, crit, attr_id, type_id);int i = 1 / atk;
}
Transactional.png

8.分页
8-1:pom.xml
<!-- 分页插件 -->
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.5</version>
</dependency>
8-2:application.yml
pagehelper:helper-dialect: mysqlreasonable: truesupport-methods-arguments: trueparams: count==countSqlpage-size-zero: true
8-3:com.toly1994.toly_mybatis.mapper.SwordMapper
    /*** 查询所有** @return*/@Select("SELECT*FROM sword")List<Sword> findALL();
8-4:com.toly1994.toly_mybatis.service.SwordService
    /*** 分页查询* @param page 当前页数* @param pageSize 每页个数* @return*/public PageInfo<Sword> findAll(int page,int pageSize) {PageHelper.startPage(page, pageSize);//改写语句实现分页查询List<Sword> all = mSwordMapper.findALL();PageInfo<Sword> info = new PageInfo<>(all);return info;}
8-5:com.toly1994.toly_mybatis.controller.SwordController
    //http://localhost:8080/findAllByPage?page=1&pageSize=3@GetMapping("/findAllByPage")public PageInfo<Sword> findAll(int page, int pageSize) {return mSwordService.findAll(page, pageSize);}
8-6:结果演示:
sword表.png
分页查询.png

SpringBoot-15-之整合MyBatis-注解篇+分页相关推荐

  1. SpringBoot 实战 (九) | 整合 Mybatis

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,本文通过注解的形式实 ...

  2. mybatis java类注解式_Spring整合Mybatis注解方式

    Spring整合Mybatis(注解方式) 环境准备 jar包: Spring所需依赖:spring-context.spring-aspects.aspectjrt.aspectjweaver.sp ...

  3. 利用IDEA搭建SpringBoot项目,整合mybatis,实现简单的登录功能。

    利用IDEA搭建SpringBoot项目,整合mybatis,实现简单的登录功能. 仅供参考!!! 仅供参考!!! 仅供参考!!! 利用闲余时间想自己搭建一个springboot+mybatis的项目 ...

  4. 3、SpringBoot整合MyBatis注解版及配置文件版

    目录 1.配置pom.xml 2.配置application.yml 3.配置DruidConfig关联yml的配置文件spring.datasource 4.创建数据库及数据库表结构 5.创建对应的 ...

  5. 玩转 SpringBoot 2.x 整合 Mybatis

    前言 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 ...

  6. 玩转 SpringBoot 2 快速整合 | RESTful Api 篇

    概述 RESTful 是一种架构风格,任何符合 RESTful 风格的架构,我们都可以称之为 RESTful 架构.我们常说的 RESTful Api 是符合 RESTful 原则和约束的 HTTP ...

  7. Spring整合Mybatis注解方式

    Spring整合Mybatis(注解方式) 目录 Spring整合Mybatis(注解方式) 环境准备 纯注解方式 配置类具体内容 测试类具体内容 环境准备 jar包: Spring所需依赖:spri ...

  8. SpringBoot 2.x 整合Mybatis二:PageHelper分页

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80696898 本文出自[赵彦军的博客] Mybatis-PageHelper 简介 ...

  9. Java使用lambda进行分页,SpringBoot(八):整合mybatis,通用mapper,分页插件,lambda,Logger,junit用法...

    本文作者:低调小熊猫 转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032 整合demo 配置依赖 tk.mybatis mapp ...

  10. springboot动态切换数据源_Springboot整合Mybatis注解实现动态数据源切换

    AbstractRoutingDataSource AbstractRoutingDataSource是spring-jdbc包提供的一个了AbstractDataSource的抽象类,它实现了Dat ...

最新文章

  1. RedHat7/CentOS7 压缩解压命令汇总
  2. WPF usercontrol 自定义依赖属性
  3. unused import statement
  4. 如何根据SAP CRM扩展字段的UI标签找到其ID
  5. Codeforces Round #588 (Div. 2) D. Marcin and Training Camp 思维
  6. [设计模式] 17 中介者模式 Mediator Pattern
  7. IDEA连接Git后类的颜色含义
  8. SQL Server 默认跟踪 -- 捕获事件详解
  9. 【OCR技术】字符识别技术总览
  10. spring事物配置
  11. python : class定义中的:__dict__,__setattr__,__getattribute__,__getattr__,
  12. 【读】这一次,让我们再深入一点 - TCP协议
  13. Quartz.NET WinFrom 关闭程序后无法结束进程
  14. css模拟title和alt的提示效果[转]
  15. beego mysql增删改查_5-BeegoORM增删改查-Go语言中文社区
  16. 第十二届蓝桥杯模拟赛Python组(第三期)
  17. 02-Spring的核心API
  18. 关于运行微信小程序报错 [微信小程序开发者工具] Error: read EBADF
  19. 多台服务器集群部署方案
  20. 又一家初创公司获得数千万融资进军边缘计算赛道!

热门文章

  1. Android表格布局之设置边框
  2. 微信公众平台开发(51)会员卡
  3. jQuery:1.5.1,复选框应用(全选,全不选,反选,提交选中的值,全选/全不选)...
  4. Delphi读写UTF-8、Unicode格式文本文件
  5. 在面向服务的设计时有四个原则:
  6. 使用 ASP.NET 2.0 ObjectDataSource 控件(整理自msdn)
  7. SQL数据库中主键和外键的应用实例
  8. 【事件驱动】【蓝牙控制车锁或电灯】初步方案
  9. VS学习笔记(一)创建C++项目
  10. 基于GEE平台分析湖北省近35年地表水变化特征