我是用xml实现了单表的增删改查之后才改用接口的,所以我这里就不把那些配置拷贝过来了,其实东西都一样。

1:接口

  这个更有意思了。把具体的操作写在了注解里面。

package com.zhao.mapper;import java.util.List;import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;import com.zhao.entity.Student;
public interface StudentMapper {@Select(value="select * from student")public List<Student> queryStudent();@Select(value="select * from student where stu_id=#{stu_id}")public Student selectStudentById(int stu_id);@Delete(value="delete from student where stu_id=#{stu_id}")public void deleteStudentById(int stu_id);@Insert(value="insert into student(stu_name,stu_gender) values(#{stu_name},#{stu_gender})")public void insertStudent(Student student);@Update(value="update student set stu_name=#{stu_name},stu_gender=#{stu_gender} where stu_id=#{stu_id}")public void updateStudentById(Student student);
}

2:测试代码

  依旧是增删改查,接口实现是session获取接口对象,然后调用接口相应的方法。至于底层的东西 mybatis就帮我们解决了,我们不需要在这里用具体的实现类,具体的实现类也不好用。这样多方法,只写法方法签名,然后把数据表的操作写注解了,直接用就可以了。

package com.zhao.mapper;import static org.junit.Assert.*;import java.io.InputStream;
import java.util.List;import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;import com.zhao.entity.Student;
import com.zhao.entity.StudentTest;public class StudentMapperTest {private SqlSessionFactory factory;@Beforepublic void before() {try {/** 1: Reader reader* =Resources.getResourceAsReader("Configuration.xml"); factory =* new SqlSessionFactoryBuilder().build(reader);*/// 2:InputStream inputStream = StudentTest.class.getClassLoader().getResourceAsStream("Configuration.xml");factory = new SqlSessionFactoryBuilder().build(inputStream);} catch (Exception e) {e.printStackTrace();}}@Testpublic void testQueryStudent() {SqlSession session = factory.openSession();try {StudentMapper mapper = session.getMapper(StudentMapper.class);List<Student> students = mapper.queryStudent();for (Student student : students) {System.out.println(student);}session.commit();} finally {session.close();}}@Testpublic void testSelectStudentById() {SqlSession session = factory.openSession();try {StudentMapper mapper = session.getMapper(StudentMapper.class);Student student = mapper.selectStudentById(2);System.out.println(student);session.commit();} finally {session.close();}}@Testpublic void testDeleteStudentById() {SqlSession session = factory.openSession();try {StudentMapper mapper = session.getMapper(StudentMapper.class);mapper.deleteStudentById(2);session.commit();} finally {session.close();}}@Testpublic void testInsertStudent() {SqlSession session = factory.openSession();try {StudentMapper mapper = session.getMapper(StudentMapper.class);Student student = new Student("zaza", "男");mapper.insertStudent(student);session.commit();} finally {session.close();}}@Testpublic void testUpdateStudentById() {SqlSession session = factory.openSession(true);try {StudentMapper mapper = session.getMapper(StudentMapper.class);Student student = new Student(7,"zaza", "m");mapper.updateStudentById(student);session.commit();} finally {session.close();}}
}

转载于:https://www.cnblogs.com/zhao307/p/5402072.html

MyBatis单表增删改查--接口实现相关推荐

  1. mybatis --入门 单表增删改查-curd

    目录 1. mybatis 环境搭建 2. 实体类映射文件配置(写sql) 3. mybatis核心配置文件 (环境配置) 4. 测试 mybatis document https://mybatis ...

  2. C# 功能完整的单表增删改查程序

    id字段自增,标识增量和种子1: using System; using System.Collections.Generic; using System.ComponentModel; using ...

  3. MySQL基础知识-MySQL概述安装,单表增删改查,函数,约束,多表查询,事物

    MySQL基础知识-MySQL概述安装,单表增删改查,函数,约束,多表查询,事物 前言 1.MySQL概述 1.1数据库相关概念 1.2MySQL数据库 1.2.1版本 1.2.2下载 1.2.3安装 ...

  4. mysql如何修改学生表_MySQL 详细单表增删改查crud语句

    MySQL 增删改查语句 1.创建练习表 这里练习表没有满足三范式 第一范式(又称 1NF):保证每列的原子性 数据表中的每一列(字段),必须是不可拆分的最小单元,也就是确保每一列的原子性.满足第一范 ...

  5. django调用python脚本返回_Django框架(九)—— 单表增删改查,在Python脚本中调用Django环境...

    单表增删改查,在Python脚本中调用Django环境 一.数据库连接配置 如果连接的是pycharm默认的Sqlite,不用改动,使用默认配置即可 如果连接mysql,需要在配置文件中的settin ...

  6. Django框架(八)--单表增删改查,在Python脚本中调用Django环境

    一.数据库连接配置 如果连接的是pycharm默认的Sqlite,不用改动,使用默认配置即可 如果连接mysql,需要在配置文件中的setting中进行配置: 将DATABASES={} 更新为 DA ...

  7. springboot整合mybatis实现简单的单表增删改查(完整代码可下载)

    搭建项目 项目简单效果,前端效果丑的一批,主要是后端功能实现: springboot增删改查 csdn完整代码下载链接: springboot+mybatis Gitee下载地址: Gitee下载地址 ...

  8. 增删改查最终总结—2.1.1(单表-增删改查)

    一个 Mybatis 开发神器:Fast MyBatis 超好用 --------Fast Mybatis开发文档 每一个增删改查前都要先看这个--增删改查操作 都需要注意: 1.在控制层最后一行代码 ...

  9. django(七)之数据库表的单表-增删改查QuerySet,双下划线

    https://www.cnblogs.com/haiyan123/p/7738435.html https://www.cnblogs.com/yuanchenqi/articles/6083427 ...

  10. Mybatis学习之单表增删改查

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapperPUBLIC "-// ...

最新文章

  1. OpenCV 3最新模块介绍
  2. python【蓝桥杯vip练习题库】ALGO-71比较字符串
  3. 初学者选黑卡还是微单_零基础,一篇读懂单反和微单
  4. 关于Background-size的几个参数区别
  5. 8能达到go速度吗 php_相同逻辑的php与golang代码效率对比,最好语言落谁家…
  6. 干掉13个区块链最常见的Bug!
  7. go struct 零值_《Go 语言程序设计》读书笔记 (五) 协程与通道
  8. Servlet 原理概述
  9. html5 canvas花瓣,canvas花瓣飘落
  10. HTML5CSS3知识点总结(1)
  11. 【深入浅出指南:JVM知多少】一、JVM内存模型
  12. 《管理的常识》读书笔记
  13. 索尼(SONY)笔记本装系统蓝屏问题解决方案
  14. 基于云桌面的外部设备重定向技术调研
  15. [面试经验]某互联网公司霸面经验
  16. python27.dll是系统自带的吗_Windows 10自带6款超强工具!好用又免费!
  17. 纵然前方困难重重,我们也要一直坚持
  18. MapReduce处理“大量”图片
  19. 网络---IP地址和端口
  20. JZOJ 3388. 【NOIP2013模拟】绿豆蛙的归宿

热门文章

  1. Ubuntu 下用 enca 转化文件字符编码
  2. Caffe学习:使用pycaffe读取caffemodel参数
  3. 【机器学习】Tensorflow基本使用
  4. Keras指定GPU训练模式,设置GPU的使用量
  5. 三种基于感知哈希算法的相似图像检索技术
  6. torch.rand() 和 torch.randn() 有什么区别?
  7. mysql数据库d盘_Windows Server 2008 R2下修改MySQL 5.5数据库目录为D盘
  8. mysql字段类型解析_MySQL数据类型之数字类型详细解析
  9. 写入接口c语言_嵌入式LCD的接口类型详解
  10. 凯立德地图导航2020年最新版车载_高精度地图会把自动驾驶带跑偏吗?