mybatis如何读取clob数据 详细过程

转载:http://zhidao.baidu.com/link?url=txmt7GCPoIuDnBxDtFDqdq_0T41kR7NQ7gYVHPtPkfTp6oSA_o14TJasV4U7txzcYkCghP5axWgCq87dHJaO5TAzL1pg4TGVicVBLTDMTnS


例子:

表结构

1
2
3
4
CREATE  TABLE  USERINFO(USERID VARCHAR2(5),   
                          USERNAME VARCHAR2(20),  
                          MEMO CLOB,  
                         constraint  PK_USERINFO   primary  key (USERID));

java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public  class  OracleClobTypeHandlerCallback  implements  TypeHandlerCallback {
     public  void  setParameter(ParameterSetter setter, Object obj)
             throws  SQLException {
         CLOB clob = CLOB.empty_lob();
         clob.setString( 1 , (String)obj);
         setter.setClob(clob);
     }
     public  Object getResult(ResultGetter getter)  throws  SQLException {
         CLOB clob = (CLOB) getter.getClob();
         return  (clob ==  null  || clob.length() ==  0  )?  null  :clob.getSubString(( long ) 1 , ( int )clob.length());
     }
     public  Object valueOf(String param) {
         return  null ;
     }
}

sqlmap配置:

1
2
3
4
5
< resultMap  id = "userResult"  class = "com.prs.application.ehld.sample.common.dto.UserInfoDTO" >
     < result  property = "userID"  column = "USERID"  columnIndex = "1" />
     < result  property = "userName"  column = "USERNAME"  columnIndex = "2" />
     < result  property = "memo"   column = "memo"  jdbcType = "CLOB"  javaType = "java.lang.String"  typeHandler =" OracleClobTypeHandlerCallback "/>
</ resultMap >


1、MyBatis介绍

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。

    iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)

2、CLOB

    SQL CLOB 是内置类型,它将字符大对象 (Character Large Object) 存储为数据库表某一行中的一个列值。默认情况下,驱动程序使用 SQL locator(CLOB) 实现 Clob 对象,这意味着 CLOB 对象包含一个指向 SQL CLOB 数据的逻辑指针而不是数据本身。Clob 对象在它被创建的事务处理期间有效。

3、MyBatis对CLOB类型数据实现增删改查

oracle表结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
create  table  T_USERS  
(  
   ID      NUMBER  not  null ,  
   NAME     VARCHAR2(30),  
   SEX     VARCHAR2(3),  
   BIRS     DATE ,  
   MESSAGE CLOB  
)  
create  sequence  SEQ_T_USERS_ID  
minvalue 1  
maxvalue 99999999  
start  with  1  
increment  by  1  
cache 20;

配置mybatis配置文件UsersMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<? xml  version = "1.0"  encoding = "UTF-8" ?>
<!DOCTYPE mapper
   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN">
 
< mapper  namespace = "examples.mapper.UsersMapper"  >
 
     <!-- Result Map-->
     < resultMap  type = "examples.bean.Users"  id = "BaseResultMap" >
         < result  property = "id"  column = "id"  />
         < result  property = "name"  column = "name"  />
         < result  property = "sex"  column = "sex"  />
         < result  property = "birs"  column = "birs"  jdbcType = "TIMESTAMP" />
         < result  property = "message"  column = "message"  jdbcType = "CLOB" 
             javaType = "java.lang.String"  typeHandler ="examples.service.OracleClobTypeHandler"/>
     </ resultMap >
     
     < sql  id = "Tabel_Name" >
         t_users
     </ sql >
     
     <!-- 表中所有列 -->
     < sql  id = "Base_Column_List"  >
         id,name,sex,birs,message
     </ sql >
 
     <!-- 查询条件 -->
     < sql  id = "Example_Where_Clause" >
         where 1=1
         < trim  suffixOverrides = "," >
             < if  test = "id != null" >
                 and id = #{id}
             </ if >
             < if  test = "name != null and name != ''" >
                 and name like concat(concat('%', '${name}'), '%')
             </ if >
             < if  test = "sex != null and sex != ''" >
                 and sex like concat(concat('%', '${sex}'), '%')
             </ if >
             < if  test = "birs != null" >
                 and birs = #{birs}
             </ if >
             < if  test = "message != null" >
                 and message = #{message}
             </ if >
         </ trim >
     </ sql >
     
     <!-- 2.查询列表 -->
     < select  id = "queryByList"  resultMap = "BaseResultMap"  parameterType = "Object" >
         select
         < include  refid = "Base_Column_List"  />
         from t_users 
         < include  refid = "Example_Where_Clause" />
     </ select >
