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 整合 ...
随机推荐
- 一次kuberneets evicted的历险
一.概述 kubernetes 的eviction检测diskpresure,检测的是kubelet的root-dir.kubelet的默认root-dir是/var/lib/kubelet,可以使用 ...
- 提高性能,MySQL 读写分离环境搭建
这是松哥之前一个零散的笔记,整理出来分享给大伙! MySQL 读写分离在互联网项目中应该算是一个非常常见的需求了.受困于 Linux 和 MySQL 版本问题,很多人经常会搭建失败,今天松哥就给大伙举 ...
- HTML--元素居中各种处理方法2
紧接上一篇. 如果要居中的是一个块元素呢. 1)如果你知道这个块元素的高度: <main> <div> I'm a block-level element with a fix ...
- vue引入bootstrap、jquery
在进行vue的学习,项目中需要引入bootstrap.jquery的步骤. 一.引入jQuery 在当前项目的目录下(就是package.json),运行命令 cnpm install jquery ...
- Window与Document
Window 表示一个包含DOM文档的窗口,其 document 属性指向窗口中载入的 DOM文档.使用 document.defaultView 属性可以获取指定文档所在窗口.window作为全局变 ...
- jquery-ajax请求.NET MVC 后台
在ajax的URL中写上"/你的控制器名/你方法名" 在后台控制器中对应有两个常用类型一个是ActionResult还有一个是JsonResult 在访问时需要在类型上加上publ ...
- jdk8 接口的变化
在jdk8之前,interface之中可以定义变量和方法,变量必须是public.static.final的,方法必须是public.abstract的.由于这些修饰符都是默认的以下写法等价 publ ...
- delegate里的Invoke和BeginInvoke
Invoke和BeginInvoke都是调用委托实体的方法,前者是同步调用,即它运行在主线程上,当Invode处理时间长时,会出现阻塞的情况,而BeginInvod是异步操作,它会从新开启一个线程,所 ...
- 1.Shell特殊位置变量
$0 文件名及路径 $1,$2 参数1,参数2 , 也可以用${1} 和 ${2} 来表示 $# 传递给脚本或函数的参数个数 $$ 当前Shell进程ID $? 判断上 ...
- #pragma once和#ifndef
C语言中的头文件只是简单的复制粘贴. C语言中变量.函数.结构体的定义和声明两个过程是分离的.声明通常放在头文件中,为了防止重复声明,需要保证头文件中的内容在构建obj文件时只被包含一次.这可以通过# ...