MyBatis(2)
@Mapper public interface UserMapper {int UpdateUser(@Param("ID") int userID,@Param("name") String username); }<update id="Update">update user set username=#{name} where userID=#{ID}</update>
1)注意mapper目录是创建在resource文件夹下面的而不是static目录
2)还要注意,你注入一个接口,还是使用@Autowried,然后@Mapper是修饰一个接口的,而不是诸如接口
对于我们向数据库中增加数据来说,程序应该返回两种类型的数据,一种是受影响的行数,一种是我想要拿到添加成功的主键,一种是我添加数据后所影响的行数;
编写代码:以向数据库中添加数据为例,根据在浏览器上面输入的username和password进行向数据库中插入数据
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
public class User {private int ClassID;private int userID;private String username;private String password;
}
第一种:我们想要拿到插入数据后的影响行数,我们还要注意我们在insert标签里面是不用进行设置resultMap和resultType这样的类型的,啥东西也不用设置
1)这是UserController里面的代码:在里面要进行参数校验
package com.example.demo.Controller;import com.example.demo.Service.UserService; import com.example.demo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/InsertAll")@ResponseBodypublic int InsertAll(User user){//我们需要在UserController进行参数校验if(user==null||user.equals("")){return -1;}return userService.InsertAll(user);} }
2)这是UserService里面的代码:
package com.example.demo.Service; import com.example.demo.User; import com.example.demo.UserMapper.SpringBootMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService {@Autowiredprivate SpringBootMapper mapper;public int InsertAll(User user) {return mapper.InsertAll(user);} }
3)这是SpringBootMapper里面的代码:
public int InsertAll(User user); //后面的XML字段属性和User中的字段属性名是一样的,当方法名的参数是一个对象的时候
4)这是xml文件里面的代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.demo.Mapper1 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.UserMapper.SpringBootMapper"><insert id="InsertAll">insert into User values(null,#{classID},#{username},#{password}) 这里面的字段名必须和对象的属性名是相同的</insert> </mapper>
我们在写XML文件的时候,我们是希望在url输入一个地址,里面的querystring就是user对象的值,但是我们在进行插入数据的时候
格式是#{Java代码中类的字段名}(对应着数据库的列名)
第二种:我们希望拿到插入数据之后的自增主键(返回自增id)
我们只需要更改xml文件里面的配置即可
下面我们对这里面添加的东西来做一个说明:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.demo.Mapper1 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.UserMapper.SpringBootMapper"><insert id="InsertAll" useGeneratedKeys="true" keyProperty="userID" keyColumn="userID">insert into User values(null,#{classID},#{username},#{password})</insert> </mapper>
1)useGeneratedKeys:这会令MyBatis使用JDBC的getGeneratedKeys方法来取出由数据库内部生成的主键,像MYSQL和SQL Server这样的数据库管理系统的自动递增字段,默认值是false
2)keyColumn:设置生成键值在数据库表中的列名,自增主键在数据库中的名字叫啥,当我们的数据库字段名和对象中的属性不一样的时候,这个keyColumn是可以不用进行设置的,但是如果说数据库的字段名是id,对象中的属性名叫做UserID,这个时候就会进行自动设置的,自动把id的值赋值给User对象中的ID属性,所以方法的返回值是可以是void,也可以设置为int
3)keyProperties:指定对象中的属性,你返回的主键赋值到对象中的哪一个属性
我们还要注意:
***调用数据库完成添加操作,执行完添加之后会将自增的userID指定到user上面的userID属性上面,因为我们进行添加操作的时候 我们是没有进行指定UserID的,是我们的数据库自己进行添加操作的,我们之前在XML里面进行设置的目的就是将我们进行成功插入之后 程序可以将我们的数据库生成的自增主键赋值到我们的user对象中的userID上面,然后我们再userController层上面就可以user的 userID属性了,如果我们的XML文件不进行设置的话,我们就此时返回的userID就不知道是啥值了1)UserController里面的代码: package com.example.demo.Controller; import com.example.demo.Service.UserService; import com.example.demo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/InsertAll")@ResponseBodypublic int InsertAll(User user){//我们需要在UserController进行参数校验if(user==null||user.equals("")){return -1;}userService.InsertAll(user);return user.getUserID();} } 2)UserService里面的代码: package com.example.demo.Service; import com.example.demo.User; import com.example.demo.UserMapper.SpringBootMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService {@Autowiredprivate SpringBootMapper mapper;public List<User> GetAll() {return mapper.GetAll();}public int InsertAll(User user) {return mapper.InsertAll(user);} } 3)接口里面的代码:public int InsertAll(User user);
回顾单元测试:
1)定义:是指对软件中的最小可测试单元进行检查和验证的过程中就叫做单元测试,在咱们的SpringBoot中是测试某一个方法,可以非常简单直观,快速的测试某一项功能是否正确
单元测试是开发者编写的一小段代码,用于检测被测试代码一个很小的,很明确的代码功能是否执行正确,就是为了证明某段代码的执行是否符合我们的预期
2)单元测试的好处:
2.1)单元测试是不需要进行启动Tomact的,还有就是说如果我们中途修改了代码,打包的时候就会自动的去执行单元测试,单元测试后错误就会被发现,也就是说在我们打包之前所有的单元测试必须通过,否则不能打包成功
2.2)使用单元测试进行测试的时候,可以不污染连接的数据库,也就是说在不对数据库进行任何污染的情况下,测试功能
创建SpringBoot单元测试:test文件夹是在src目录下的
咱们的SpringBoot项目创建的时候会默认单元测试框架spring-boot-test,而这个单元测试框架是依靠另一个著名的测试框架Junit来进行实现的,这个依赖是SpringBoot项目自动添加的,所以说咱们的高版本的SpringBoot已经内置了一些框架,单元测试,JSON格式的处理
1)创建准备条件:测试框架(默认已经添加)添加测试的目录,默认是已经被创建的;
2)在需要进行测试的类左边进行右键双击Generate生成,选择生成单元测试,生成测试类和测试的方法,点击test
Testing library:单元测试框架,选择默认即可
ClassName:生成单元测试的类名
Superclass:表示单元测试的父类,不需要直接设置,成空就可以了
Destination package:生成单元测试的目录
Generate:是否生成前置方法(执行测试方法之前的前一个方法)或者后置方法(不动)
这个前置方法和后置方法可以用来记录一下程序执行时间
Member:测试方法列表
最后点击OK就行了
3)要给当前类加上@SpringBootTest注解,声明当前的类是在SpringBoot容器里面运行的,也就是说咱们要进行测试的类是一个SpringBoot
4)在方法中构建咱们的测试代码:
@Transaction的好处就是执行完我们的单元测试代码之后可以进行回滚操作,就不会进行污染业务数据,不会污染原有的数据库,红色表示单元测试执行失败
package com.example.demo.Controller;import com.example.demo.User; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional;import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional class UserControllerTest {@AutowiredUserController userController;@Testvoid insertAll() {User user=new User();user.setClassID(2);user.setUsername("白雪");user.setPassword("我想吃饭");int len=userController.InsertAll(user);Assertions.assertEquals(len,1);//比较它们是否相等,如果相等,说明单元测试过了,否则是不会过的} }
下面来我们来使用一下单元测试的方式来进行验证一下插入操作返回的时候是主键ID
package com.example.demo.Controller; import com.example.demo.User; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional class UserControllerTest {@AutowiredUserController userController; //需要先注入UserController对象,调用里面的方法@Testvoid insertAll() {User user=new User();user.setClassID(3);user.setUsername("李佳鑫");user.setPassword("生活要战斗");int len= userController.InsertAll(user);Assertions.assertEquals(len,user.getUserID());} }
程序报错---->程序直接爆红
测试不通过--->颜色变成橙色
测试通过---->直接变成蓝色
@Param里面的字段数据要和XML里面的数据要相同,一一对应,可以修改接口中的参数的别名,尽量写@param,尽量写@Param,会出现找不到参数的异常
@Transaction加上这个注解,就可以保证单元测试的数据不会污染到原来的数据
下面我们来实现以下修改操作:我想修改指定人名的密码:
我们此时一定要注意,我们修改的属性和名字和我们的数据库中的名字和Java代码中的类的名字都是不一致的
http://127.0.0.1:8080/update?name=李佳伟&studentPassword=778896
上面是我们在浏览器上面输入的url
一:这是UserController里面的代码:
import java.util.List; @Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/update")@ResponseBodypublic int UpDate(String name,String studentPassword){//我们首先要在Controller层进行参数校验if(name==null||name.equals("")||studentPassword.equals("")||studentPassword==null){return -1;}return userService.UpDate(name,studentPassword);} }
二:这是UserService里面的代码:
package com.example.demo.Service; import com.example.demo.User; import com.example.demo.UserMapper.SpringBootMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService {@Autowiredprivate SpringBootMapper mapper;public int UpDate(String name, String studentPassword) {return mapper.UpDate(name,studentPassword);} }
三:这是XML文件和接口中的代码:
int UpDate(String name, String studentPassword);<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.demo.Mapper1 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.UserMapper.SpringBootMapper"><update id="UpDate">update user set password=#{studentPassword} where username=#{name}</update> </mapper>
我们使用单元测式的方式来进行检测:
package com.example.demo.Controller; import com.example.demo.User; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional class UserControllerTest {@AutowiredUserController userController;@Testvoid upDate() {int len= userController.UpDate("李佳伟","773377");Assertions.assertEquals(len,1);} }
我们来实现一下删除操作:我们设想是通过浏览器传递的UserID来进行删除
1)UserController里面的代码:
package com.example.demo.Controller; import com.example.demo.Service.UserService; import com.example.demo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class UserController {@AutowiredUserService userService;@RequestMapping("/Delete")@ResponseBodypublic int Delete(Integer userID){if(userID==null||userID.equals("")||userID<=0){return -1;}return userService.Delete(userID);} }
2)UserService和Mapper里面的代码:
package com.example.demo.Service; import com.example.demo.User; import com.example.demo.UserMapper.SpringBootMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService {@Autowiredprivate SpringBootMapper mapper;public int Delete(Integer userID) {return mapper.Delete(userID);} }int Delete(Integer userID);
3)我们实现的XML文件的代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.demo.Mapper1 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.UserMapper.SpringBootMapper"><delete id="Delete">delete from user where userID=#{userID}</delete> </mapper>
我们使用单元测试的方式来进行测试我们的删除操作:
package com.example.demo.Controller; import com.example.demo.User; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @Transactional class UserControllerTest {@AutowiredUserController userController;@Testvoid delete() {int len= userController.Delete(1);}
1)在接口中可以写成这样:User run(@Param("userID") int userID);多加上一个注解2)在线演示地址,linux服务器项目地址(在线演示地址,提供用户名和密码),源码地址(githup)(加上注释),(然后重要的技能放前面,学校等不占优势的放在后面)3)在进行查询的时候,一定要加上resultMap或者resultType,否则就会报500这样的错误,二者必选其一,但是update标签只需要设置id就可以
MyBatis(2)相关推荐
- mybatis查询报错:com.mysql.cj.exceptions.DataConversionException: Cannot determine value type from string
mybatis查询报错: com.mysql.cj.exceptions.DataConversionException: Cannot determine value type from strin ...
- MyBatis的插入后获得主键的方式
需求: 使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值. 方法: 在mapper中指定keyProperty属性,示例如下: <insert id=" ...
- mybatis使用注解开发
mybatis使用注解开发 面向接口编程 在之前我们是通过面向对象编程,但是在真正开发的时候我们会选择面向接口编程. 根本原因 : 解耦 , 可拓展 , 提高复用 , 分层开发中 , 上层不用管具体的 ...
- mybatis ResultMap
ResultMap 解决属性名和字段的名称不一致的问题. 查询为null的问题 创建java实体类: public class User {private int id; //idprivate St ...
- mybatis配置文件解析
mybatis配置文件解析 mybatis核心配置文件`mybatis-config.xml文件. mybatis的配置文件包含了会深深影响mybatis行为的设置和属性信息. 能配置的内容: con ...
- mybatis CRUD操作
mybatis CRUD操作 select select标签是mybatis最常用的标签之一. select语句有很多属性可以详细的配置每一天sql语句. id 命名空间唯一的标识. 接口中的方法名与 ...
- java mybatis基础
java mybatis基础 1.1 什么是mybatis? mybatis是一个优秀的持久层框架. 避免几乎所有的JDBC代码和手动设置参数以及获取结果集的过程. 可以使用简单的xml或者注解来配置 ...
- mybatis的资源过滤错误及xml文件编码错误
mybatis 解决maven项目内资源过滤的问题 写的配置文件无法被导出或者生效的问题. 解决方案: <build><resources><resource>&l ...
- Mybatis传递多个参数的4种方式
现在大多项目都是使用Mybatis了,但也有些公司使用Hibernate.使用Mybatis最大的特性就是sql需要自己写,而写sql就需要传递多个参数.面对各种复杂的业务场景,传递参数也是一种学问. ...
- SpringBoot (五) :SpringBoot整合mybatis
说在前面 mybatis刚开始使用的时候比较麻烦,需要各种配置文件.实体类.dao层映射关联.还有一大推其它配置.初期开发了generator可以根据表结果自动生产实体类.配置文件和dao层代码,可以 ...
最新文章
- AI技术必备资源汇总
- 学python心得体会1000字-学习python cgi的一点心得体会
- json字符串中的大括号转义传到后台_json转义问题
- Python实现switch效果
- 操作篇 isis协议实验
- (pytorch-深度学习系列)pytorch实现对Fashion-MNIST数据集进行图像分类
- 小学认识计算机硬件ppt,认识计算机硬件课件.ppt
- gbdt 回归 特征重要性 排序_gbdt、xgb、lgb、cat面经整理——from牛客
- 我们为什么活得这么累
- 客户端请求时间和服务端处理时间不匹配问题
- python的取负运算_python 负数取模运算实例
- java开发大全、系列文章、精品教程
- Linux一些最基本命令
- word文字铺满页面_word如何把行铺满页面 怎样使word文档铺满页面
- python 列表索引位置_python列表索引查找
- 【Unity3D开发小游戏】《超级马里奥游戏》Unity开发教程
- [每周心学]示弟立志说(附译文)
- C++面向对象(1):防卫式声明
- 小程序input组件type=“nickname“获取不到微信昵称的值,解决方法
- 堡塔APP 免费使用教程【图文教程】
热门文章
- Android Wi-Fi 修改国家码(QCOM平台)
- 科学计数法在计算机上怎么表示,科学计数法怎么表示
- ZYNQ7000 FSBL加载启动代码详解
- 3000通电话培养出一个性格分析专家,阿里“柔军”是这样养成的
- TypeScript中的?.是什么意思
- 减肥期间早餐或者晚餐吃馒头包子可以吗?
- Redis-入门学习笔记
- java类中获取tomcat下的webap路径方法
- 电脑上不去网的6点原因
- 原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想)...