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

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

  • 相关阅读:
    浏览器兼容
    jquery 判断网络资源,网络文件是否存在
    [Maven] 变态问题收集
    maven project中,在main方法上右键Run as Java Application时,提示错误:找不到或无法加载主类XXX.XXXX.XXX
    maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project web_nanchang
    java 获取文件列表,并按照文件名称排序
    maven install时报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
    tar 命令基本使用(加密)
    八卦某 G 的前端开发方式及流程
    use mkisofs 重新打包beini,tinycore linux
  • 原文地址:https://www.cnblogs.com/YC-L/p/14280070.html
Copyright © 2011-2022 走看看