MyBatis的CURD操作

添加CURD接口方法

package mapper;

import entity.UserEntity;
import org.apache.ibatis.annotations.Param;
import java.util.List; /**
* @desc User映射器接口
* @date 2020/6/19 上午8:59
*/
public interface UserMapper {
/**
* 根据年龄查询用户信息
* @param age 年龄
* @return user 用户实体集合
*/
public List<UserEntity> selectUserByAge(int age); /**
* 根据年龄和性别查询用户信息
* @param userOne 获取年龄
* @param userTwo 获取性别
* @return 用户实体集合
*/
public List<UserEntity> selectUserByAgeAndSex(@Param("userOne") UserEntity userOne,@Param("userTwo") UserEntity userTwo); /**
* 根据姓名和年龄查询用户信息
* @param name 姓名
* @param user 获取年龄
* @return
*/
public List<UserEntity> selectUserByNameAndAge(@Param("name") String name, @Param("user") UserEntity user); /**
* 查询所有用户信息
* @return 用户实体集合
*/
public List<UserEntity> selectUserAll(); /**
* 新增用户
* @param user 用户实体
* @return 影响行数
*/
public int insertUser(UserEntity user); /**
* 更新用户姓名
* @param user 用户姓名
* @return 影响行数
*/
public int updateUser(@Param("id") int id,@Param("name") String name); /**
* 根据姓名删除用户
* @param name 用户姓名
* @return 影响行数
*/
public int deleteUserById(int id);
}

