【视频&交流平台】
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
相关推荐
- [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]...
提供一致的Web UI用于生成兼容mybatis-plus框架的相关功能代码,包括Entity,Mapper,Mapper.xml,Service,Controller等,可以自定义模板以及各种输出参数,也可以通过SQL查询语句直接生成代码。 使用方法 ♡maven...
spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web...
春天启动启动器mybatis 这是MyBatis的非官方Spring Boot Starter 与官方相反,它遵循其他Spring Boot数据存储库的约定并实现适当...spring.mybatis.mapperLocations =classpath*:mapper/**/*.xml # mappers file spri
spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-...
最简单的springboot + mybatis +mapper.xml 配置项目 简单配置java spring boot项目
pdf介绍spring-boot-mybatis-plus数据层框架。 Spring Boot中使用MyBatis-Plus作为数据层框架,可以极大提高开发效率和简化数据操作的复杂性。 多数据源支持:MyBatis-Plus支持配置多数据源,这对于需要处理多个...
# Spring Boot 集成 MyBatis, 分页插件 PageHelper, 通用 Mapper ## 项目依赖 ```xml <!--mybatis--> <groupId>org.mybatis.spring.boot <artifactId>mybatis-spring-boot-starter <version>1.1.1 <!...
mybatis在持久层框架中还是比较火的,一般...虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活。但正其操作都要通过SQL语句进行,就必须写大量的xml文件,很是麻烦。mybatis-plus就很好的解决了这个问题。
mybatis_plus插件,idea快速从mapper.java文件进入mapper.xml文件,通用版。使用起来开发更快捷,mybatis一键跳转工具,安装可参考“我的博客->idea开发相关->idea快速从mapper.java跳转到mapper.xml文件的插件”。
Spring Boot MyBatis 版本 基于Spring Boot 1.3.0.M5 介绍 由于默认情况下在Spring Boot项目中不支持MyBatis而创建。 只需添加依赖项,即可在Spring Boot中支持使用MyBatis 已实现,因此无需现有的xml配置即可扩展...
spring boot + spring + mybatis 集合xmlspring boot + spring + mybatis 集合xmlspring boot + spring + mybatis 集合xml
Spring Boot整合MyBatis-Plus,使用MySQL,基于xml开发及注解开发
- Spring Boot - Spring MVC - Spring Data - Spring Security - Spring Cloud 4. Web开发: - HTML、CSS、JavaScript - HTTP协议 - Servlet、JSP - AJAX、JSON、XML 5. 框架和工具: - MyBatis - ...
mybatis作为一款轻量级的持久层框架实现了比较简单的操作数据库的能力,但是它是一个半ORM(对象关系映射)的持久层框架,因为它需要我们在XML文件中写SQL语句,不能完全专注于业务逻辑,即是它后来做了一些改进,有了...
- 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 - Spring MVC - Spring Data - Spring Security - Spring Cloud 4. Web开发: - HTML、CSS、JavaScript - HTTP协议 - Servlet、JSP - AJAX、JSON、XML 5. 框架和工具: - MyBatis - ...
- 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的音乐网站后端设计。项目包含52个文件,其中包括30个Java源文件、4个属性文件、3个Gitignore文件、3个JAR包、3个命令文件、3个XML文件、3个YAML文件、1个开发环境...
Maven依赖:【***.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【***.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【***-sources.jar下载地址(官方地址+国内镜像地址).txt】...