說明:
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
建表代碼如下:
- createtable pda2_user(
- id varchar2(50) primarykey,
- name varchar2(100) notnull,
- psw varchar2(100) notnull
- );
- ######################################
- createsequence pda2_user_sequence
- minvalue 1
- maxvalue 999999999999999999999999999
- start with 1
- increment by 1
- cache 20;
- #####################################
- createtrigger"id_auto_increment_triger"
- before inserton pda2_user
- for each row
- when (new.id isnull)
- begin
- select pda2_user_sequence.nextval into :new.id from dual;
- end;
- ###################################################
- insertinto pad2_user (name,psw) values('chenjun','123')
- ####說明######
- 我使用的是Oracle數據庫,為了使用主鍵自增長的方式,特此建立一個使用sequence的觸發器
User bean代碼如下:
- package com.deppon.pda.v2.model.basic.User;
- publicclass User {
- private String ID;
- private String name;
- private String psw;
- public User() {
- super();
- }
- public User(String name, String psw) {
- super();
- this.name = name;
- this.psw = psw;
- }
- public String getID() {
- return ID;
- }
- publicvoid setID(String iD) {
- ID = iD;
- }
- public String getName() {
- return name;
- }
- publicvoid setName(String name) {
- this.name = name;
- }
- public String getPsw() {
- return psw;
- }
- publicvoid setPsw(String psw) {
- this.psw = psw;
- }
- }
User實體對應的映射器UserMapper代碼如下:
- package com.deppon.pda.v2.mapper.User;
- import com.deppon.pda.v2.model.basic.User.User;
- publicinterface UserMapper {
- publicvoid insertUser(User user);
- public User getUser(String name);
- }
映射器配置文件UserMapper.xml代碼如下:
- ?xml version="1.0"encoding="UTF-8"?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mappernamespace="com.deppon.pda.v2.mapper.User.UserMapper">
- <insertid="insertUser"parameterType="com.deppon.pda.v2.model.basic.User.User">
- insert into pda2_user(name,age) values(#{name},#{age})
- </insert>
- <selectid="getUser"resultType="com.deppon.pda.v2.model.basic.User.User"parameterType="java.lang.String">
- select * from pda2_user where name=#{name}
- </select>
- <!-- 當使用該Mybatis與Spring整合的時候,該文件必須和相應的Mapper接口文件同名,並在同一路徑下 -->
- </mapper>
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-08/105043p2.htm