Spring Boot 知识笔记(整合Mybatis续-补充增删改查)
续上篇,补充数据库增删改查的其他场景。
一、Mapper中添加其他场景操作
package net.Eleven.demo.Mapper; import net.Eleven.demo.domain.UserNew;
import org.apache.ibatis.annotations.*; import java.util.List; /**
* 功能描述:访问数据库的接口,相当于dao层
* @author Eleven
*/ public interface UserMapper { //推荐使用#{}取值,不要用${},因为存在注入的风险 /**
* 向数据库插入一条数据
* @param userNew
* @return
*/
@Insert("INSERT INTO user(name,phone,create_time,age) VALUES(#{name},#{phone},#{creatTime},#{age})")
@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id") //keyProperty java对象的属性;keyColumn表示数据库的字段
int insert(UserNew userNew); /**
* 查找全部,功能比较简单,也可以直接跳过service层,直接在controller注入mapper
* @return
*/
@Select("select * from user")
@Results({@Result(column = "create-time",property = "createTime")})
List<UserNew> getAllUser(); /**
* 根据id查询,返回的是一个user对象
* @param id
* @return
*/
@Select("select * from user where id=#{id}")
@Results({@Result(column = "create-time",property = "createTime")})
UserNew findById(int id); /**
* 根据id更新对象的name
* @param userNew
*/
@Update("update user set name=#{name} where id=#{id}")
void updateById(UserNew userNew); /**
* 根据id,删除一个对象
*/
@Delete("delete from user where id =#{id}")
void deleteById(int id); }
二、impl文件中增加对应的调用mapper的方法
package net.Eleven.demo.Service.impl; import net.Eleven.demo.Mapper.UserMapper;
import net.Eleven.demo.Service.UserService;
import net.Eleven.demo.domain.UserNew;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import javax.validation.constraints.Null;
import java.util.List; @Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper; @Override
public int add(UserNew userNew) {
userMapper.insert(userNew);
int id = userNew.getId();
return id;
} //查找所有,对应mapper的getAlluser
public Object findAll(){
return userMapper.getAllUser();
} //根据id查找,对应mapper的findById
public Object findById(int id){
return userMapper.findById(id);
} //更新一个对象
public void updateById(UserNew userNew){
userMapper.updateById(userNew);
} //删除一个对象
public void deleteById(int id){
userMapper.deleteById(id);
}
}
三、controller中增加对应的request请求,通过注入,调用impl的方法,继而执行数据库命令。
package net.Eleven.demo.controller; import net.Eleven.demo.Service.UserService;
import net.Eleven.demo.Service.impl.UserServiceImpl;
import net.Eleven.demo.domain.JsonData;
import net.Eleven.demo.domain.UserNew;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController
@RequestMapping("/api/sql/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private UserServiceImpl userServiceImpl;
@GetMapping("add")
public Object userAdd(){
UserNew userNew = new UserNew();
userNew.setAge(11);
userNew.setCreatTime(new Date());
userNew.setName("Eleven");
userNew.setPhone("18211111111");
int id = userService.add(userNew);
return JsonData.buildSuccess(id);
} //查找全部
@GetMapping("find_all")
public Object findAll(){
return JsonData.buildSuccess(userServiceImpl.findAll());
} //根据id查找对象
@GetMapping("find_by_id")
public Object findById(int id){
return JsonData.buildSuccess(userServiceImpl.findById(id));
} //根据id更新对象的name
@GetMapping("update_by_id")
public Object updateById(String name,int id){
UserNew userNew = new UserNew();
userNew.setName(name);
userNew.setId(id);
userServiceImpl.updateById(userNew);
return JsonData.buildSuccess();
} //根据id删除对象
@GetMapping("delete_by_id")
public Object deleteById(int id){
userServiceImpl.deleteById(id); } }
四、执行结果




