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

mybatis如何读取clob数据 详细过程,第1张

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表结构

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配置文件UsersMapperxml

<xml version="10" encoding="UTF-8">

<!DOCTYPE mapper

  PUBLIC "-//mybatisorg//DTD Mapper 30//EN">

<mapper namespace="examplesmapperUsersMapper" >

<!-- Result Map-->

<resultMap type="examplesbeanUsers" 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 = "javalangString"  typeHandler ="examplesserviceOracleClobTypeHandler"/>

</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类接口

package examplesmapper;

import javautilList;

public interface UsersMapper<T> {

public List<T> queryBySelective(T t);

public List<T> queryByList(T t);

}

类型转换工具类

package examplesservice;

import javasqlCallableStatement;

import javasqlPreparedStatement;

import javasqlResultSet;

import javasqlSQLException;

import oraclesqlCLOB;

import orgapacheibatistypeJdbcType;

import orgapacheibatistypeTypeHandler;

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) arg0getClob(arg1);

return (clob == null || cloblength() == 0)  null : clobgetSubString((long) 1, (int) cloblength());

}

@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 = CLOBempty_lob();

clobsetString(1, (String) arg2);

arg0setClob(arg1, clob);

}

}

Spring配置文件

<xml version="10" encoding="UTF-8">

<beans xmlns="

xmlns:xsi="

xmlns:mvc="

xmlns:tx="

xsi:schemaLocation="

default-autowire="byType">

<!-- 配置数据源 -->

<bean id="dataSource" class="orgspringframeworkjdbcdatasourceDriverManagerDataSource">

         <property name="driverClassName"><value>oraclejdbcdriverOracleDriver</value></property> 

         <property name="url"><value>jdbc:oracle:thin:@127001: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="orgmybatisspringSqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

   <property name="configLocation" value="classpath:mybatis-configxml"/>

</bean>

 -->

<bean id="sqlSessionFactory" class="orgmybatisspringSqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="mapperLocations">

<list>

<value>classpath:examples/mybatis/oracle/UsersMapperxml</value>

</list>

</property>

</bean>

<!-- 通过设置 mapperInterface属性,使接口服务bean 和对应xml文件管理 可以使用其中的sql -->

<bean id="dao" class="orgmybatisspringmapperMapperFactoryBean">

<!-- 此处等同于 Mybatis 中 ServerDao serverDao = sqlSessiongetMapper(ServerDaoclass); 指明映射关系 -->

<property name="mapperInterface" value="examplesmapperUsersMapper" />

<property name="sqlSessionFactory" ref="sqlSessionFactory" />

</bean>

</beans>

测试类

package examplesservice;

import javatextParseException;

import javatextSimpleDateFormat;

import javautilList;

import orgspringframeworkcontextApplicationContext;

import orgspringframeworkcontextsupportClassPathXmlApplicationContext;

import examplesbeanUsers;

import examplesmapperUsersMapper;

public class TestUsersService {

@SuppressWarnings("unchecked")

public static void main(String[] args) throws ParseException {

ApplicationContext ac = 

new ClassPathXmlApplicationContext("classpath:/examples/service/springxml");

UsersMapper<Users> dao = (UsersMapper<Users>)acgetBean("dao");

//查询

 Users nullBean = new Users();

List<Users> list = daoqueryByList(nullBean);

if(list != null) {

for(Users user : list) {

Systemoutprintln(user);

}

}

}

}

/

封装Spring ApplicationConext引用, 方便工程通过API获取bean实例

@author bjb-336

/

@Component

public class SpringContextWrapper implements ApplicationContextAware{

private static ApplicationContext appContext;

/

根据beanName 获取bean实例

@param beanName

@return

/

public static Object getBean(String beanName){

Object obj = null;

if (null != appContext){

obj = appContextgetBean(beanName);

}

return obj;

}

/

根据bean名称和类型进行获取Bean的实例

@param beanName

@param clsType

@return

/

public static <T> T getBean(String beanName, Class<T> clsType){

T obj = null;

if (null != appContext){

obj = appContextgetBean(beanName, clsType);

}

return obj;

}

/

根据类型进行获取Bean的实例

@param clsType

@return

/

public static <T> T getBean(Class<T> clsType){

T obj = null;

if (null != appContext){

obj = appContextgetBean(clsType);

}

return obj;

}

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

SpringContextWrapperappContext = applicationContext;

}

}

在平时的工作中有时候是需要在配置文件中配置全局变量的,因为这些东西是不会变的,并且每个mapper都传参的话也显得有点繁琐,还好mybatis本身是支持全局变量的,今天工作中用到了,记录一下。

先在实例化sqlSessionFactory的时候添加上mybatis-configurationxml的配置文件

1

2

3

4

5

<bean id="sqlSessionFactory" class="orgmybatisspringSqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="mapperLocations" value="classpath:mapping/xml" />

<property name="configLocation" value="classpath:mybatis-configurationxml" />

</bean>

 之后在mybatis-configuration配置文件中添加全局变量

1

2

3

4

5

6

7

8

9

<xml version="10" encoding="UTF-8">

<!DOCTYPE configuration

PUBLIC "-//mybatisorg//DTD Config 30//EN"

">

以上就是关于mybatis如何读取clob数据 详细过程全部的内容,包括:mybatis如何读取clob数据 详细过程、工具类中怎么注入service,spring mvc +mybatiss、mybatis怎么获得全局变量等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9630581.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存