zoukankan      html  css  js  c++  java
  • Mybatis的介绍和基本使用

     数据库操作框架的历程

     JDBC,几乎不用了,但其他框架都是依据JDBC搭建的

     DBUtils

     

     Hibernate,很少用了

     

     Hibernate 优势

     

     Hibernate 劣势

     

     memcache已经被淘汰掉了,都用redis

     JDBCTemplate

     

     Mybatis

     官方教程

     https://mybatis.org/mybatis-3/getting-started.html

    • 用maven在pom中加载
    • 构建SqlSessionFactory,使用xml或者不使用xml
    • 从SqlSessionFactory 中获取 SqlSession

     EmpDao.xml

     

     mybatis-config.xml

     

     environment,transactionManager,dataSource

     db.properties

     

      mybatis-config.xml

     

     探究已映射的 SQL 语句

    public class Emp{
        private Integer empno;
        private String ename;
        private String job;
        private Integer mgr;
        private Date hiredate;
        private Double sal;
        private Double comm;
        private Integer deptno;
    }
    
    public interface EmpDao{
        public void save(Emp emp);
        public Integer update(Emp emp);
        public Integer delete(Integer empno);
        @Select()
        public Emp selectEmpByEmpno(Integer empno);
    }
    
    public class MyTest{
    
        SqlSessionFactory sqlSessionFactory = null;
    
        public void init(){
            String resource = "mybatis-config.xml";
            InputStream inputStream = null;
            try{
                inputStream Resources.getResourceAsStream(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            }catch(IOException e){
                e.printStackTrace();
            }        
        }
    
        @Test
        public void test01 thorws IOException(){
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            // 读取数据库会话
            SqlSession sqlSession = sqlSessionFactory.openSession();
            // 获取对应的映射接口对象
            EmpDao mapper = sqlSession.getMapper(EmpDao.class);
            // 执行具体的sql语句
            Emp emp = mapper.selectEmpByEmpno(7369);
            System.out.println(emp);
            Integer i = mapper.save(new Emp());
            System.out.println(i);// 插入不成功, 需要在EmpDao.xml或者注解中编写完整, 复杂就写配置文件
            // 关闭会话
            sqlSession.close();
        }
    
        @Test
        public void test02() thorws IOException{
            SqlSession sqlSession = sqlSessionFactory.openSession();
            EmpDao mapper = sqlSession.getMapper(EmpDao.class);
            Emp emp = new Emp();
            emp.setEmpno(3333);
            emp.setEname("zs");
            Integer save = mapper.save(emp);
            System.out.println(save);
        }
    
        @Test
        public void test03() thorws IOException{
            SqlSession sqlSession = sqlSessionFactory.openSession();
            EmpDao mapper = sqlSession.getMapper(EmpDao.class);
            Emp emp = new Emp();
            emp.setEmpno(3333);
            emp.setEname("zs");
            emp.setComm(500.0);
            Integer update = mapper.save(emp);
            System.out.println(update);
            sqlSession.commit();
            sqlSession.close();
        }
    }

     补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字

  • 相关阅读:
    vue : 无法加载文件 C:Users1AppDataRoaming pmvue.ps1,因为在此系统上禁止运行脚本
    Flutter 常用的第三方库
    Dart 中的类
    Flutter 学习
    在 VSCode 中开发Flutter项目
    Flutter 环境配置的一些坑
    前端资源和优秀项目地址
    一小时学习JQuery材料
    基于RCT6的YX6100语音模块方案
    Java中反射和内省代码实例
  • 原文地址:https://www.cnblogs.com/YC-L/p/14280070.html
Copyright © 2011-2022 走看看