续上篇,补充数据库增删改查的其他场景。

一、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续-补充增删改查)的更多相关文章

  1. spring boot整合mybatis框架及增删改查(jsp视图)

    工具:idea.SQLyog 版本:springboot1.5.9版本.mysql5.1.62 第一步:新建项目 第二步:整合依赖(pom.xml) <dependencies> < ...

  2. Spring Boot 使用Mybatis注解开发增删改查

    使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...

  3. Mybatis入门之增删改查

    Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...

  4. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  5. MyBatis -- 对表进行增删改查(基于注解的实现)

    1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1  首先须要定义映射sql的 ...

  6. Mybatis实现简单增删改查

    Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...

  7. SpringBoot 源码解析 (九)----- Spring Boot的核心能力 - 整合Mybatis

    本篇我们在SpringBoot中整合Mybatis这个orm框架,毕竟分析一下其自动配置的源码,我们先来回顾一下以前Spring中是如何整合Mybatis的,大家可以看看我这篇文章Mybaits 源码 ...

  8. spring boot 1.4 整合 mybatis druid

    http://www.jianshu.com/p/cef49ad91ba9spring boot 1.4 整合 mybatis druid

  9. 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 整合 ...

随机推荐

  1. AtomicInteger例子

    AtomicInteger可以保证原子性,可见性,有序性 public class AtomicIntegerTest { private static AtomicInteger value = n ...

  2. Rider中Winform开发支持预览(5)

    1.Rider .netCore3.0 winform设计器支持预览,这点vs目前还不支持. 2.不过winform下控件选择工具栏都是没有图标的

  3. Nginx php上传文件大小的设置

  4. R语言dai xie

    R语言,Python长期招代写,作业量充足,需要一定英文能力,价格满意.有意者请留言联系,谢谢

  5. react的标记渲染机制

    // ReactUpdates.js  - enqueueUpdate(component) function dirtyComponents.push(component); https://jue ...

  6. WPF MainWindow的TopMost,Resizemode

    Topmost -[true,false] The default is false, but if set to true, your Window will stay on top of othe ...

  7. TCP/UDP协议(二)

    面试问题:Tcp/Udp协议是什么,各有什么异同点,各自的使用场景? Tcp协议(传输控制协议) tcp是面向连接的协议,在收发数据之前,必须与对方建立可靠的连接: 三次握手:简单形象通俗描述: 主机 ...

  8. jq处理动画累加

    问题:日程提醒(跟日历一样的切换效果),只用一个div来展示当天日程数据,每次清空div里的数据再加载数据,导致切换日期时,数据展示div有闪动,于是采用动画来进行过渡,这样就巧妙地避免了闪动: $( ...

  9. DP之背包

    一.01背包: (以下均可用一维来写 即只能选择一次的物品装在一定容积的背包中.f[i][j]表示前i件物品在容积为j时的最大价值. for(int i = 1; i <= n ;  i++){ ...

  10. css3+JS实现幻灯片轮播图

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...