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

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

User实体类:

package com.example.demo.entity;

public class User {

    private Long id;

    private String name;

    private int age;

    public Long getId()
{
return id;
} public void setId(Long id)
{
this.id = id;
} public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} public int getAge()
{
return age;
} public void setAge(int age)
{
this.age = age;
}
}

Dao:

package com.example.demo.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select; import com.example.demo.entity.User; @Mapper
public interface UserDao { @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age") })
@Select("SELECT * FROM user WHERE age = #{age}")
List<User> get(int age); @Insert("INSERT INTO user(id, name, age) VALUES (#{id}, #{name}, #{age})")
void insert(User user);
}

UserService:

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.example.demo.dao.UserDao;
import com.example.demo.entity.User; @Service
public class UserService { @Autowired
private UserDao userDao; public String show()
{
return "Hello World!";
} public String insert(String name, int age)
{
User user = new User();
user.setId(System.currentTimeMillis());
user.setName(name);
user.setAge(age);
userDao.insert(user);
return "Insert ( \"" + name + "\", age" + age + ") OK!";
}
}

UserController:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.example.demo.service.UserService; @RestController
public class UserController { @Autowired
private UserService userService; @RequestMapping(value = "/show")
public String show()
{
return userService.show();
} @RequestMapping(value = "/insert")
public String insert(String name, int age)
{
return userService.insert(name, age);
}
}

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

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan(basePackages = { "com.example.demo.dao" })
public class DemoApplication { public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}

启动,访问: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. JDBC01 获取数据库连接

    概述 Java Database Connectivity(JDBC)直接访问数据库,通用的SQL数据库存取和操作的公共接口,定义访问数据库的标准java类库(java.sql,javax.sql) ...

  2. oracle keep

    语法: min | max(column1) keep (dense_rank first | last order by column2) over (partion by column3); -- ...

  3. Spring Cloud使用

    一.创建提供者工程01-provider-8081 (1) 创建工程 创建一个Spring Initializr工程,并命名为01-provider-8081.导入Lombok.Web.JPA及MyS ...

  4. 【编程思想】【设计模式】【结构模式Structural】享元模式flyweight

    Python版 https://github.com/faif/python-patterns/blob/master/structural/flyweight.py #!/usr/bin/env p ...

  5. js 时间戳转换为年月日时分秒的格式

    <script type="text/javascript"> var strDate = ''; $(function(){ // 获取时间戳 var nowDate ...

  6. 记一次 .NET 某妇产医院 WPF内存溢出分析

    一:背景 1. 讲故事 上个月有位朋友通过博客园的短消息找到我,说他的程序存在内存溢出情况,寻求如何解决. 要解决还得通过 windbg 分析啦. 二:Windbg 分析 1. 为什么会内存溢出 大家 ...

  7. drone 使用git tag触发构建

    配置ref为tag .drone.yml中配置trigger为ref trigger: ref: - refs/tags/FileService 或者配置when为ref when: ref: - r ...

  8. TMS570LS3137笔记-内部Flash FEE使用

    1.基本简介 TMS570LS3137内部Flash分为三个 Bank,主Flash 数据存储区3MB,是Bank1和Bank2.还有一个Bank7是作为内部Flash模拟EEPROM使用.内部存储器 ...

  9. LR常见报错

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

  10. Table.NestedJoin合并…Join(Power Query 之 M 语言)

    数据源: "销量表"和"部门表"两个查找表,每个表中都有"姓名"列 目标: 根据"姓名列"将"部门表" ...