zoukankan      html  css  js  c++  java
  • MyBatis单表增删改查--接口实现

      我是用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;
    
        @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<Student> 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();
            }
        }
    }
  • 相关阅读:
    Java笔记6之三目运算符
    java笔记5之逻辑运算符以及&&与&的区别
    SAP CRM OData模型里的addressable为true的含义
    SAP CRM OData multiple origin Composition的测试
    重构老系统遗留代码的一些方法学习笔记
    SAP CRM系统里的附件存储逻辑
    如何用Postman创建SAP CRM的Opportunity业务数据
    另一种使用SAP SAT事务码对通过浏览器启动的应用的性能测量和分析方式
    SAP Hybris Commerce,CRM和C4C的登录语言选择
    SAP Hybris Commerce的页面路由实现
  • 原文地址:https://www.cnblogs.com/zhao307/p/5402072.html
Copyright © 2011-2022 走看看