【视频 & 交流平台】
http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
https://gitee.com/happyangellxq520/spring-boot
http://412887952-qq-com.iteye.com/blog/2321532
需求缘起:
在前面文章中我们介绍了如何进行插入操作,在实际中,我们还有查询、修改、删除操作,这些在项目当中都是经常会用到的。我们看下下本节大纲:
本节大纲:
(2)delete:查询操作
(3)select all: 查询全部
(4)select by id:根据id查询操作
(5)update题外话
(6)代码
接下来看下具体的内容:
(1)update:修改操作
在原先的代码基础上进行编码,在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。
(2)delete:查询操作
在Demo.xml文件中加入:
<delete id="delete"> delete from demo where id = #{id} </delete>
在DemoMapper中加入:
public int delete(int id);
注意:返回值代表成功删除了多少条数据。
(3)select 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语句。
(4)select by id:根据id查询操作
在Demo.xml文件中加入:
<select id="selectById" resultMap="baseResultMap"> select *from demo where id = #{id} </select>
在DemoMapper类中加入:
public Demo selectById(int id);
(5)update题外话
在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); }
视频+交流平台:
http://study.163.com/course/introduction.htm?courseId=1004329008
http://412887952-qq-com.iteye.com/blog/2321532