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 整合 ...
随机推荐
- 并行 Webclient(一)
在 Stackoverflow 上看到了一个提问,关于并行的 WebClient,觉得回答者的代码很有参考性,下面记录一下,以便日后用到: 提问者: 我有一个功能基本上分为两个子功能. html=Re ...
- MySQL 只能做小项目?松哥要说几句公道话!
松哥上学那会,很多人对 MySQL 有一些偏见,偏见主要集中在以下几方面: MySQL 不支持事务(事实上 MyISAM 有表锁,但是效率比较低) MySQL 存储的数据量比较小,适合小项目,大项目还 ...
- MediaWiki上传文件大小设置
一.概述 MediaWiki默认最大上传文件大小为2M,碰到文件较大需要修改这个限制,需要改为8M. 二.修改php.ini 使用docker运行的MediaWiki,默认是没有php.ini这个文件 ...
- [Silverlight 4] Textbox style模擬Textblock 使可以選取、複製
childwindow 做為訊息視窗,使用textblock,可是textbloc無法選取內容及複製, 就改用textbox假裝成textblock ---原本的textblock <contr ...
- Linux纯小白操作(以安装JDK为例)
[本文只针对纯小白,有基础的请略过] 最近公司给分配工作使用的虚拟机都是Linux系统的,以前完全没接触过,今天按照网上一些教程操作,好多地方感觉对小白不够友好(有些问题非常小白那些教程没有写出来.我 ...
- Python Lab Assignments
引用: https://github.com/saismaran33/Python-Lab-Assignments/wiki/Python-Lab-Assignment-2 Lab 1 对于任何Web ...
- 基础系列(1)-- html
(随笔杂谈,自己做的笔记) 网页的组成 结构 ------ xhtml,xml 表现 ------ css 行为 ------ bom,dom,ECMAScript html5结构 < ...
- Burp Suite渗透实战操作指南-上篇
Burp必备知识 在介绍功能之前有必要让大家了解一些burp的常用功能,以便在使用中更好的发挥麒麟臂的优势. 1.1 快捷键 很多人可能都没用过burp的快捷键吧,位置如下,不说话,如果不顺手可以自 ...
- 【转载】Gradle学习 第十章:网络应用快速入门
转载地址:http://ask.android-studio.org/?/article/8 This chapter is a work in progress.这一章是一项正在进行中的工作. Th ...
- Python3报错:ModuleNotFoundError: No module named '_bz2'
系统信息 系统:CentOS Linux release 7.6.1810 (Core) python版本:python3.7 报错信息 from _bz2 import BZ2Compresso ...