歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

MyBatis+Spring整合示例

說明:

1、由於目前Spring官方還沒有出整合Mybatis的特性,所以這裡在Spring框架和MyBatis框架上再添加用於整合的框架“mybatis-spring-1.0.0.jar”(該框架時MyBatis官方自己出的)。

2、這裡同樣以持久化一個User bean為示例,這裡不在詳述每個文件的意義,可參考上一篇日志: http://www.linuxidc.com/Linux/2014-08/105042.htm

Java實戰應用:Mybatis實現單表的增刪改 http://www.linuxidc.com/Linux/2014-06/103456.htm

[Java][Mybatis]物理分頁實現 http://www.linuxidc.com/Linux/2014-04/99889.htm

Mybatis快速入門教程 http://www.linuxidc.com/Linux/2013-06/85762.htm

Mybatis的關於批量數據操作的測試 http://www.linuxidc.com/Linux/2012-05/60863.htm

Mybatis中對List<Object> 對象List的批處理插入操作 http://www.linuxidc.com/Linux/2014-02/96916.htm

建表代碼如下:

  1. createtable pda2_user(
  2. id varchar2(50) primarykey,
  3. name varchar2(100) notnull,
  4. psw varchar2(100) notnull
  5. );
  6. ######################################
  7. createsequence pda2_user_sequence
  8. minvalue 1
  9. maxvalue 999999999999999999999999999
  10. start with 1
  11. increment by 1
  12. cache 20;
  13. #####################################
  14. createtrigger"id_auto_increment_triger"
  15. before inserton pda2_user
  16. for each row
  17. when (new.id isnull)
  18. begin
  19. select pda2_user_sequence.nextval into :new.id from dual;
  20. end;
  21. ###################################################
  22. insertinto pad2_user (name,psw) values('chenjun','123')
  23. ####說明######
  24. 我使用的是Oracle數據庫,為了使用主鍵自增長的方式,特此建立一個使用sequence的觸發器

 

User bean代碼如下:

  1. package com.deppon.pda.v2.model.basic.User;
  2. publicclass User {
  3. private String ID;
  4. private String name;
  5. private String psw;
  6. public User() {
  7. super();
  8. }
  9. public User(String name, String psw) {
  10. super();
  11. this.name = name;
  12. this.psw = psw;
  13. }
  14. public String getID() {
  15. return ID;
  16. }
  17. publicvoid setID(String iD) {
  18. ID = iD;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. publicvoid setName(String name) {
  24. this.name = name;
  25. }
  26. public String getPsw() {
  27. return psw;
  28. }
  29. publicvoid setPsw(String psw) {
  30. this.psw = psw;
  31. }
  32. }

 

User實體對應的映射器UserMapper代碼如下:

  1. package com.deppon.pda.v2.mapper.User;
  2. import com.deppon.pda.v2.model.basic.User.User;
  3. publicinterface UserMapper {
  4. publicvoid insertUser(User user);
  5. public User getUser(String name);
  6. }

 

映射器配置文件UserMapper.xml代碼如下:

  1. ?xml version="1.0"encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mappernamespace="com.deppon.pda.v2.mapper.User.UserMapper">
  6. <insertid="insertUser"parameterType="com.deppon.pda.v2.model.basic.User.User">
  7. insert into pda2_user(name,age) values(#{name},#{age})
  8. </insert>
  9. <selectid="getUser"resultType="com.deppon.pda.v2.model.basic.User.User"parameterType="java.lang.String">
  10. select * from pda2_user where name=#{name}
  11. </select>
  12. <!-- 當使用該Mybatis與Spring整合的時候,該文件必須和相應的Mapper接口文件同名,並在同一路徑下 -->
  13. </mapper>

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-08/105043p2.htm

Copyright © Linux教程網 All Rights Reserved