博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis单表增删改查--接口实现
阅读量:4328 次
发布时间:2019-06-06

本文共 4033 字,大约阅读时间需要 13 分钟。

  我是用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
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;    @Before    public 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();        }    }    @Test    public void testQueryStudent() {        SqlSession session = factory.openSession();        try {            StudentMapper mapper = session.getMapper(StudentMapper.class);            List
students = mapper.queryStudent(); for (Student student : students) { System.out.println(student); } session.commit(); } finally { session.close(); } } @Test public 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(); } } @Test public void testDeleteStudentById() { SqlSession session = factory.openSession(); try { StudentMapper mapper = session.getMapper(StudentMapper.class); mapper.deleteStudentById(2); session.commit(); } finally { session.close(); } } @Test public 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(); } } @Test public 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

你可能感兴趣的文章
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>