`

148. Spring Boot MyBatis升级篇-XML-增删改查

阅读更多

 

【视频&交流平台】

à SpringBoot视频

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à SpringCloud视频

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot源码

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

 

需求缘起

       在前面文章中我们介绍了如何进行插入操作,在实际中,我们还有查询、修改、删除操作,这些在项目当中都是经常会用到的。我们看下下本节大纲:

本节大纲

(1)update:修改操作
(2)delete:查询操作
(3)select all: 查询全部
(4)select by id:根据id查询操作
(5)update题外话
(6)代码

 

       接下来看下具体的内容:

1update:修改操作

       在原先的代码基础上进行编码,在Demo.xml中加入:

 

<update id="update">
       update demo set name = #{name} where id =  #{id}
</update>
   

 

       DemoMapper中加入如下代码:

public int update(@Param("id")int id,@Param("name")String name);

 

       注意:在这里返回了int值,这个是值返回成功修改的数量,比如:成功找到了5条数据,并且修改成功了,那么返回值就是5

 

2delete:查询操作

       Demo.xml文件中加入:     

<delete id="delete">
       delete from demo where id = #{id}
    </delete>

 

DemoMapper中加入:

public int delete(int id);

 

       注意:返回值代表成功删除了多少条数据。

 

3select all: 查询全部

       Demo.xml中加入:

   <resultMap id="baseResultMap" type="com.kfit.demo.bean.Demo" >
        <id property="id" column="id" />
        <result property="name" column="name" />
</resultMap
    <select id="selectAll" resultMap="baseResultMap">
        select *from demo
</select>

 

 

 

DemoMapper类中加入:

    public List<Demo> selectAll();

 

       使用@Select注明这是select语句。

 

4select by id:根据id查询操作

       Demo.xml文件中加入:

    <select id="selectById" resultMap="baseResultMap">
        select *from demo where id = #{id}
</select>

 

 

 

DemoMapper类中加入:

    public Demo selectById(int id);

 

 

5update题外话

DemoMapper中,update是指定了多个参数,但是实际中一个实体类中会有很多的字段,这种方式,总感觉不是很好,那么有更好的方式呢,有的,如下的写法:

原先写法:

public int update(@Param("id")int id,@Param("name")String name);

 

对象写法:

public int update(Demo demo);

 

修改完之后瞬间清爽了很多,但是原先的写法也是需要会的,使用场景,比如:我们要修改状态status的话,那么使用updateStatus(int id,int status)的方式,直接指定会更好。

 

6)代码

Demo.xml配置文件代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kfit.demo.mapper.DemoMapper">
   
    <resultMap id="baseResultMap" type="com.kfit.demo.bean.Demo" >
        <id property="id" column="id" />
        <result property="name" column="name" />
    </resultMap>
   
   
    <!--  insert 语句. -->
    <insert id="save" parameterType="com.kfit.demo.bean.Demo" useGeneratedKeys="true" keyProperty="id">
       insert into demo (name) values (#{name})
    </insert>
   
    <update id="update">
       update demo set name = #{name} where id =  #{id}
    </update>
   
    <delete id="delete">
       delete from demo where id = #{id}
    </delete>
   
   
    <select id="selectAll" resultMap="baseResultMap">
        select *from demo
    </select>
   
    <select id="selectById" resultMap="baseResultMap">
        select *from demo where id = #{id}
    </select>
</mapper>

 

 

       DemoMapp.java代码如下:

package com.kfit.demo.mapper;
 
import java.util.List;
 
import org.apache.ibatis.annotations.Param;
 
import com.kfit.demo.bean.Demo;
 
public interface DemoMapper {
   
    public int save(Demo demo);
   
    public int update(@Param("id")int id,@Param("name")String name);
   
    public int delete(int id);
   
    public List<Demo> selectAll();
   
    public Demo selectById(int id);
}

 

 

 

视频+交流平台

à Spring Boot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

 

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

 

分享到:
评论

相关推荐

    spring-boot2.0全新教程实例20例.zip

    - [spring-boot-jpa-thymeleaf-curd](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa-thymeleaf-curd):Spring Boot + Jpa + Thymeleaf 增删改查示例 - [spring-boot-rabbitmq]...

    mybatis-plus-generator-ui:对mybatis-plus-generator进行封装,通过Web UI快速生成兼容的Spring boot,mybatis-plus框架的各类业务代码

    提供一致的Web UI用于生成兼容mybatis-plus框架的相关功能代码,包括Entity,Mapper,Mapper.xml,Service,Controller等,可以自定义模板以及各种输出参数,也可以通过SQL查询语句直接生成代码。 使用方法 ♡maven...

    Spring boot 示例 官方 Demo

    spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web...

    spring-boot-starter-mybatis:Spring Boot MyBatis入门

    春天启动启动器mybatis 这是MyBatis的非官方Spring Boot Starter 与官方相反,它遵循其他Spring Boot数据存储库的约定并实现适当...spring.mybatis.mapperLocations =classpath*:mapper/**/*.xml # mappers file spri

    Spring Boot Examples

    spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-...

    spring boot + mybatis +mapper.xml 项目

    最简单的springboot + mybatis +mapper.xml 配置项目 简单配置java spring boot项目

    spring-boot-mybatis-plus数据层框架

    pdf介绍spring-boot-mybatis-plus数据层框架。 Spring Boot中使用MyBatis-Plus作为数据层框架,可以极大提高开发效率和简化数据操作的复杂性。 多数据源支持:MyBatis-Plus支持配置多数据源,这对于需要处理多个...

    spring-boot-集成mybatis带分页page

    # Spring Boot 集成 MyBatis, 分页插件 PageHelper, 通用 Mapper ## 项目依赖 ```xml &lt;!--mybatis--&gt; &lt;groupId&gt;org.mybatis.spring.boot &lt;artifactId&gt;mybatis-spring-boot-starter &lt;version&gt;1.1.1 &lt;!...

    spring-boot-mybatis-push-01.rar

    mybatis在持久层框架中还是比较火的,一般...虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活。但正其操作都要通过SQL语句进行,就必须写大量的xml文件,很是麻烦。mybatis-plus就很好的解决了这个问题。

    mybatis_plus-17版本.zip

    mybatis_plus插件,idea快速从mapper.java文件进入mapper.xml文件,通用版。使用起来开发更快捷,mybatis一键跳转工具,安装可参考“我的博客-&gt;idea开发相关-&gt;idea快速从mapper.java跳转到mapper.xml文件的插件”。

    spring-boot-mybatis

    Spring Boot MyBatis 版本 基于Spring Boot 1.3.0.M5 介绍 由于默认情况下在Spring Boot项目中不支持MyBatis而创建。 只需添加依赖项,即可在Spring Boot中支持使用MyBatis 已实现,因此无需现有的xml配置即可扩展...

    spring boot + spring + mybatis 集合xml

    spring boot + spring + mybatis 集合xmlspring boot + spring + mybatis 集合xmlspring boot + spring + mybatis 集合xml

    Spring Boot MyBatis-Plus

    Spring Boot整合MyBatis-Plus,使用MySQL,基于xml开发及注解开发

    202307-MyBatis面试题(2023最新版)思维导图.zip

    - Spring Boot - Spring MVC - Spring Data - Spring Security - Spring Cloud 4. Web开发: - HTML、CSS、JavaScript - HTTP协议 - Servlet、JSP - AJAX、JSON、XML 5. 框架和工具: - MyBatis - ...

    Spring Boot 教程 - MyBatis-Plus.docx

    mybatis作为一款轻量级的持久层框架实现了比较简单的操作数据库的能力,但是它是一个半ORM(对象关系映射)的持久层框架,因为它需要我们在XML文件中写SQL语句,不能完全专注于业务逻辑,即是它后来做了一些改进,有了...

    202318-Spring Cloud面试题(2023最新版)思维导图.zip

    - Spring Boot - Spring MVC - Spring Data - Spring Security - Spring Cloud 4. Web开发: - HTML、CSS、JavaScript - HTTP协议 - Servlet、JSP - AJAX、JSON、XML 5. 框架和工具: - MyBatis - ...

    202306-Spring面试题((2023最新版)思维导图.zip

    - Spring Boot - Spring MVC - Spring Data - Spring Security - Spring Cloud 4. Web开发: - HTML、CSS、JavaScript - HTTP协议 - Servlet、JSP - AJAX、JSON、XML 5. 框架和工具: - MyBatis - ...

    202305-Spring MVC面试题(2023最新版)思维导图.zip

    - Spring Boot - Spring MVC - Spring Data - Spring Security - Spring Cloud 4. Web开发: - HTML、CSS、JavaScript - HTTP协议 - Servlet、JSP - AJAX、JSON、XML 5. 框架和工具: - MyBatis - ...

    基于Spring Boot和Mybatis-Plus的音乐网站后端设计源码

    本源码提供了一个基于Spring Boot和Mybatis-Plus的音乐网站后端设计。项目包含52个文件,其中包括30个Java源文件、4个属性文件、3个Gitignore文件、3个JAR包、3个命令文件、3个XML文件、3个YAML文件、1个开发环境...

    mybatis-spring-boot-autoconfigure-2.1.1.jar中文-英文对照文档.zip

    Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【***.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【***-sources.jar下载地址(官方地址+国内镜像地址).txt】...

Global site tag (gtag.js) - Google Analytics