</ mapper >

Mapper类接口

1
2
3
4
5
6
7
8
9
10
11
package  examples.mapper;
 
import  java.util.List;
 
public  interface  UsersMapper<T> {
     
     public  List<T> queryBySelective(T t);
 
     public  List<T> queryByList(T t);
     
}

类型转换工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package  examples.service;
 
import  java.sql.CallableStatement;
import  java.sql.PreparedStatement;
import  java.sql.ResultSet;
import  java.sql.SQLException;
 
import  oracle.sql.CLOB;
 
import  org.apache.ibatis.type.JdbcType;
import  org.apache.ibatis.type.TypeHandler;
 
public  class  OracleClobTypeHandler  implements  TypeHandler<Object> {
     
     public  Object valueOf(String param) {
         return  null ;
     }
 
     @Override
     public  Object getResult(ResultSet arg0, String arg1)  throws  SQLException {
         CLOB clob = (CLOB) arg0.getClob(arg1);
         return  (clob ==  null  || clob.length() ==  0 ) ?  null  : clob.getSubString(( long 1 , ( int ) clob.length());
     }
 
     @Override
     public  Object getResult(ResultSet arg0,  int  arg1)  throws  SQLException {
         return  null ;
     }
 
     @Override
     public  Object getResult(CallableStatement arg0,  int  arg1)  throws  SQLException {
         return  null ;
     }
 
     @Override
     public  void  setParameter(PreparedStatement arg0,  int  arg1, Object arg2, JdbcType arg3)  throws  SQLException {
         CLOB clob = CLOB.empty_lob();
         clob.setString( 1 , (String) arg2);
         arg0.setClob(arg1, clob);
     }
}

Spring配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< beans  xmlns="
 
     xmlns:xsi="
 
     xmlns:mvc="
     xmlns:tx="
     xsi:schemaLocation="
     default-autowire = "byType" >
 
     <!-- 配置数据源 -->
     < bean  id = "dataSource"  class = "org.springframework.jdbc.datasource.DriverManagerDataSource"
          < property  name = "driverClassName" >< value >oracle.jdbc.driver.OracleDriver</ value ></ property
          < property  name = "url" >< value >jdbc:oracle:thin:@127.0.0.1:1521:pms</ value ></ property
          < property  name = "username" >< value >pms</ value ></ property
          < property  name = "password" >< value >pms</ value ></ property >
     </ bean >
 
     <!-- 配完数据源 和 拥有的 sql映射文件 sqlSessionFactory 也可以访问数据库 和拥有 sql操作能力了 -->
<!-- 
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="dataSource" />
           <property name="configLocation" value="classpath:mybatis-config.xml"/>
     </bean>
  -->
     < bean  id = "sqlSessionFactory"  class = "org.mybatis.spring.SqlSessionFactoryBean" >
         < property  name = "dataSource"  ref = "dataSource"  />
         < property  name = "mapperLocations" >
             < list >
                 < value >classpath:examples/mybatis/oracle/UsersMapper.xml</ value >
             </ list >
         </ property >
     </ bean >
 
     <!-- 通过设置 mapperInterface属性,使接口服务bean 和对应xml文件管理 可以使用其中的sql -->
     < bean  id = "dao"  class = "org.mybatis.spring.mapper.MapperFactoryBean" >
         <!-- 此处等同于 Mybatis 中 ServerDao serverDao = sqlSession.getMapper(ServerDao.class); 指明映射关系 -->
         < property  name = "mapperInterface"  value = "examples.mapper.UsersMapper"  />
         < property  name = "sqlSessionFactory"  ref = "sqlSessionFactory"  />
     </ bean >
</ beans >

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package  examples.service;
 
import  java.text.ParseException;
import  java.text.SimpleDateFormat;
import  java.util.List;
 
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
 
import  examples.bean.Users;
import  examples.mapper.UsersMapper;
 
public  class  TestUsersService {
 
     @SuppressWarnings ( "unchecked" )
     public  static  void  main(String[] args)  throws  ParseException {
             ApplicationContext ac = 
             new  ClassPathXmlApplicationContext( "classpath:/examples/service/spring.xml" );
             UsersMapper<Users> dao = (UsersMapper<Users>)ac.getBean( "dao" );
             
             //查询
              Users nullBean =  new  Users();
             List<Users> list = dao.queryByList(nullBean);
             if (list !=  null ) {
                 for (Users user : list) {
                     System.out.println(user);
                 }
             }
             
     }
 
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值