添加CURD接口方法对应的 UserMapper.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">
<!--namespace表示命名空间,填写Mapper映射器接口全路径-->
<mapper namespace="mapper.UserMapper">
<!--结果集映射(ORM)-->
<resultMap id="userResultMap" type="entity.UserEntity">
<!-- propery表示UserEntity属性名,column表示tb_user表字段名-->
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="password" column="password" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
<result property="birthday" column="birthday" />
<result property="created" column="created" />
<result property="updated" column="updated" />
</resultMap> <!--select查询语句 id表示接口方法名 resultMap表示引用结果集映射-->
<select id="selectUserByAge" resultMap="userResultMap">
select * from tb_user where age > #{age};
</select> <select id="selectUserByAgeAndSex" resultMap="userResultMap">
select * from tb_user where age > #{userOne.age} and sex = #{userTwo.sex};
</select> <select id="selectUserByNameAndAge" resultMap="userResultMap">
select * from tb_user where name = #{name} and age > #{user.age};
</select> <select id="selectUserAll" resultMap="userResultMap">
select * from tb_user;
</select> <insert id="insertUser">
insert into tb_user (id,userName, password, name, age, sex, birthday, created, updated) values
(null,#{userName},#{password},#{name},#{age},#{sex},#{birthday},now(),now());
</insert> <update id="updateUser">
update tb_user set name=#{name} where id=#{id};
</update> <delete id="deleteUserById">
delete from tb_user where id=#{id};
</delete>
</mapper>

使用 JUnit 单元测试框架测试 CURD 接口方法

在 test/java下创建 MyBatisTest.java 文件,如下:

import entity.UserEntity;
import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; /**
* @author benjamin.xu
* @desc
* @date 2020/6/23 下午5:36
*/
public class MyBatisTest {
private UserMapper userMapper;
private SqlSession sqlSession; @Before
public void setUp() throws Exception {
// 指定mybatis环境配置文件
String resource = "mybatis-config.xml";
// 读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource); // 构建sqlSessionFactory
SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(inputStream); // 获取sqlSession
sqlSession = sqlSessionFactory.openSession(); // 获取userMapper实例
userMapper = sqlSession.getMapper(UserMapper.class);
} @After
public void tearDown() throws Exception {
sqlSession.close();
} @Test
public void selectUserByAgeTest() {
List<UserEntity> userEntities = userMapper.selectUserByAge(20);
Assert.assertNotNull(userEntities);
} @Test
public void selectUserAllTest() {
List<UserEntity> userEntities = userMapper.selectUserAll();
Assert.assertEquals(2,userEntities.size());
} @Test
public void insertUserTest() throws Exception{
UserEntity userEntity = new UserEntity();
userEntity.setUserName("ww");
userEntity.setName("王五");
userEntity.setAge(21);
userEntity.setPassword("123456");
userEntity.setSex(1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdf.parse("1992-01-10");
userEntity.setBirthday(birthday); int result = userMapper.insertUser(userEntity);
sqlSession.commit(); //提交事务
Assert.assertEquals(1,result);
} @Test
public void updateUserTest() {
int result = userMapper.updateUser(1,"张三三");
sqlSession.commit();
Assert.assertEquals(1,result);
} @Test
public void deleteUserByIdTest() {
int result = userMapper.deleteUserById(4);
sqlSession.commit();
Assert.assertEquals(1,result);
} @Test
public void selectUserByAgeAndSexTest() {
UserEntity userEntityOne = new UserEntity();
userEntityOne.setAge(20);
UserEntity userEntityTwo = new UserEntity();
userEntityTwo.setSex(1); List<UserEntity> userEntities
= userMapper.selectUserByAgeAndSex(userEntityOne,userEntityTwo);
System.out.println(userEntities);
Assert.assertNotNull(userEntities);
} @Test
public void selectUserByNameAndAgeTest() {
UserEntity user = new UserEntity();
user.setAge(20); List<UserEntity> userEntities =
userMapper.selectUserByNameAndAge("李四",user);
System.out.println(userEntities);
Assert.assertNotNull(userEntities);
}
}

MyBatis 单表CURD操作(五)的更多相关文章

  1. mysql之字段的修改,添加、删除,多表关系(外键),单表详细操作(增删改)

    字段的修改.添加和删除 create table tf1( id int primary key auto_increment, x int, y int ); #修改 alter table tf1 ...

  2. django-两种方式对单表的操作

    单表操作的内容 我们这里对数据库单表的操作包含增删改查四部分 具体链接数据库的方式我们是通过pymysql,当然你也可以用其他的. 两种方式的概念与区别 1.新url的方式 主要就是我们每一次向后台提 ...

  3. mybatis单表操作实现完全java代码封装

    之前在项目中用到mybtis操作数据库时都是手动写sql,对于我这种sql水平不是很好地人来说痛苦死了:动态查询的sql我表示到现在还不会写呀! 还好,利用数据库表反向生成的工具可以帮我解决大部分的s ...

  4. Django中ORM简介与单表数据操作

    一. ORM简介  概念:.ORM框架是用于实现面向对象编程语言种不同类型系统的数据之间的转换 构建模型的步骤:重点 (1).配置目标数据库信息,在seting.py中设置数据库信息 DATABASE ...

  5. 3、MyBatis教程之CURD操作

    4.CURD操作 1.查询 根据用户 Id查询用户 在UserMapper中添加对应方法 public interface UserMapper { List<User> getUserL ...

  6. 通用mybatis单表操作接口

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  7. springboot整合mybatis及封装curd操作-配置文件

    1 配置文件  application.properties  #server server.port=8090 server.address=127.0.0.1 server.session.tim ...

  8. mysql复习---仅涉及单表的操作

    一.登录数据库 二.创建数据库: 三.删除数据库 四.使用数据库创建表 五.向表中插入数据 六.查询 1.查询所有数据: 2.姓名查询 3.性别查询 4.查询姓名 5.根据年龄大小查询 6.多个条件查 ...

  9. 一次基于innobackupex备份及binlog的单表恢复操作

    [环境介绍] 系统环境:Red Hat Enterprise Linux Server release 7.0 (Maipo) + Server version: 5.7.18-log MySQL C ...

随机推荐

  1. Spring Boot demo系列(六):HTTPS

    2021.2.24 更新 1 概述 本文演示了如何给Spring Boot应用加上HTTPS的过程. 2 证书 虽然证书能自己生成,使用JDK自带的keytool即可,但是生产环境是不可能使用自己生成 ...

  2. Go-31-杂序

    标识符: 在go语言里,标识符要么从包里公开,要么不从包里公开. 当代码导入一个包时,程序可以直接访问这个包中任意一个公开的标识符.这些标识符以大写字母开头.以小写字母开头的标识符是不公开的,不能被其 ...

  3. Knight Moves UVA - 439

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  4. 算法、数据结构、与设计模式等在游戏开发中的运用 (一):单例设计(Singleton Design)

    算法.数据结构.与设计模式等在游戏开发中的运用 (一):单例设计(Singleton Design) 作者: Compasslg 李涵威 1. 什么是单例设计(Singleton Design) 在学 ...

  5. Day 12_61_多线程的创建和启动(二)

    多线程的创建和启动 * 在java中实现多线程的第二种方法 实现java.lang.Runnable接口,重写run()方法 * 推荐使用这种方式,因为实现接口还可以保留类的继承. package c ...

  6. hdu3460 字典树(打印机)

    题意:        给你一些名字,让你用一台打印机去打印这些名字,打印机只有三个操作 (1)打印的都是小写字母 (2)每次可以在当前字母的后面加一位,或删除一位. (3)打印当前串 问你最少多少步可 ...

  7. LA3403天平难题(4个DFS)

    题意:      给出房间的宽度r和每个吊坠的重量wi,设计一个尽量宽但宽度不能超过房间宽度的天平,挂着所有挂坠,每个天平的一段要么挂这一个吊坠,要么挂着另一个天平,每个天平的总长度是1,细节我给出题 ...

  8. hdu4291 暴力循环节+矩阵快速幂

    题意:       给你一个关系式,x[n] = 3*x[n-1] + x[n-2],求x(x(x[n]))%1000000007. 思路:       做这个题目要明确一点,就是对于取余操作大多数时 ...

  9. POJ1325二分匹配或者DINIC(最小路径覆盖)

    题意:        有k个任务,两个机器,第一个机器有n个模式,第二个机器有m个模式,每个任务要么在第一个机器的一个模式下工作,要么在第二个机器的一个模式下工作,机器每切换一个模式需要重启一次,两个 ...

  10. UVA11054Gergovia的酒交易

    题意:       有n个村庄,每个村庄要么买酒要么买酒,负数是买酒,整数是买酒,题目保证所有的数字想加和为0,保证有解,然后每一个村庄往相邻的村庄运k坛酒的花费是k,问满足所有的村庄的最小花费是多少 ...