小峰mybatis(1) 处理clob,blob等。。
一、mybatis处理CLOB、BLOB类型数据
create table t_student(
id int primary key auto_increment,
name varchar(20),
age int,
pic longblob,
remark longtext
)
项目结构:
1)jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_demo
jdbc.username=root
jdbc.password=123456
2)mybatis_config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"/>
<!-- 别名 -->
<typeAliases>
<package name="com.cy.model"/>
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> <mappers>
<package name="com.cy.mapper"/>
</mappers>
</configuration>
3)Student.java model:
package com.cy.model; public class Student {
private Integer id;
private String name;
private Integer age;
private byte[] pic;
private String remark; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public byte[] getPic() {
return pic;
}
public void setPic(byte[] pic) {
this.pic = pic;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", remark=" + remark + "]";
} }
4)获取sqlSession:SqlSessionFactoryUtil.java:
package com.cy.util; import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactoryUtil {
private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory(){
if(sqlSessionFactory==null){
InputStream inputStream=null;
try {
inputStream=Resources.getResourceAsStream("mybatis_config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
} return sqlSessionFactory;
} public static SqlSession openSession(){
return getSqlSessionFactory().openSession();
}
}
5)测试代码:StudentTest.java: service层:
package com.cy.service; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.cy.mapper.StudentMapper;
import com.cy.model.Student;
import com.cy.util.SqlSessionFactoryUtil; public class StudentTest {
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null; @Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
} @After
public void tearDown() throws Exception {
sqlSession.close();
} @Test
public void testInsertStudent(){
Student student=new Student();
student.setName("zhangsan");
student.setAge(14);
student.setRemark("很长的文本...");
byte []pic=null;
try{
File file=new File("I://shoot.png");
InputStream inputStream=new FileInputStream(file);
pic=new byte[inputStream.available()];
inputStream.read(pic);
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
student.setPic(pic);
studentMapper.insertStudent(student);
sqlSession.commit();
} @Test
public void testGetStudentById(){
Student student=studentMapper.getStudentById(1);
System.out.println(student);
byte []pic=student.getPic();
try{
File file=new File("i://boy.png");
OutputStream outputStream=new FileOutputStream(file);
outputStream.write(pic);
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
} }
执行testInsertStudent方法后,查看数据库中插入情况:
执行testGetStudentById获取这个记录,将pic保存到I盘 boy.png,已将数据库中的图片,以流的形式写入到I盘中;
6)StudentMapper.java:
package com.cy.mapper; import com.cy.model.Student; public interface StudentMapper { //插入
public int insertStudent(Student student); //根据id获取student
public Student getStudentById(Integer id); }
7)StudentMapper.xml mapper映射文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cy.mapper.StudentMapper"> <insert id="insertStudent" parameterType="Student">
insert into t_student values(null,#{name},#{age},#{pic},#{remark});
</insert> <select id="getStudentById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>
</mapper>
二、项目中使用log4j:
在项目中加入log4j的jar包,以及配置log4j.properties:
1)log4j.properties配置文件:
log4j.rootLogger=info,appender1,appender2 log4j.appender.appender1=org.apache.log4j.ConsoleAppender log4j.appender.appender2=org.apache.log4j.FileAppender
log4j.appender.appender2.File=I:/logFile.txt log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout
而且,发现配置文件名必须为log4j.properties;
上面的输入到两个地方:
console;
I盘下面的logFile.txt;
2)测试代码:StudentTest.java:
package com.cy.service; import org.apache.log4j.Logger;
import org.junit.Test; public class StudentTest { private static Logger logger = Logger.getLogger(StudentTest.class); @Test
public void testLogger(){
logger.info("测试log4j.....");
} @Test
public void testOtherLogger(){
logger.info("测试other log4j.....");
}
}
运行后console输出:这样信息:
[main] INFO com.cy.service.StudentTest - 测试other log4j.....
查看I盘下面logFile.txt:
小峰mybatis(1) 处理clob,blob等。。的更多相关文章
- 小峰mybatis(3)mybatis分页和缓存
一.mybatis分页-逻辑分页和物理分页: 逻辑分页: mybatis内置的分页是逻辑分页:数据库里有100条数据,要每页显示10条,mybatis先把100条数据取出来,放到内存里,从内存里取10 ...
- 小峰mybatis(4)mybatis使用注解配置sql映射器
主流开发还是使用xml来配置:使用注解配置比较快,但是不支持所有功能:有些功能还是得用配置文件: 一.基本映射语句: @Inert @Update @Delete @Select 二.结果集映射语句 ...
- 小峰mybatis(2)mybatis传入多个参数等..
一.mybatis传入多个参数: 前面讲传入多个参数都是使用map,hashmap:key value的形式:-- 项目中开发都建议使用map传参: 比如现在通过两个参数,name和age来查询: 通 ...
- 小峰mybatis(5)mybatis使用注解配置sql映射器--动态sql
一.使用注解配置映射器 动态sql: 用的并不是很多,了解下: Student.java 实体bean: package com.cy.model; public class Student{ pri ...
- 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之Clob & Blob
1. 表结构 1.1 在Mysql中的数据类型,longblob --> blob, longtext --> clob 2. 配置文件, 请参考 myBatis之入门示例 3. L ...
- mybatis学习之CLOB、BLOB处理及多参数方法映射
CLOB数据mysql对应数据类型为longtext.BLOB类型为longblob: model实体: ... private Integer id; private String name; pr ...
- Mybatis对MySQL中BLOB字段的读取
1.在sqlMapConfig中,定义一个typeHandlers <typeHandlers> <typeHandler jdbcType="BLOB" jav ...
随机推荐
- vue+ajax+bootstrap+python实现增删改
http://www.cnblogs.com/xwwin/p/5816527.html script src= " http://code.jquery.com/jquery.min.js ...
- hdu 1003 Max Sum (动态规划)
转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...
- python学习:变量与字符串
counter = # 赋值整型变量 miles = 1000.0 # 浮点型 name = "John" # 字符串 print counter print miles prin ...
- STL标准库-迭代器
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 本节主要介绍STL六大部件中的Iterators迭代器. 在语言方面讲,容器是一个class template, 算法是一个仿函 ...
- workflow
一:项目进程 1研发部设计demo (选择方案--方案确认--设计电路图layout--固件开发--软件开发-打样板--调试demo--可靠性分析--稳定性检测) 2进入ES阶段(engineer s ...
- freeradius的https查询功能
一.服务器要求 Radius服务器:centos6.6.ip.hostname.selinux disabled.stop iptables freeradius版本:3.0.12 二.源码安装fr ...
- JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
JavaWeb中监听器+过滤器+拦截器区别.配置和实际应用 1.前沿上一篇文章提到在web.xml中各个元素的执行顺序是这样的,context-param-->listener-->fil ...
- 【linux基础】查看硬盘位置信息
nvidia@tegra-ubuntu:/media/nvidia/Elements/data$
- gettimeofday的使用
前言 好像只能在linux平台使用,在windows平台不能使用.... #include <sys/time.h> long cur_tp, cost_tp, tmp_tp; struc ...
- Spring boot 2.1.0 -- swagger2 整合
依赖版本信息 Spring boot 2.1.0.RELEASE swagger2 2.7.0 1. mvn 配置 pom.xml 包引入 <!--swagger2依赖--> <d ...