• MyBatis约定及基于动态代理方式的增删改查


    mapper动态代理方式的crud(MyBatis接口开发)
    原则 :约定优于配置
    硬编码方式;
    abc.java
    Configuration conf=new Configuration();
    con.setName("myProject");
    配置方式;
    abc.xml
    <name>myProject</name>
    约定:默认值就是myProject


    具体实现步骤;
    1.基础环境:mybatis.jar、ojdbc.jar、conf.xml、mapper.xml
    2.(不同之处)
    约定的目标:省略掉statement,即根据约定直接可以定位出SQL语句
    a.接口,接口中的方法必须遵循以下约定
    1.方法名和mapper.xml文件中的标签的id值相同
    2.方法的输入参数和mapper.xml文件中的标签的parameterType类型一致
    3.方法的返回值和mapper.xml文件中标签的resultType类型一致(无论查询结果是一个还是多个,mapperxml的便签中只写一个;如果没有resultType,则返回值为void)
    除了以上约定,要实现接口中的方法和mapper.xml中的SQL标签一一对应,还需要以下一点;
    namespace的值,就是接口的全类名(接口-mapper.xml一一对应)

    匹配的过程:(约定的过程)
    1.根据接口名找到mapper.xml文件(根据的是namespace=接口全类名)
    2.根据接口的方法名找到mapper.xml文件中的SQL标签(方法名=SQL标签的ID值)

    以上2点可以保证:当我们调用接口的方法时,
    程序能自动定位到某一个mapper.xml文件中的sql标签

    习惯:SQL映射文件(mapper.xml)和接口放在同一个包中(注意修改conf.xml)

    /MyBatisProject3/src/org/myy/test/Test.java

    package org.myy.test;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.util.List;
    
    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.myy.entity.Student;
    import org.myy.mapper.StudentMapper;
    
    import oracle.net.aso.s;
    
    public class Test {
        //查询单个学生
        public static void queryStudentByStuno() throws IOException {
            //Connection - SqlSession操作Mybatis
                    //conf.xml->reader
                    Reader reader = Resources.getResourceAsReader("conf.xml");
                    //reader->sqlSession
                    
                    //可以通过build的第二参数 指定数据库环境
                    SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"development");
                    SqlSession session = sessionFactory.openSession();
                    
                    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
                    Student student = studentMapper.queryStudentByStuno(1);
                    System.out.println(student);
                    session.close();
        }
        //查询全部学生
        public static void queryAllStudent() throws IOException {
            //Connection - SqlSession操作Mybatis
                    //conf.xml->reader
                    Reader reader = Resources.getResourceAsReader("conf.xml");
                    //reader->sqlSession
                    //可以通过build的第二参数 指定数据库环境
                    SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"development");
                    SqlSession session = sessionFactory.openSession();
                    
                    
                    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
                    List<Student> students = studentMapper.queryAllStudent();
                    System.out.println(students);
                    session.close();
        }
        //增加学生
        public static void addStudent() throws IOException {
            //Connection - SqlSession操作Mybatis
                    //conf.xml->reader
                    Reader reader = Resources.getResourceAsReader("conf.xml");
                    //reader->sqlSession
                    //可以通过build的第二参数 指定数据库环境
                    SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"development");
                    SqlSession session = sessionFactory.openSession();
                    
                    Student student=new Student(3,"myy",200,"yym");
                    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
                    int count = studentMapper.addStudent(student);
                    session.commit();//提交事务
                    System.out.println("增加"+count+"个学生");
                    session.close();
        }
        //删除学生
        public static void deleteStudentByStuno() throws IOException {
            //Connection - SqlSession操作Mybatis
                    //conf.xml->reader
                    Reader reader = Resources.getResourceAsReader("conf.xml");
                    //reader->sqlSession
                    //可以通过build的第二参数 指定数据库环境
                    SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"development");
                    SqlSession session = sessionFactory.openSession();
                    
                    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
                    int count = studentMapper.deleteStudentByStuno(3);
                    session.commit();//提交事务
                    System.out.println("删除"+count+"个学生");
                    session.close();
        }
        //修改学生
            public static void updateStudentByStuno() throws IOException {
                //Connection - SqlSession操作Mybatis
                        //conf.xml->reader
                        Reader reader = Resources.getResourceAsReader("conf.xml");
                        //reader->sqlSession
                        //可以通过build的第二参数 指定数据库环境
                        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"development");
                        SqlSession session = sessionFactory.openSession();
                        
                        Student student=new Student();
                        //修改哪个人
                        student.setStuNo(2);
                        student.setStuName("lss");
                        student.setStuAge(9);
                        student.setGraName("222");
                        
                        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
                        int count = studentMapper.updateStudentByStuno(student);
                        session.commit();//提交事务
                        System.out.println("修改"+count+"个学生");
                        session.close();
            }
        public static void main(String[] args) throws IOException {
            queryStudentByStuno();
            addStudent();
            queryAllStudent();
            deleteStudentByStuno();
            queryAllStudent();
            updateStudentByStuno();
            queryAllStudent();
        }
    }

    /MyBatisProject3/src/org/myy/mapper/StudentMapper.java

    package org.myy.mapper;
    
    import java.util.List;
    
    import org.myy.entity.Student;
    
    //操作Mybatis的接口
    public interface StudentMapper {
        /* 
         1.方法名和mapper.xml文件中的标签的id值相同 
         2.方法的输入参数和mapper.xml文件中的标签的parameterType类型一致
         3.方法的返回值和mapper.xml文件中标签的resultType类型一致
         */
        //public abstract Student1 queryStudentByStuno(int stuno);
        Student queryStudentByStuno(int stuno);
        //查询qun
        List<Student> queryAllStudent();
        
        int addStudent(Student student);
        
        int deleteStudentByStuno(int stuno);
        
        int updateStudentByStuno(Student student);
    }
  • 相关阅读:
    select2插件用法
    MVC htmlAttributes and additionalViewData
    Log4Net 配置详解
    Windows10搭建ASP.NET MVC5 IIS局域网(外网可以访问的)服务器
    网络连接状态指示器(NCSI ,Network Connectivity Status Indicator)
    微软官方开发文档结构WPF
    旋转编码器,无限旋转的音量按钮
    Steam Dota2 安装方法以及一些设置·
    Windows资源管理器打开文件夹卡顿原因及解决办法
    字幕变集合制作工具
  • 原文地址:https://www.cnblogs.com/mayouyou/p/13195320.html
Copyright © 2020-2023  润新知