mybatis 实现oracle主键自增的机制

论坛 期权论坛 脚本     
匿名技术用户   2021-1-6 01:51   35   0

本篇文章将研究mybatis 实现oracle主键自增的机制

首先我们看对于同一张student表,对于mysql,sql server,oracle中它们都是怎样创建主键的

在mysql中

  1. create table Student(
  2. Student_ID int(6) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  3. Student_Name varchar(10) NOT NULL,
  4. Student_Age int(2) NOT NULL
  5. );
  6. insert into student(student_name,student_age) values('zhangsan',20);

在sql server中

  1. create table Student(
  2. Student_ID int primary key identity(1,1),
  3. Student_Name varchar2(10) NOT NULL,
  4. Student_Age number(2) NOT NULL
  5. );
  6. insert into student(student_name,student_age) values('zhangsan',20);

在oracle中

  1. create table Student(
  2. Student_ID number(6) NOT NULL PRIMARY KEY,
  3. Student_Name varchar2(10) NOT NULL,
  4. Student_Age number(2) NOT NULL
  5. );

而oracle如果想设置主键自增长,则需要创建序列

  1. CREATE SEQUENCE student_sequence
  2. INCREMENT BY 1
  3. NOMAXVALUE
  4. NOCYCLE
  5. CACHE 10;
  6. insert into Student values(student_sequence.nextval,'aa',20);
如果使用了触发器的话,就更简单了

  1. create or replace trigger student_trigger
  2. before insert on student
  3. for each row
  4. begin
  5. select student_sequence.nextval into :new.student_id from dual;
  6. end student_trigger;
  7. /
此时插入的时候触发器会帮你插入id

  1. insert into student(student_name,student_age) values('wangwu',20);

至此,mysql,sql server,oracle中怎样创建表中的自增长主键都已完成。看一看出oracle的主键自增较mysql和sql sever要复杂些,mysql,sqlserver配置好主键之后,插入时,字段和值一一对应即可,数据库就会完成你想做的,但是在oracle由于多了序列的概念,那么oracle怎样实现主键自增呢?且看下文

首先是mybatis框架的配置文件

jdbc.properties文件

[html] view plain copy
  1. username=go
  2. password=go
  3. url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
  4. driver=oracle.jdbc.driver.OracleDriver

mybatis-config.xml文件

[html] view plain copy
  1. <configuration>
  2. <properties resource="jdbc.properties"/>
  3. <typeAliases>
  4. <package name="com.bean"/>
  5. </typeAliases>
  6. <environments default="development">
  7. <environment id="development">
  8. <transactionManager type="JDBC" />
  9. <dataSource type="POOLED">
  10. <property name="driver" value="${driver}" />
  11. <property name="url" value="${url}" />
  12. <property name="username" value="${username}" />
  13. <property name="password" value="${password}" />
  14. </dataSource>
  15. </environment>
  16. </environments>
  17. <!-- 将mapper文件加入到配置文件中 -->
  18. <mappers>
  19. <mapper resource="com/bean/Student.xml" />
  20. </mappers>
  21. </configuration>

对应的实体类Student无变化,参考hibernate操作oracle数据库 主键自增

http://blog.csdn.net/thepeakofmountain/article/details/17173715

对应的Student.xml文件

[html] view plain copy
  1. <mapper namespace="com.bean.Student">
  2. <insert id="add" parameterType="Student">
  3. <!--
  4. <selectKey keyProperty="student_id" resultType="int" order="BEFORE">
  5. select student_sequence.nextval from dual
  6. </selectKey>
  7. 如果未使用触发器,请保留该注释
  8. --!>
  9. insert into student(student_id,student_name,student_age) values(#{student_id},#{student_name},#{student_age})
  10. </insert>
  11. <select id="load" parameterType="int" resultType="Student">
  12. select * from student where student_id=#{student_id}
  13. </select>
  14. <select id="delete" parameterType="int" resultType="int">
  15. delete from student where student_id=#{student_id}
  16. </select>
  17. <update id="update" parameterType="Student">
  18. update student set student_name=#{student_name},student_age=#{student_age} where student_id=#{student_id}
  19. </update>
  20. <select id="list" resultType="Student">
  21. select * from student
  22. </select>
  23. </mapper>


测试类

[java] view plain copy
  1. public class TestMybatis {
  2. @Test
  3. public void testAdd() { //为原始的获取配置文件,自己创建session,一步一步走的
  4. try {
  5. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
  6. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
  7. SqlSession session = factory.openSession();
  8. Student u = new Student();
  9. u.setStudent_name("sunwukong");
  10. u.setStudent_age(50);
  11. session.insert("com.bean.Student.add", u);
  12. session.commit();
  13. session.close();
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. @Test
  19. public void testUpdate(){
  20. SqlSession session = null;
  21. try {
  22. session = MyBatisUtil.createSession();
  23. Student stu = new Student();
  24. stu.setStudent_id(11);
  25. stu.setStudent_name("bajie");
  26. stu.setStudent_age(20);
  27. session.update(Student.class.getName()+".update", stu);
  28. session.commit();
  29. session.close();
  30. } catch (Exception e) {
  31. // TODO: handle exception
  32. e.printStackTrace();
  33. }
  34. }
  35. /*@Test
  36. public void testDelete() {
  37. SqlSession session= null;
  38. try {
  39. session = MyBatisUtil.createSession();
  40. session.delete(Student.class.getName()+".delete",105);
  41. session.commit();
  42. session.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }*/
  47. @Test
  48. public void testLoad() {
  49. SqlSession session = null;
  50. try{
  51. session = MyBatisUtil.createSession();
  52. Student u = (Student)session.selectOne(Student.class.getName()+".load", 11);
  53. System.out.println(u.getStudent_name());
  54. } finally {
  55. MyBatisUtil.closeSession(session);
  56. }
  57. }
  58. @Test
  59. public void testList() {
  60. SqlSession session = null;
  61. try{
  62. session = MyBatisUtil.createSession();
  63. List<Student> us = session.selectList(Student.class.getName()+".list", null);
  64. System.out.println(us.size());
  65. } finally {
  66. MyBatisUtil.closeSession(session);
  67. }
  68. }
  69. }
工厂类MyBatisUtil

[java] view plain copy
  1. public class MyBatisUtil {
  2. public static SqlSessionFactory factory;
  3. static {
  4. try {
  5. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
  6. factory = new SqlSessionFactoryBuilder().build(is);
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. public static SqlSession createSession() {
  12. return factory.openSession();
  13. }
  14. public static void closeSession(SqlSession session) {
  15. if(session!=null) session.close();
  16. }
  17. }

小结:mybatis+oracle主键自增实现的核心,就插入来说,就是先从序列中查找一个序列值,然后插入到对应的表中,也就是分两步走

先select student_sequence.nextval from dual

后insert into student(student_id,student_name,student_age) values(#{student_id},#{student_name},#{student_age})

比较hibernate和mybatis,实现oracle主键自增都是需要两步,而在hibernate中无论是注解版还是非注解版,都需要将id字段映射到创建的序列名上。

补充:mybatis框架导入的jar包为mybatis-3.3.2.jar版本,junit为junit-4.5.jar,连接oracle的jar包ojdbc14.jar,其中MyBatisUtil.java文件其实是一个创建简单工厂模式,如果有兴趣,可以看看设计模式方面的书

对于我来说,还是喜欢用sql语句,感觉更原始,更清楚的知道自己在干什么,当然越底层,效率的话,肯定是mybatis高一些,但是现在还是hibernate用的多吧,当然只是我一家之言,欢迎与各路朋友探讨相关问题。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP