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();
        }
    }

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

  • 相关阅读:
    C# 各版本的新特性
    EntityFramework增删改查
    web.config配置文件中的configSource属性
    IOC
    权限系统设计实现
    代码依赖和解除具体依赖的技术
    ajax利用html5新特性带进度条上传文件
    React Native 从入门到原理
    npm中本地安装命令行类型的模块是不注册Path的
    需求分析-验证控件的功能需求列表
  • 原文地址:https://www.cnblogs.com/YC-L/p/14280070.html
Copyright © 2011-2022 走看看