CLOB数据mysql对应数据类型为longtext、BLOB类型为longblob:

model实体:

...
private Integer id;
private String name;
private int age; private byte[] pic; // 映射blob
private String remark; // 映射longtext
...

1、blob、clob数据插入:

<insert id="insertStudent" parameterType="Student">
insert into t_student values(null,#{name},#{age},#{pic},#{remark})
</insert>

对应Dao接口:

/**
* 插入学生
* @param student
* @return
*/
public int insertStudent(Student student);

junit测试:

@Test
public void testInsert() throws Exception {
logger.info("新增学生");
Student student = new Student();
student.setAge(12);
student.setName("晁州");
student.setRemark("长文本");
byte[] pic = null;
try {
File file = new File("c://test.png");
InputStream is = new FileInputStream(file);
pic = new byte[is.available()];
is.read(pic);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
student.setPic(pic);
studentDao.insertStudent(student);
sqlSession.commit();
}

2、blob、clob数据查询(blob数据查询出来对应java的byte[]):

<select id="getStudentById" parameterType="Integer" resultType="Student">
select * from t_student where id = #{id}
</select>

Dao接口部分:

@Test
public void testGet() throws Exception {
logger.info("查询学生");
Student student = studentDao.getStudentById(34);
System.out.println(student);
byte[] pic = student.getPic();
try {
File file = new File("c://output.png");
OutputStream os = new FileOutputStream(file);
os.write(pic);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

3、mybatis的多参数查询:

Dao接口部分:

/**
* 根据姓名和年龄进行查询(mybatis多参数查询)
* @param name
* @param age
* @return
*/
public List<Student> getStudentsBy2Args(String name,Integer age);

对应mapper映射:

<select id="getStudentsBy2Args" resultMap="StudentResult">
select * from t_student where name like #{param1} and age = #{param2} <!-- 与方法的参数顺序一一对应 -->
</select>

junit测试:

@Test
public void testGetStudentsBy2Args() throws Exception {
List<Student> students = studentDao.getStudentsBy2Args("%晁%", 24);
for (Student student : students) {
System.out.println("####### "+student);
}
}

mybatis学习之CLOB、BLOB处理及多参数方法映射的更多相关文章

  1. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...

  2. Mybatis学习笔记15 - 两个内置参数_parameter和_databaseId

    两个内置参数:除了方法传递过来的参数可以被用来判断,取值外,mybatis默认还有两个内置参数: _parameter:代表整个参数 单个参数:_parameter就代表这个单个参数 多个参数:参数会 ...

  3. [原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持

    前言 今天在开发练习项目时遇到两个mybatis使用问题 第一个问题是mapper方法参数问题,在参数大于一个时,mybatis不会自动识别参数命名 第二个问题是Pojo中使用Joda DateTim ...

  4. nodejs 学习六 express 三种查询url参数方法

    req.param() 是被废弃的api req.params 俗点:取带冒号的参数 req.body 可以肯定的一点是req.body一定是post请求,express里依赖的中间件必须有bodyP ...

  5. MyBatis(3.2.3) - Handling the CLOB/BLOB types

    MyBatis provides built-in support for mapping CLOB/BLOB type columns. Assume we have the following t ...

  6. mybatis 处理CLOB/BLOB类型数据

    BLOB和CLOB都是大字段类型. BLOB是按二进制来存储的,而CLOB是可以直接存储文字的. 通常像图片.文件.音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去.文章或者是较长的文字 ...

  7. MyBatis学习 之 四、MyBatis配置文件

    目录(?)[-] 四MyBatis主配置文件 properties属性 settings设置 typeAliases类型别名 typeHandlers类型句柄 ObjectFactory对象工厂 pl ...

  8. MyBatis学习 之 二、SQL语句映射文件(1)resultMap

    目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...

  9. MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射

    在上一章中我们学习了<MyBatis学习总结(一)——ORM概要与MyBatis快速起步>,这一章主要是介绍MyBatis核心配置文件.使用接口+XML实现完整数据访问.输入参数映射与输出 ...

随机推荐

  1. Winfrom 桌面弹窗拦截 关闭进程简易程序 源代码下载

    ***********************2019 2.7更新 v 2.0*************************************************** 程序 源代码 交互 ...

  2. alicemq 方便的RabbitMQ 可视化工具

    尽管RabbitMQ 自带一个管理插件,但是还不是那么强大,alicemq 是一个方便强大的可视化工具 rabbitmq 环境准备 docker-compose 文件 version: "3 ...

  3. 一个标准的AJAX请求

    这是一个标准的ajax请求: $.ajax({ type:"post", url:basePath+"/resourcePush/operationLog", ...

  4. Python中执行变量而非字符串

    Python中执行变量而非字符串 设想这样的场景,你需要大型项目的开发.但是项目的开发第一步是啥? 当然是import导入了. ...but............ 默认 import 后面跟着字符串 ...

  5. 虚拟机安装CentOS,网络配置

    CentOS6和CentOS7,基础配置差不多. 安装CentOs6,过程:略: 安装完成后,系统默认启用动态ip,每次重启主机IP可能就会变化,搭配Xftp和Xshell工具会很难受,因此设置静态I ...

  6. windows 安装python问题总结

    一.安装支持包 很多二进制包 NumPy-1.9+MKL 以及 Microsoft Visual C++ 2008 (x64, x86, and SP1 for CPython 2.6 and 2.7 ...

  7. 【Alpha】Scrum Meeting 0&1

    前言 第0次会议和第1次会议分别在4月1日和4月2日21:00由PM在大运村一公寓3层召开. 第0次时长50min,主要明确了接下来的任务,对工作进行了分配. 第1次会议时长20min,调研了当日工作 ...

  8. leetcode 53 最大子序列之和(动态规划)

    思路:nums为给定的数组,动态规划: 设 一维数组:dp[i] 表示 以第i个元素为结尾的一段最大子序和. 1)若dp[i-1]小于0,则dp[i]加上前面的任意长度的序列和都会小于nums[i], ...

  9. Linux下安装渗透测试框架Metasploit

    我们先来说一种方法,直接从github来下载: git clone --depth=1 git://github.com/rapid7/metasploit-framework metasploit ...

  10. 命令提示符下的WQL

    WQL就是英文WMI Query Language的缩写,就是说wmic这个命令支持简单的一些SQL查询,我们以实例来讲解他的部分用法,这个命令过于强大,因此以下只是该命令的冰山一角. 列出本地连接的 ...