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);
}
}

最新文章

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

热门文章

  1. OCP 052最新题库还有答案收集整理-第26题
  2. 实用的bash别名和函数
  3. WordPress翻译更新失败解决方法
  4. oracle 创建临时表空间/表空间,用户及授权
  5. Kettle入门及性能优化FAQ
  6. Linux的vim和vi编辑器
  7. PHP 数组与CSV文件互转
  8. Laravel 控制器 Controller 传值到 视图 View 的几种方法总结
  9. iview2.0 bug之+8 区的 DatePicker
  10. Python学习 day13