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 整合 ...
随机推荐
- Android studio(AS) svg图片使用
1.下载svg文件,https://www.iconfont.cn/阿里的2.通过AS - New - Vectro Asset转换成安卓可用的xml文件(ic_back.xml)3.配置Gradle ...
- 使用eclipse git插件合并merge部分代码方法
当有一个父项目,它的下面有多个子项目:或者一个项目下边,只想合并部分路径,甚至部分文件的内容,使用下边的方法可以达到目的,特此记录: 1.主项目右键 -> team -> remove f ...
- python调用MySQL数据库
在Python中访问mysql数据库中的数据需要三步骤: 1,建立连接 2,操作数据库 3,连接关闭
- PHP 垃圾回收机制详解
前言:之前对PHP的GC只是了解了个大概,这次详细了解下PHP的垃圾回收机制(GC). 介于网上大部分都是PHP5.X的GC,虽然 php5 到 php7 GC部分做出的改动较小,但我觉得还是一起写下 ...
- net core 记录自定义端口多个方式
1.直接修改 . 2.代码定义 public class Program { public static void Main(string[] args) { CreateWebHostBuilder ...
- Lambda表达式的用法
参考:https://www.cnblogs.com/knowledgesea/p/3163725.html
- WebService--导出excel并将excel发送到邮箱
1.利用NPOI生成EXCEL 2.导出excel之后发送邮件 //导出excel //path :保存路径 //dt:要导出的数据 public static bool DataTableToExc ...
- .NET创建Windows定时任务
创建Windows定时任务教程 1.创建一个控制台应用程序,保证程序正常运行. 2.右键点击我的电脑->点击管理. 3.在计算机管理弹出框->展开计算机管理(本地)->展开系统工具- ...
- JSON,全称:JavaScript Object Notation,作为一个常见的轻量级的数据交换格
JSON,全称:JavaScript Object Notation,作为一个常见的轻量级的数据交换格式,应该在一个程序员的开发生涯中是常接触的.简洁和清晰的层次结构使得 JSON 成为理想的数据交换 ...
- C#7语法快速参考-第一章 Hello World
选择IDE 要开始使用C#编程,您需要一个支持微软.NET框架的集成开发环境(IDE).最受欢迎的选择是微软自己的Visual Studio.初学可以使用Visual Studio Community ...