Spring Boot集成Mybatis非常简单,在之前搭建好的项目中加入Mybatis依赖的jar,在配置文件中加入数据库配置即可,如下图所示:

创建对应的Controller、Service、Dao等,代码如下:

User实体类:

  1. package com.example.demo.entity;
  2.  
  3. public class User {
  4.  
  5. private Long id;
  6.  
  7. private String name;
  8.  
  9. private int age;
  10.  
  11. public Long getId()
  12. {
  13. return id;
  14. }
  15.  
  16. public void setId(Long id)
  17. {
  18. this.id = id;
  19. }
  20.  
  21. public String getName()
  22. {
  23. return name;
  24. }
  25.  
  26. public void setName(String name)
  27. {
  28. this.name = name;
  29. }
  30.  
  31. public int getAge()
  32. {
  33. return age;
  34. }
  35.  
  36. public void setAge(int age)
  37. {
  38. this.age = age;
  39. }
  40. }

Dao:

  1. package com.example.demo.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.ibatis.annotations.Insert;
  6. import org.apache.ibatis.annotations.Mapper;
  7. import org.apache.ibatis.annotations.Result;
  8. import org.apache.ibatis.annotations.Results;
  9. import org.apache.ibatis.annotations.Select;
  10.  
  11. import com.example.demo.entity.User;
  12.  
  13. @Mapper
  14. public interface UserDao {
  15.  
  16. @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age") })
  17. @Select("SELECT * FROM user WHERE age = #{age}")
  18. List<User> get(int age);
  19.  
  20. @Insert("INSERT INTO user(id, name, age) VALUES (#{id}, #{name}, #{age})")
  21. void insert(User user);
  22. }

UserService:

  1. package com.example.demo.service;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import com.example.demo.dao.UserDao;
  7. import com.example.demo.entity.User;
  8.  
  9. @Service
  10. public class UserService {
  11.  
  12. @Autowired
  13. private UserDao userDao;
  14.  
  15. public String show()
  16. {
  17. return "Hello World!";
  18. }
  19.  
  20. public String insert(String name, int age)
  21. {
  22. User user = new User();
  23. user.setId(System.currentTimeMillis());
  24. user.setName(name);
  25. user.setAge(age);
  26. userDao.insert(user);
  27. return "Insert ( \"" + name + "\", age" + age + ") OK!";
  28. }
  29. }

UserController:

  1. package com.example.demo;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6.  
  7. import com.example.demo.service.UserService;
  8.  
  9. @RestController
  10. public class UserController {
  11.  
  12. @Autowired
  13. private UserService userService;
  14.  
  15. @RequestMapping(value = "/show")
  16. public String show()
  17. {
  18. return userService.show();
  19. }
  20.  
  21. @RequestMapping(value = "/insert")
  22. public String insert(String name, int age)
  23. {
  24. return userService.insert(name, age);
  25. }
  26. }

DemoApplication中加入扫描Mapper的注解,修改后的代码如下所示:

  1. package com.example.demo;
  2.  
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6.  
  7. @SpringBootApplication
  8. @MapperScan(basePackages = { "com.example.demo.dao" })
  9. public class DemoApplication {
  10.  
  11. public static void main(String[] args)
  12. {
  13. SpringApplication.run(DemoApplication.class, args);
  14. }
  15. }

启动,访问:http://localhost:8080/insert?name=yyy&age=20 插入数据成功:

集成Mybatis完成。

Spring Boot实战二:集成Mybatis的更多相关文章

  1. 6、Spring Boot 2.x 集成 MyBatis

    1.6 Spring Boot 2.x 集成 MyBatis 简介 详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 完整源码: 1.6.1 创建 spring-bo ...

  2. Spring Boot实战:集成Swagger2

    一.Swagger简介 上一篇文章中我们介绍了Spring Boot对Restful的支持,这篇文章我们继续讨论这个话题,不过,我们这里不再讨论Restful API如何实现,而是讨论Restful ...

  3. Spring Boot 数据访问集成 MyBatis 与事物配置

    对于软件系统而言,持久化数据到数据库是至关重要的一部分.在 Java 领域,有很多的实现了数据持久化层的工具和框架(ORM).ORM 框架的本质是简化编程中操作数据库的繁琐性,比如可以根据对象生成 S ...

  4. Spring Boot 实战 —— MyBatis(注解版)使用方法

    原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...

  5. Spring Boot实战系列(7)集成Consul配置中心

    本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...

  6. spring boot实战(第十二篇)整合RabbitMQ

    前言 最近几篇文章将围绕消息中间件RabbitMQ展开,对于RabbitMQ基本概念这里不阐述,主要讲解RabbitMQ的基本用法.Java客户端API介绍.spring Boot与RabbitMQ整 ...

  7. Spring Boot 实战与原理分析视频课程

    Spring Boot 实战与原理分析视频课程 链接:https://pan.baidu.com/share/init?surl=PeykcoeqZtd1d9lN9V_F-A 提取码: 关注公众号[G ...

  8. spring boot 实战教程

    二八法则 - get more with less Java.spring经过多年的发展,各种技术纷繁芜杂,初学者往往不知道该从何下手.其实开发技术的世界也符合二八法则,80%的场景中只有20%的技术 ...

  9. spring boot / cloud (二) 规范响应格式以及统一异常处理

    spring boot / cloud (二) 规范响应格式以及统一异常处理 前言 为什么规范响应格式? 我认为,采用预先约定好的数据格式,将返回数据(无论是正常的还是异常的)规范起来,有助于提高团队 ...

随机推荐

  1. words in English that contradict themselves

    [S1E10, TBBT]Leonard: I don't get it. I already told her a lie. Why would I replace it with a differ ...

  2. python生成器浅析

    A 'generator' is a function which returns a generator iterator. It looks like a normal function exce ...

  3. Spark的shuffle和MapReduce的shuffle对比

    目录 MapperReduce的shuffle Spark的shuffle 总结 MapperReduce的shuffle shuffle阶段划分 Map阶段和Reduce阶段 任务 MapTask和 ...

  4. C++字节对齐(对象大小)

    内部数据成员对齐参考这篇 https://www.cnblogs.com/area-h-p/p/10316128.html 这里只强调C++字节对齐特点 ①静态数据成员属于类域,在对象中不占大小 ②若 ...

  5. 【Linux】【Services】【Package】Basic

    Linux程序包管理           概述         API:Application Program Interface         ABI:Application Binary Int ...

  6. 【Java基础】JAVA中优先队列详解

    总体介绍 优先队列的作用是能保证每次取出的元素都是队列中权值最小的(Java的优先队列每次取最小元素,C++的优先队列每次取最大元素).这里牵涉到了大小关系,元素大小的评判可以通过元素本身的自然顺序( ...

  7. Mysql安全加固

    1.确保MYSQL_PWD环境变量未设置 描述 MYSQL_PWD环境变量的使用意味着MYSQL凭证的明文存储,极大增加MySQL凭据泄露风险. 加固建议 删除系统环境变量中MySQL密码(MYSQL ...

  8. 转:Intent 操作常用URI代码示例

    以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent 一.打开一个网页,类别是Intent.ACTION_VIEW 1 2 Uri uri = Uri.parse(&q ...

  9. LR常见报错

    转:https://blog.csdn.net/yoyo_sunny/article/details/43406503

  10. python selenium 多账户自动登入163邮箱

    pycharm一些快捷键: ' ctrl ' +' / ' :注释 ' Tab ' :同时缩进 ' shift ' +' Tab ' :左移 一次缩进 本文webinfo.txt路径:C:\Pytho ...