SpringBoot数据访问之整合mybatis注解版
SpringBoot数据访问之整合mybatis注解版
mybatis注解版:
贴心链接:Github
在网页下方,找到快速开始文档
上述链接方便读者查找。
通过快速开始文档,搭建环境:
创建数据库:
use vuesite;
CREATE TABLE city
(
id INT PRIMARY KEY auto_increment,
name VARCHAR(255),
state VARCHAR(255),
country VARCHAR(255)
);

创建实体类:
package com.xbhog.pojo;
import lombok.Data;
@Data
public class City {
private Long id;
private String name;
private String state;
private String country;
}
创建Mapper:
创建CityMapper并采用注解的方式实现sql映射的问题:
package com.xbhog.Mapper;
import com.xbhog.pojo.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CityMapper {
@Select("select * from user where id = #{id}")
public City getCityId(Long id);
}
创建Service:
package com.xbhog.service;
import com.xbhog.Mapper.CityMapper;
import com.xbhog.pojo.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CityService {
@Autowired
CityMapper cityMapper;
public City getCityId(Long id){
return cityMapper.getCityId(id);
}
}
使用Service注解声明,并将该类加入到容器中,方便后面调用,在service层调用Mapper层的方法。
创建Controller:
import com.xbhog.pojo.City;
import com.xbhog.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class Mycontro {
@Autowired
CityService cityService;
@ResponseBody
@GetMapping("/city")
public City getCity(@RequestParam("id") Long id){
return cityService.getCityId(id);
}
}
增加数据库信息:

测试:

