mybatis学习之CLOB、BLOB处理及多参数方法映射
2024-08-31 13:54:36
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);
}
}
最新文章
- JavaWeb学习笔记——DAO设计模式
- spring官方案例程序
- 进程外session
- 兰勃特投影C#实现
- PHP: 深入pack/unpack 字节序
- Good Bye 2015 A
- iOS开发总结-UITableView 自定义cell和动态计算cell的高度
- [Machine Learning] 梯度下降(BGD)、随机梯度下降(SGD)、Mini-batch Gradient Descent、带Mini-batch的SGD
- Asp.Net请求响应过程
- iosOC不可变字典和可变字典
- Windows下检测文件名大小写是否匹配
- Js中的subStr和subString的区别
- 2020考研-必须了解的干货";极限微分和你说的悄悄话";
- experiment 3
- 省市区三级联动(附j全国省市区json文件)
- git rebase修改历史提交内容
- centos6.5安装jdk(解压tar.gz)
- Ubuntu下eclipse中运行Hadoop时所需要的JRE与JDK的搭配
- pandas函数get_dummies的坑
- RecyclerView拖拽排序;