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

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

  • 相关阅读:
    xshell 缺少mfc110u.dll
    notepad++ 常用插件
    java 发送 http post 和 get 请求(利用unirest)
    my.conf配置大全
    md5算法
    sprinboot+redis
    jq下拉插件,chosen
    springboot+idea 热部署
    Jquery 监听浏览器前进后退
    手机自带的表情入库
  • 原文地址:https://www.cnblogs.com/YC-L/p/14280070.html
Copyright © 2011-2022 走看看