Spring Boot 知识笔记(整合Mybatis续-补充增删改查)的更多相关文章
- spring boot整合mybatis框架及增删改查(jsp视图)
工具:idea.SQLyog 版本:springboot1.5.9版本.mysql5.1.62 第一步:新建项目 第二步:整合依赖(pom.xml) <dependencies> < ...
- Spring Boot 使用Mybatis注解开发增删改查
使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...
- Mybatis入门之增删改查
Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- MyBatis -- 对表进行增删改查(基于注解的实现)
1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1 首先须要定义映射sql的 ...
- Mybatis实现简单增删改查
Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...
- SpringBoot 源码解析 (九)----- Spring Boot的核心能力 - 整合Mybatis
本篇我们在SpringBoot中整合Mybatis这个orm框架,毕竟分析一下其自动配置的源码,我们先来回顾一下以前Spring中是如何整合Mybatis的,大家可以看看我这篇文章Mybaits 源码 ...
- spring boot 1.4 整合 mybatis druid
http://www.jianshu.com/p/cef49ad91ba9spring boot 1.4 整合 mybatis druid
- SSH(Struts 2.3.31 + Spring 4.1.6 + Hibernate 5.0.12 + Ajax)框架整合实现简单的增删改查(包含分页,Ajax 无刷新验证该用户是否存在)
软件152 余建强 该文将以员工.部门两表带领大家进入SSH的整合教程: 源码下载:http://download.csdn.net/detail/qq_35318576/9877235 SSH 整合 ...
随机推荐
- 局域网Linux机器中病毒简单处理 .aliyun.sh 挖矿病毒 ---不彻底
1. 昨天晚上同事打电话给我说自己的服务器上面的redis无故被清空了,并且查看aof 日志有很多 wget和write指令 一想就是大事不好.局域网中病毒了.. 2. 今天早上到公司忙完一阵简单看了 ...
- [转帖]springboot+k8s+抛弃springcloud.eureka
springboot+k8s+抛弃springcloud.eureka https://www.cnblogs.com/lori/p/12048743.html springboot开发微服务框架一般 ...
- golang学习笔记 ---slice
Go 语言中的slice类型可以理解为是数组array类型的描述符,包含了三个因素: 指向底层数组的指针 slice目前使用到的底层数组的元素个数,即长度 底层数组的最大长度,即容量 因此当我们定义一 ...
- PHP小程序后端支付代码亲测可用
小程序后端支付代码亲测可用 <?php namespace Home\Controller; use Think\Controller; class WechatpayController ex ...
- SQLServer之行数据转换为列数据
准备工作 创建表 use [test1] go create table [dbo].[student]( ,) not null, ) null, ) null, [score] [int] nul ...
- Java自学-I/O 字符流
Java的字符流 Reader Writer Reader字符输入流 Writer字符输出流 专门用于字符的形式读取和写入数据 步骤 1 : 使用字符流读取文件 FileReader 是Reader子 ...
- QPainter绘制图片填充方式(正常大小、剪切大小、自适应大小、平铺)
Qt中QPainter提供了绘制图像的API,极大地方便了我们对图像的绘制. Qt中提供了QPixmap, QBitmap,QBitMapQImage,QPicture等图像绘图设备,它们的类关系如下 ...
- 利用Python读取图片exif敏感信息
众所周知,现在很多的照相机等软件,拍摄会有选项,是否包含位置信息等. 当然有的人会说,我在微信中查看图片exif信息并没有啊,这是因为你发送到微信服务器的时候,微信帮你完成了保密工作. 常见的图片中包 ...
- 【转载】Gradle学习 第一章:引言
转载地址:http://ask.android-studio.org/?/article/7 We would like to introduce Gradle to you, a build sys ...
- 【转】LockSupport深入浅出
原文:https://www.cnblogs.com/qingquanzi/p/8228422.html 本篇是<自己动手写把"锁">系列技术铺垫的最后一个知识点.本篇 ...