Spring Boot实战二:集成Mybatis
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的更多相关文章
- 6、Spring Boot 2.x 集成 MyBatis
1.6 Spring Boot 2.x 集成 MyBatis 简介 详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 完整源码: 1.6.1 创建 spring-bo ...
- Spring Boot实战:集成Swagger2
一.Swagger简介 上一篇文章中我们介绍了Spring Boot对Restful的支持,这篇文章我们继续讨论这个话题,不过,我们这里不再讨论Restful API如何实现,而是讨论Restful ...
- Spring Boot 数据访问集成 MyBatis 与事物配置
对于软件系统而言,持久化数据到数据库是至关重要的一部分.在 Java 领域,有很多的实现了数据持久化层的工具和框架(ORM).ORM 框架的本质是简化编程中操作数据库的繁琐性,比如可以根据对象生成 S ...
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
- Spring Boot实战系列(7)集成Consul配置中心
本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...
- spring boot实战(第十二篇)整合RabbitMQ
前言 最近几篇文章将围绕消息中间件RabbitMQ展开,对于RabbitMQ基本概念这里不阐述,主要讲解RabbitMQ的基本用法.Java客户端API介绍.spring Boot与RabbitMQ整 ...
- Spring Boot 实战与原理分析视频课程
Spring Boot 实战与原理分析视频课程 链接:https://pan.baidu.com/share/init?surl=PeykcoeqZtd1d9lN9V_F-A 提取码: 关注公众号[G ...
- spring boot 实战教程
二八法则 - get more with less Java.spring经过多年的发展,各种技术纷繁芜杂,初学者往往不知道该从何下手.其实开发技术的世界也符合二八法则,80%的场景中只有20%的技术 ...
- spring boot / cloud (二) 规范响应格式以及统一异常处理
spring boot / cloud (二) 规范响应格式以及统一异常处理 前言 为什么规范响应格式? 我认为,采用预先约定好的数据格式,将返回数据(无论是正常的还是异常的)规范起来,有助于提高团队 ...
随机推荐
- 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 ...
- python生成器浅析
A 'generator' is a function which returns a generator iterator. It looks like a normal function exce ...
- Spark的shuffle和MapReduce的shuffle对比
目录 MapperReduce的shuffle Spark的shuffle 总结 MapperReduce的shuffle shuffle阶段划分 Map阶段和Reduce阶段 任务 MapTask和 ...
- C++字节对齐(对象大小)
内部数据成员对齐参考这篇 https://www.cnblogs.com/area-h-p/p/10316128.html 这里只强调C++字节对齐特点 ①静态数据成员属于类域,在对象中不占大小 ②若 ...
- 【Linux】【Services】【Package】Basic
Linux程序包管理 概述 API:Application Program Interface ABI:Application Binary Int ...
- 【Java基础】JAVA中优先队列详解
总体介绍 优先队列的作用是能保证每次取出的元素都是队列中权值最小的(Java的优先队列每次取最小元素,C++的优先队列每次取最大元素).这里牵涉到了大小关系,元素大小的评判可以通过元素本身的自然顺序( ...
- Mysql安全加固
1.确保MYSQL_PWD环境变量未设置 描述 MYSQL_PWD环境变量的使用意味着MYSQL凭证的明文存储,极大增加MySQL凭据泄露风险. 加固建议 删除系统环境变量中MySQL密码(MYSQL ...
- 转:Intent 操作常用URI代码示例
以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent 一.打开一个网页,类别是Intent.ACTION_VIEW 1 2 Uri uri = Uri.parse(&q ...
- LR常见报错
转:https://blog.csdn.net/yoyo_sunny/article/details/43406503
- python selenium 多账户自动登入163邮箱
pycharm一些快捷键: ' ctrl ' +' / ' :注释 ' Tab ' :同时缩进 ' shift ' +' Tab ' :左移 一次缩进 本文webinfo.txt路径:C:\Pytho ...