# 逆向工程测试 **Repository Path**: future-one/reverse-engineering-test ## Basic Information - **Project Name**: 逆向工程测试 - **Description**: 用于测试逆向工程 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-05-12 - **Last Updated**: 2023-05-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 一、还原配置步骤 1. 从git把工程代码拉下来 2. 配置maven 3. 下来lombok插件 # 二、接口的说明 | 返回类型 | 方法 | 功能说明 | | :------- | :----------------------------------------------------------- | :------------------------------- | | int | countByExample(UsersExample example); | 按条件计数 | | int | deleteByExample(UsersExample example); | 按条件删除 | | int | deleteByPrimaryKey(Long uId); | 按主键删除 | | int | insert(Users record); | 插入数据,所有字段都会执行 | | int | insertSelective(Users record); | 插入数据,只执行有数据的字段 | | User | selectByExample(UsersExample example); | 按条件查询 | | 对象 | selectByPrimaryKey(Long uId); | 按主键查询 | | list | updateByExampleSelective(@Param("record") Users record, @Param("example") UsersExample example); | 按条件更新表的某些属性而不是所有 | | int | updateByExample(@Param("record") Users record, @Param("example") UsersExample example); | 按条件更新所有的属性 | | int | updateByPrimaryKeySelective(Users record); | 按主键更新值不为null的字段 | | int | updateByPrimaryKeySelective(Users record); | 根据id更新,会判断字段为空 | | int | updateByPrimaryKey(Users record); | 更具id更新,不会判断字段 | ![img](https://www.shijiayi.top/attachment/20211027/98e097e46bcf42729741d6494f8fba07.png) # 三、测试代码 ```sql /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.5.54 : Database - mybatis ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `mybatis`; /*Table structure for table `money` */ DROP TABLE IF EXISTS `money`; CREATE TABLE `money` ( `m_id` BIGINT(18) NOT NULL AUTO_INCREMENT COMMENT '消费记录id', `m_type` VARCHAR(50) NOT NULL DEFAULT '人民币' COMMENT '结算类型', `m_number` INT(11) NOT NULL DEFAULT '0' COMMENT '拥有金额', `m_user` BIGINT(11) NOT NULL COMMENT '所属用户', `m_payment_method` INT(1) NOT NULL DEFAULT '1' COMMENT '1支付宝 2微信 3网银 4其它', PRIMARY KEY (`m_id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='消费记录'; /*Table structure for table `users` */ DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `u_id` BIGINT(18) NOT NULL AUTO_INCREMENT COMMENT '用户表id', `u_name` VARCHAR(300) NOT NULL COMMENT '用户名', `u_pass` VARCHAR(300) NOT NULL COMMENT '密码', `u_idcard` CHAR(18) NOT NULL COMMENT '身份证', `u_sex` CHAR(1) NOT NULL DEFAULT '1' COMMENT '性别 1男 0女', `u_email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱', `u_create_time` DATETIME NOT NULL COMMENT '创建时间', `u_valid` tinyint(1) NOT NULL DEFAULT '1' COMMENT '有效列', PRIMARY KEY (`u_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; ``` ## 3.1 测试接口 ```java package com.xxgc.mybatis; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import UsersMapper; import Users; import UsersExample; import MyBatisUtils; import jdk.nashorn.internal.runtime.logging.Logger; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.ArrayList; import java.util.Date; import java.util.List; @Logger(name = "logger") public class UsersTest { //插入数据 @Test public void test1() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); Users users = new Users(); users.setUCreateTime(new Date()); users.setUEmail("13541@qq.com"); users.setUIdcard("3213124213624057"); users.setUName("张三"); users.setUPass("123456"); int j = 0; for (int i = 0; i < 1000; i++) { j += mapper.insertSelective(users); } System.out.println("j = " + j); sqlSession.commit(); sqlSession.close(); } //查询统计 @Test public void test2() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); //用户例子 UsersExample example = new UsersExample(); //添加查询条件 example.createCriteria() .andUPassEqualTo("123456")//判断密码是123456 .andUSexNotEqualTo("0");//判断性别不能为0 //把查询条件放进去 long l = mapper.countByExample(example); System.out.println("l = " + l); sqlSession.close(); } //根据条件进行删除 @Test public void test3() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); //用户例子 UsersExample example = new UsersExample(); //判断值包含某些数据的时候 List values = new ArrayList(); values.add((long) 2); values.add((long) 4); values.add((long) 6); //添加查询条件 example.createCriteria() .andUIdIn(values); int i = mapper.deleteByExample(example); System.out.println("影响行数 = " + i); sqlSession.commit(); sqlSession.close(); } //根据主键进行删除 @Test public void test4() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); int i = mapper.deleteByPrimaryKey((long) 3); System.out.println("影响行数 = " + i); sqlSession.commit(); sqlSession.close(); } //根据全部字段添加 @Test public void test5() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); Users users = new Users(); users.setUCreateTime(new Date()); users.setUEmail("13541@qq.com"); users.setUIdcard("3213124213624057"); users.setUName("翠花"); users.setUPass("123456"); users.setUSex("0"); users.setUValid(true); int i = mapper.insert(users); System.out.println("影响行数 = " + i); sqlSession.commit(); sqlSession.close(); } //根据条件查询 @Test public void test6() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); //用户例子 UsersExample example = new UsersExample(); //查询所有用户名叫张三的用户,每页显示5条 example.createCriteria().andUNameEqualTo("张三"); //开启分页 PageHelper.startPage(1,5); List users = mapper.selectByExample(example); //把结果集给分页插件封装 PageInfo usersPageInfo = new PageInfo(users); System.out.println("数据" + usersPageInfo.getList()); System.out.println("总条数" + usersPageInfo.getTotal()); sqlSession.close(); } //根据主键查询 @Test public void test7() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); Users users = mapper.selectByPrimaryKey((long) 7); System.out.println("users = " + users); sqlSession.close(); } //把除了11 12 13 14的男性姓张的用户 用户的密码改为 654321 //根据条件字段和更新字段进行更新 @Test public void test8() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); //值 Users record = new Users(); record.setUPass("654321"); //条件 UsersExample example = new UsersExample(); List values = new ArrayList(); values.add((long)11); values.add((long)12); values.add((long)13); values.add((long)14); example.createCriteria() .andUIdNotIn(values) .andUSexEqualTo("1") .andUNameLike("张%"); //调用接口 int i = mapper.updateByExampleSelective(record, example); System.out.println("影响行数 = " + i); sqlSession.commit(); sqlSession.close(); } //根据条件修改 但是会修改所有值(用的不多,作为了解) @Test public void test9() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); //值 Users record = new Users(); //条件 UsersExample example = new UsersExample(); //调用接口 int i = mapper.updateByExample(record, example); sqlSession.commit(); sqlSession.close(); } //根据主键的条件更新 @Test public void test10() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); //值 Users record = new Users(); record.setUId((long)500); record.setUName("翠花"); //调用接口 int i = mapper.updateByPrimaryKeySelective(record); System.out.println("i = " + i); sqlSession.commit(); sqlSession.close(); } //根据主键的所有更新 @Test public void test11() { //调用工具类 得到sqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //加载dao层映射 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); //查询id为600的用户 Users users = mapper.selectByPrimaryKey((long) 600); //值 users.setUPass("223344"); System.out.println(users); //调用接口 int i = mapper.updateByPrimaryKey(users); System.out.println("i = " + i); sqlSession.commit(); sqlSession.close(); } } ```