zoukankan      html  css  js  c++  java
  • 事务提交与不同数据库的自增方式

    事务自动提交

      手动提交:

    //Connection - SqlSession操作Mybatis
            //conf.xml->reader
            Reader reader = Resources.getResourceAsReader("conf.xml");
            //reader->sqlSession
    
            //可以通过build的第二参数 指定数据库环境
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"devOracle");
            SqlSession session = sessionFactory.openSession();
    
            StudentMapper studentMapper=session.getMapper(StudentMapper.class);
            Student student=new Student(6,"xx006",105,"xx");
    
    
            studentMapper.addStudent(student);
    
            session.commit();//手动提交
            session.close();

      自动提交:

         //Connection - SqlSession操作Mybatis
            //conf.xml->reader
            Reader reader = Resources.getResourceAsReader("conf.xml");
            //reader->sqlSession
    
            //可以通过build的第二参数 指定数据库环境
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"devOracle");
            SqlSession session = sessionFactory.openSession(true);//设置自动提交
    
            StudentMapper studentMapper=session.getMapper(StudentMapper.class);
            Student student=new Student(6,"xx006",105,"xx");
    
    
            System.out.println(student);
            studentMapper.addStudent(student);
    
           // session.commit();//手动提交
            session.close();

     自增问题

      a.mysql支持自增

    创建自增表

    create table student1(
        stuno int(4) primary key auto_increment,
        stuname varchar(10),
        stuage int(4),
        graname varchar(10)
    );
    srcorgmyymapperstudentMapper.xml

    自增写入:只需要配置两个属性即可:useGeneratedKeys="true" keyProperty="stuNo"

        <insert id="addStudent" parameterType="Student" databaseId="mysql"
                useGeneratedKeys="true" keyProperty="stuNo">
            insert into student1(stuname,stuage,graname) values(#{stuName},#{stuAge},#{graName})
        </insert>

    srcorgmyy estTest.java

            //Connection - SqlSession操作Mybatis
            //conf.xml->reader
            Reader reader = Resources.getResourceAsReader("conf.xml");
            //reader->sqlSession
    
            //可以通过build的第二参数 指定数据库环境
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"devMysql");
            SqlSession session = sessionFactory.openSession(true);//设置自动提交
    
            StudentMapper studentMapper=session.getMapper(StudentMapper.class);
            Student student=new Student(null,"xx006",105,"xx");
    
            /*int result=studentMapper.addStudent(student);
            System.out.println("增加了"+result+"个学生");*/
    
            System.out.println(student);
            studentMapper.addStudent(student);
            Integer stuno=student.getStuNo();
            System.out.println(stuno);//自增写入:只需要配置两个属性即可:useGeneratedKeys="true" keyProperty="stuNo"
            System.out.println(student);
           // session.commit();//手动提交
            session.close();        

    b.oracle不支持自增:通过序列模拟实现
    创建自增序列

    create sequence myseq
    increment by 1
    start with 1;

    序列自带的两个属性:
    nextval:序列中下一个值
    currval:当前值


    srcorgmyymapperstudentMapper.xml

    //推荐

    <insert id="addStudent" parameterType="org.myy.entity.Student" databaseId="oracle">
            <selectKey keyProperty="stuNo" resultType="Integer" order="BEFORE">
                select myseq.nextval from dual
            </selectKey>
            insert into student1(stuno,stuname,stuage,graname) values(#{stuNo},#{stuName},#{stuAge},#{graName})
    </insert>

     通过<insert>的子标签<selectKey>实现:在<selectKey>中查询下一个序列(自增后的值),再将此值传入keyProperty="stuNo"属性,最后在真正执行时使用该属性值

    或者

    <insert id="addStudent" parameterType="Student" databaseId="oracle">
            <selectKey keyProperty="stuNo" resultType="Integer" order="AFTER"> 
                select myseq.currval from dual
            </selectKey>
            insert into student1(stuno,stuname,stuage,graname)
            values(myseq.nextval,#{stuName},#{stuAge},#{graName})
    </insert>

    srcorgmyy estTest.java

         //Connection - SqlSession操作Mybatis
            //conf.xml->reader
            Reader reader = Resources.getResourceAsReader("conf.xml");
            //reader->sqlSession
    
            //可以通过build的第二参数 指定数据库环境
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"devOracle");
            SqlSession session = sessionFactory.openSession(true);//设置自动提交
    
            StudentMapper studentMapper=session.getMapper(StudentMapper.class);
            Student student=new Student(null,"xx006",105,"xx");
    
    
            System.out.println(student);
            studentMapper.addStudent(student);
            Integer stuno=student.getStuNo();
            System.out.println(stuno);
            System.out.println(student);
           // session.commit();//手动提交
            session.close();



  • 相关阅读:
    福大软工1816 · 第三次作业
    福大软工1816 · 第二次作业
    2018福大软工第一次作业
    20180823-软工实践第一次作业-自我介绍
    福大软工1816 · 第一次作业
    开博之作 · 简单的自我介绍
    2018软件工程实践第一次作业
    系列最终篇!
    含继承多态思想的四则运算器和简单工厂模式初步
    作业六 栈的使用和界面编程探索
  • 原文地址:https://www.cnblogs.com/mayouyou/p/13262875.html
Copyright © 2011-2022 走看看