Springboot第五篇:结合myBatis进行SQL操作
前提:和之前同样的,本篇会从前端和后台一起讲述关于SQL的select操作(其他操作原理大致类似,不多做解释了)。
大致流程:前端通过AJAX将数据发送到后台的路由,后台路由会根据发送的数据进行SQL操作,并返回对应数据。
1:DB的table表建立
我们这边只建立一个简单的table表,建表的语句大致如下(本地为mysql):
CREATE TABLE `user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
)
建立好table以后,我们插入一条记录,执行语句:INSERT INTO `user_info`(`id`,`name`)VALUES(1,‘zhuyt');
2:前端fetch
fetch与剧中的url需要根据自己的后台设置,对应后台执行的是select操作,代码如下:
fetch("http://localhost:8080/index/userInfo",{
method:'post', // or 'PUT'
headers:{
"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
},
body:"id=1", // data can be `string` or {object}!
}).then(res => res.json()).then(data => {
console.log(data);
}).catch(error => console.error('Error:', error));
3:后台
首先关于pom,我们要加入两个依赖模块,第一个模块是关于mybatis与mysql的,第二个模块是关于JSON格式相关的内容:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
关于controller模块,代码如下:
package maven.example.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import maven.example.service.UserService;
import net.sf.json.JSONArray; @RestController
@RequestMapping(value = "/index")
public class IndexController { @Autowired
private UserService userService; @RequestMapping("/userInfo")
public JSONArray userInfo(@RequestParam("id") String id) {
return userService.getUserInfo(Integer.valueOf(id));
}
}
关于service模块,代码如下:
package maven.example.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import maven.example.entity.UserEntity;
import maven.example.mapper.UserMapper;
import net.sf.json.JSONArray; @Service
public class UserService { @Autowired
private UserMapper userMapper; public JSONArray getUserInfo(Integer id){
List<UserEntity>li = userMapper.getUserInfo(id);
JSONArray listArray = JSONArray.fromObject(li);
return listArray;
}
}
关于UserMapper接口模块,代码如下:
package maven.example.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select; import maven.example.entity.UserEntity; @Mapper
public interface UserMapper { @Select("SELECT * FROM USER_INFO WHERE ID=#{id}")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name")
})
List<UserEntity>getUserInfo(@Param("id") Integer id);
}
关于model模块,代码如下:
package maven.example.entity;
public class UserEntity {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
完整的后台代码可以去以下地址拉取,https://github.com/nnnnnjjjjj/springboot.git
4:执行
我们打开浏览器,输入对应的地址,并点击F12键,可以看到确实打印出了DB查询到的数据。

Springboot第五篇:结合myBatis进行SQL操作的更多相关文章
- SpringBoot第五篇:整合Mybatis
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10869315.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 ORM框架 ...
- SpringBoot(五)Springmvc+Mybatis+mysql
为了整合这个花了,好长时间,因为上几个连在一起,在pom文件中有没注释的配置,导致我找这个问题找了好久.下面开始 1,先new project 时选择spring initializr,新建好一个空项 ...
- 第五篇、常用的SQL语句和函数介绍
简介: 在使用到sqlite3的时候,常常需要写一些SQL语句,现将常用到的部分语句稍微总结以下,由于个人习惯,关键字用大写. 附: /*简单约束*/ CREATE TABLE IF NOT EXIS ...
- Mybatis第五篇【Mybatis与Spring整合】
Mybatis与Spring整合 既然我们已经学了Mybatis的基本开发了,接下来就是Mybatis与Spring的整合了! 以下使用的是Oracle数据库来进行测试 导入jar包 aopallia ...
- mybatis入门篇:mybatis动态SQL
1.if用法 <select id="selectUser" resultType="com.forest.owl.entity.User"> se ...
- Mybatis相关SQL操作总结
1.resultMap和resultType等参数以及结果集 <select id="getApplicationByRoleCode" resultType="p ...
- SpringBoot第五集:整合Druid和MyBatis(2020最新最易懂)
SpringBoot第五集:整合Druid和MyBatis(2020最新最易懂) 1.SpringBoot整合Druid Druid是阿里巴巴的一个开源项目,是一个数据库连接池的实现,结合了C3P0. ...
- 第五篇 SQL Server安全架构和安全
本篇文章是SQL Server安全系列的第五篇,详细内容请参考原文. 架构本质上是一个数据库对象,其他对象的一个容器,在复杂的数据库中它能够很容易的管理各组对象.架构具有重要的安全功能.在这一篇你会学 ...
- 第五篇 SQL Server代理理解代理错误日志
本篇文章是SQL Server代理系列的第五篇,详细内容请参考原文. 正如这一系列的前几篇所述,SQL Server代理作业是由一系列的作业步骤组成,每个步骤由一个独立的类型去执行.在第四篇中我们看到 ...
随机推荐
- null与not null
.not null CREATE TABLE `test` ( `col1` VARCHAR( ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NUL ...
- Mybatis的map用法
当Mybatis传过来的值是map类型的时候,有两种处理方法 1.将数值装入类封装起来 public interface IStudentDao { // 根据姓名和年龄查询 List<Stud ...
- 疯狂JAVA讲义---第十五章:输入输出(上)流的处理和文件
在Java中,把这些不同类型的输入.输出抽象为流(Stream),而其中输入或输出的数据称为数据流(Data Stream),用统一的接口来表示,从而使程序设计简单明了. 首先我要声明下:所谓的输入输 ...
- (轉)CSS 单行溢出文本显示省略号...的方法(兼容IE FF)
轉自:http://www.cnblogs.com/hlz789456123/archive/2009/02/18/1392972.html html代码:<div><p>&l ...
- Sourcetree拉取推送问题
以下sourcetree使用时出现的问题:可以暂时用删除passwd文件解决 拉取时账号密码输入错误 已经有一个远程仓库了,但是需要拉取另一个远程仓库时,提示URL 进入 C:\Users\用户名\A ...
- 后台web请求代码(含https,json提交)
后台web请求 namespace XXXX.Utilites { using System; using System.Collections.Generic; using System.IO; u ...
- windows过滤指定IP
通过windows的安全管理策略工具我们可以实现对IP的过滤.整个过程比较复杂.我们以图形演示. 下面我们以windows 8.1作为示例. 1.控制面板=>管理工具=>本地安全策略. 2 ...
- Buffer Pool--内存总结1
物理地址空间是处理器用来访问位于总线上的所有部件的集合.在32位处理器上,地址总线为32位,寻址空间为4GB.在使用PAE的32位服务器上,地址总线为36位,寻址空间为64GB.在64位的处理器上生产 ...
- Tempdb--monitoring and troubleshooting
TempDB用来存放临时表.全局临时表.表变量以及排序或HASH等操作引发的中间结果集 TempDB在每次实例重启时重新创建,TempDB数据库文件的初始化大小取决于Model数据库的文件大小或显示A ...
- 转载WPF:创建你的第一个WPF项目
转载:http://www.cnblogs.com/pengjinyu/archive/2009/08/19/1549845.html