148. Spring Boot MyBatis Upgrade - XML - Addition, Delete, Modify and Check

 

【Video & Communication Platform】

à SpringBoot Video 

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

à  SpringCloud video

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

à Spring Boot source code 

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

à Spring Boot communication platform 

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

 

 

Origin of need :

       In the previous article, we introduced how to perform insert operations. In practice, we also have query, modification, and deletion operations, which are often used in projects. Let's take a look at the outline of this section:

Outline of this section :

(1) update: modification operation
(2) delete: query operation
(3) select all: query all
(4) select by id: query operation according to id
(5) update off-topic
(6) code

 

       Next, look at the specific content:

( 1 ) update : modification operation

       Code on the basis of the original code, and add in Demo.xml :

 

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

 

       Add the following code to DemoMapper :

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

 

       Note: The int value is returned here . This is the number of successful modifications returned by the value. For example, if 5 pieces of data are successfully found and the modification is successful, then the return value is 5 .

 

( 2 ) delete : query operation

       Add to the Demo.xml file:     

<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

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944550&siteId=291194637