mybatis混合版:
我们在CItyMapper中添加一个方法:
package com.xbhog.Mapper;
import com.xbhog.pojo.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface CityMapper {
@Select("select * from city where id = #{id}")
public City getCityId(Long id);
public void addCity(City city);
}
这个方法我们采用配置文件来绑定。
创建CityMapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xbhog.Mapper.CityMapper">
<insert id="addCity" parameterType="com.xbhog.pojo.City">
insert into city(`name`,`state`,`country`) values (#{name},#{state},#{country});
</insert>
</mapper>
其中命名空间与CityMapper要对应,进行插入操作。
在Service层增加方法:
package com.xbhog.service;
import com.xbhog.Mapper.CityMapper;
import com.xbhog.pojo.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CityService {
@Autowired
CityMapper cityMapper;
public void addCity(City city){
cityMapper.addCity(city);
}
}
在Controller中增加方法:
import com.xbhog.pojo.City;import com.xbhog.service.CityService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;@Controllerpublic class Mycontro { @Autowired CityService cityService; @ResponseBody @PostMapping("/add") public City addcity(City city){ cityService.addCity(city); return city; }}
通过返回的city来查看信息是否正确。
测试:
我们通过PostMan来发送请求信息进行测试。
请求的方式为Post,url就是我们Controller中的add请求。post提交的参数:name、state、country。
会自动封装为City。

从上图可以发现,我们的id是Null,怎么样让添加后的数据返回id呢,在Mybatis中有useGeneratedKeys自增主键,自增主键的名字叫id。
这样添加进去的数据就会将id返回给传入的city中的id。
<insert id="addCity" parameterType="com.xbhog.pojo.City" useGeneratedKeys="true" keyProperty="id"> insert into city(`name`,`state`,`country`) values (#{name},#{state},#{country});</insert>
也可以采用注解的方式实现:
import com.xbhog.pojo.City;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Options;import org.apache.ibatis.annotations.Select;import java.util.List;@Mapperpublic interface CityMapper { @Select("select * from city where id = #{id}") public City getCityId(Long id); @Insert("insert into city(`name`,`state`,`country`) values (#{name},#{state},#{country})") @Options(useGeneratedKeys = true,keyProperty = "id") public void addCity(City city);}
public City addcity(City city){ //city -->id有值了 cityService.addCity(city); return city; }
查看效果:

参考文献:
官网:Github
结束:
如果你看到这里或者正好对你有所帮助,希望能点个关注或者推荐,感谢;
有错误的地方,欢迎在评论指出,作者看到会进行修改。
SpringBoot数据访问之整合mybatis注解版的更多相关文章
- SpringBoot数据访问之整合Mybatis配置文件
环境搭建以及前置知识回顾 SpringBoot中有两种start的形式: 官方:spring-boot-starter-* 第三方:*-spring-boot-starter Mybatis属于第三方 ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
- java框架之SpringBoot(9)-数据访问及整合MyBatis
简介 对于数据访问层,无论是 SQL 还是 NOSQL,SpringBoot 默认采用整合 SpringData 的方式进行统一处理,添加了大量的自动配置,引入了各种 Template.Reposit ...
- Spring Boot数据访问之整合Mybatis
在Mybatis整合Spring - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中谈到了Spring和Mybatis整合需要整合的点在哪些方面,需要将Mybatis中数据库连接池等相关对 ...
- SpringBoot整合MyBatis(注解版)
详情可以参考Mybatis官方文档 http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ (1). ...
- 【串线篇】SpringBoot数据访问【数据源/mybatis/指定映射文件位置】
一.配置数据源 1.1.jdbc版本 JDBC(.tomcat.jdbc.pool.DataSource作为数据源) <?xml version="1.0" encoding ...
- Spring Boot2 系列教程 (十三) | 整合 MyBatis (XML 版)
前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,之前介绍过了 SpringBoot 整合MyBatis 注解版的使用,上一篇介绍过 MyBatis ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- springboot整合mybatis(注解)
springboot整合mybatis(注解) 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> ...
随机推荐
- noip模拟23[联·赛·题]
\(noip模拟23\;solutions\) 怎么说呢??这个考试考得是非常的惨烈,一共拿了70分,为啥呢 因为我第一题和第三题爆零了,然后第二题拿到了70分,还是贪心的分数 第一题和第二题我调了好 ...
- 网络损伤仪WANsim中的乱序功能
乱序 乱序功能需要指定每个帧 发生乱序的概率,以及新的帧的位置相较于原来位置的时间范围. 乱序的概率范围是0%~20%,颗粒度是0.001%.Delay的设置范围为 0s~10s,颗粒度为0.1 ms ...
- 期望长度P1365,CF235B,P1654
期望长度 定义 这里期望长度表示一段序列连续长度的期望.具体来说,对于一段序列,每个点都有一个概率连续和断开.求所有连续序列和的期望. 当然,对于以上期望长度的定义,我们只需要求出每个点存在的期望的和 ...
- 1.4matlab矩阵的表示
1.4matlab矩阵的表示 矩阵的建立 利用直接输入法建立矩阵:将矩阵的元素用中括号括起来,按矩阵的顺序输入各元素,同一行的各元素之间用逗号或空格分隔,不同行的元素之间用分号分隔. 利用已建立好的矩 ...
- SpringBoot自动装配-Import
1. 简介 @Import导入的类会被Spring加载到IOC容器中.而@Import提供4中用法: 导入Bean 导入配置类 导入 ImportSelector 实现类.一般用于加载配置文件中的类 ...
- 关于win7+cenos 7双系统安装
---恢复内容开始--- 1,cenos 0 7制作U盘启动 制作工具 http://pan.baidu.com/s/1nv9lpmp 镜像自备 2,安装centos 7 释放磁盘空间,如:20G.用 ...
- etcd raft 处理流程图系列1-raftexample
最近在看raft相关的代码和实现,发现etcd的raft模块在实现上还是比较灵活的,但缺点就是需要用户实现比较多的功能,如存储和网络等,同时带来的优点就是不会对用户的存储和传输作限制.网上对该模块的描 ...
- OpenGL学习笔记(五)变换
目录 变换 向量 向量的运算 向量与标量运算 向量取反 向量加减 求向量长度 向量的单位化 向量相乘 点乘(Dot Product) 叉乘 矩阵 矩阵的加减 矩阵的数乘 矩阵相乘 矩阵与向量相乘 与单 ...
- 最全总结 JavaScript Array 方法详解
JavaScript Array 指南.png Array API 大全 (公众号: 前端自学社区).png 前言 我们在日常开发中,与接口打交道最多了,前端通过访问后端接口,然后将接口数据二次处理渲 ...
- MSTP
目录 一.生成树存在的问题 二.MSTP 三.MSTP的网络层次 四.MSTP的端口状态 五.MSTP的保护功能 一.生成树存在的问题 STP和RSTP的问题 PVST的问题 二.MSTP 多生成树 ...