mybatis学习之CLOB、BLOB处理及多参数方法映射
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处理及多参数方法映射的更多相关文章
- mybatis学习之路----批量更新数据两种方法效率对比
原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...
- Mybatis学习笔记15 - 两个内置参数_parameter和_databaseId
两个内置参数:除了方法传递过来的参数可以被用来判断,取值外,mybatis默认还有两个内置参数: _parameter:代表整个参数 单个参数:_parameter就代表这个单个参数 多个参数:参数会 ...
- [原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持
前言 今天在开发练习项目时遇到两个mybatis使用问题 第一个问题是mapper方法参数问题,在参数大于一个时,mybatis不会自动识别参数命名 第二个问题是Pojo中使用Joda DateTim ...
- nodejs 学习六 express 三种查询url参数方法
req.param() 是被废弃的api req.params 俗点:取带冒号的参数 req.body 可以肯定的一点是req.body一定是post请求,express里依赖的中间件必须有bodyP ...
- 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 ...
- mybatis 处理CLOB/BLOB类型数据
BLOB和CLOB都是大字段类型. BLOB是按二进制来存储的,而CLOB是可以直接存储文字的. 通常像图片.文件.音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去.文章或者是较长的文字 ...
- MyBatis学习 之 四、MyBatis配置文件
目录(?)[-] 四MyBatis主配置文件 properties属性 settings设置 typeAliases类型别名 typeHandlers类型句柄 ObjectFactory对象工厂 pl ...
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...
- MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射
在上一章中我们学习了<MyBatis学习总结(一)——ORM概要与MyBatis快速起步>,这一章主要是介绍MyBatis核心配置文件.使用接口+XML实现完整数据访问.输入参数映射与输出 ...
随机推荐
- [转]解读Unity中的CG编写Shader系列5——理论知识
经过前面的系列文章中的三个例子,尽管代码简单,但是我想应该还有些地方没有100%弄明白,我们现在得回过头来补充一些必备的数学.图形学知识 1.图形管道第一个例子中我有提到顶点着色和片段着色在整个图形绘 ...
- 配合前端开发,调试前端页面bug
同事开发的H5页面 在iOS10.1的手机上会有bug 先下载ios10.1的模拟器,安装到xcode上,注意给电脑腾出足够的空间 https://stackoverflow.com/questio ...
- [ActionScript 3.0] 动态链接库
很多时候,我们为了项目和程序结构更加清晰,需要将发布好的swf放到一个固定的地方供主文件引用,这时就会出现发布好的swf所用的as类路径发生改变,为避免这个问题,需要用到动态链接库,以下做一个简单例子 ...
- JPush Android 推送如何区分开发、生产环境
我们 Android 开发者在使用极光推送时可能会发现,在通过官方控制台进行推送时.只有 iOS 区分了开发和生产环境,而 Android 则没有.但实际开发中又确实经常需要针对开发和生产环境分别来推 ...
- Vim寄存器
(cut && copy && paste) VS ( delete && yank && put ) "" 无名寄 ...
- xml约束技术之dtd
DTD(文档类型定义)的作用是定义 XML 文档的合法构建模块.这篇文章作简单介绍下DTD的用法.想学习完整的请点击下面w3c的教程. 1.DTD官方教程 ##2.xml约束技术: DTD约束:语法相 ...
- python 强大的工具
numpy Python科学计算的基础包 安装工具 pip3 install numpy pandas包含了高级的数据结构和操作工具,它们使得Python数据分析更加快速和容易. 安装工具 pip3 ...
- leetcode-198-House Robber(动态规划)
题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- 963 AlvinZH打怪刷经验(背包DP大作战R)
963 AlvinZH打怪刷经验 思路 这不是一道普通的01背包题.大家仔细观察数据的范围,可以发现如果按常理来的话,背包容量特别大,你也会TLE. 方法一:考虑01背包的一个常数优化----作用甚微 ...
- 求一个n元一次方程的解,Gauss消元
求一个n元一次方程的解,Gauss消元 const Matrix=require('./Matrix.js') /*Gauss 消元 传入一个矩阵,传出结果 */ function Gauss(mat ...