【Mybatis】Mybatis对clob字段的处理
【前篇】
【纲领】
用jdbc处理clob字段,存入时需要创建Clob对象,然后以setString(1,"...")的方式存入;取出时需要把字段内容先放入Clob对象里,然后要用个char数组将内容一段段读出来,再拼接成文字。一言以蔽之,有点麻烦!
在Mybatis的帮助下,Clob字段可以和varchar2,nvarchar2字段等同视之,一律映射为String类型,其中繁琐工作就由Mybatis代行就好了,便利很多。
【表结构】
create table test03( id number(3), content clob, primary key(id) )
映射的类:
public class Article { private long id; private String content; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
注意Article的content成员和表Test03的字段content的对应关系。
【Mapper类】
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface Test03Mapper { Article findById(long id); int addOne(@Param("id") long id, @Param("content") String content); }
【Mapper.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"> <mapper namespace="Test03Mapper"> <select id="findById" resultType="Article"> select * from test03 where id = #{id} select> <insert id="addOne"> insert into test03 (id,content) values (#{id},#{content}) insert> mapper>
【存入代码】
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; public class MybatisClobWriter { public static void main(String[] args) throws Exception{ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sqlSessionFactory.openSession()) { Test03Mapper mapper = session.getMapper(Test03Mapper.class); String content=readFromFile("c:\\hy\\yylj.txt"); mapper.addOne(2,content); session.commit(); } } static String readFromFile(String path) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8")); String line = null; StringBuilder sb=new StringBuilder(); while( ( line = br.readLine() ) != null ) { sb.append(line+"\n"); } br.close(); return sb.toString(); } }
【取出代码】
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; public class MybaticClobReader { public static void main(String[] args) throws Exception{ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sqlSessionFactory.openSession()) { Test03Mapper mapper = session.getMapper(Test03Mapper.class); Article a = mapper.findById(2); System.out.println(a.getContent()); } } }
【代码下载】
https://files.cnblogs.com/files/heyang78/RestAccessEs220226.rar?t=1645882753
END