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. CYJian的水题大赛2 解题报告

    这场比赛是前几天洛谷上 暮雪﹃紛紛dalao的个人公开赛,当时基本上都在水暴力分......也没有好好写正解(可能除了T1) 过了几天颓废的日子之后,本蒟蒻觉得应该卓越一下了qwq,所以就打算写一个解 ...

  2. Django-04模板层

    你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...

  3. 在linux 创建网络会话和绑定两块网卡

    1. 如果我们在公司网络中要手动指定网络的IP地址,当我们回到家里则是使用DHCP(动态主机配置协议)自动分配IP地址.这就有点麻烦了,因为要频繁的修改IP地址,所以接下来我们来创建网络会话----- ...

  4. jqury表单验证

    结合天天生鲜的用户注册页面,学习验证表单js register.js--表单验证源码 $(function(){ var error_name = false; var error_password ...

  5. java集合类学习笔记之LinkedHashMap

    1.简述 LinkedHashMap是HashMap的子类,他们最大的不同是,HashMap内部维护的是一个单向的链表数组,而LinkedHashMap内部维护的是一个双向的链表数组.HashMap是 ...

  6. 在Grafana中可视化Jenkins管道结果

    这次我描述了一些稍微轻松的话题,与之前的一些帖子相比.就个人而言,我认为Grafana是一个非常酷的工具,用于可视化任何时间轴数据.事实证明,使用InfluxDB插件存储和可视化Jenkins构建结果 ...

  7. FreeRTOS-06任务运行时间信息统计

    根据正点原子FreeRTOS视频整理 单片机:STM32F207VC FreeRTOS源码版本:v10.0.1 * 1. 要使用vTaskGetRunTimeStats()函数,需满足以下条件: * ...

  8. List<Type> 随机

    public List<T> GetRandomList<T>(List<T> inputList){ //Copy to a array T[] copyArra ...

  9. Java 高并发解决方案(电商的秒杀和抢购)

    转载:https://blog.csdn.net/icangfeng/article/details/81201575 电商的秒杀和抢购,对我们来说,都不是一个陌生的东西.然而,从技术的角度来说,这对 ...

  10. portmap安装