码迷,mamicode.com
首页 > 其他好文 > 详细

小峰mybatis(1) 处理clob,blob等。。

时间:2017-04-07 10:06:27      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:sources   UI   zhang   图片   param   output   技术   etag   nbsp   

一、mybatis处理CLOB、BLOB类型数据

CLOB:大文本类型;小说啊等大文本的;对应数据库类型不一致,有long等;
BLOB:二进制的,图片;电影、音乐等二进制的;
在mysql中:
blob:
longblob:存储的东西比blob更大;
longtext:存储大文本类型的;
 
新建t_studeng表:
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
View Code

2)mybatis_config.xml:

技术分享
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <properties resource="jdbc.properties"/>
 7     <!-- 别名 -->
 8     <typeAliases>
 9         <package name="com.cy.model"/>
10     </typeAliases>
11     
12     <environments default="development">
13         <environment id="development">
14             <transactionManager type="JDBC" />
15                 <dataSource type="POOLED">
16                     <property name="driver" value="${jdbc.driverClassName}" />
17                     <property name="url" value="${jdbc.url}" />
18                     <property name="username" value="${jdbc.username}" />
19                     <property name="password" value="${jdbc.password}" />
20                 </dataSource>
21         </environment>
22         <environment id="test">
23             <transactionManager type="JDBC" />
24             <dataSource type="POOLED">
25                 <property name="driver" value="${jdbc.driverClassName}" />
26                 <property name="url" value="${jdbc.url}" />
27                 <property name="username" value="${jdbc.username}" />
28                 <property name="password" value="${jdbc.password}" />
29             </dataSource>
30         </environment>
31     </environments>
32     
33     <mappers>
34         <package name="com.cy.mapper"/>
35     </mappers>
36 </configuration>
View Code

3)Student.java model:

技术分享
 1 package com.cy.model;
 2 
 3 public class Student {
 4     private Integer id;
 5     private String name;
 6     private Integer age;
 7     private byte[] pic;
 8     private String remark;
 9     
10     public Integer getId() {
11         return id;
12     }
13     public void setId(Integer id) {
14         this.id = id;
15     }
16     public String getName() {
17         return name;
18     }
19     public void setName(String name) {
20         this.name = name;
21     }
22     public Integer getAge() {
23         return age;
24     }
25     public void setAge(Integer age) {
26         this.age = age;
27     }
28     public byte[] getPic() {
29         return pic;
30     }
31     public void setPic(byte[] pic) {
32         this.pic = pic;
33     }
34     public String getRemark() {
35         return remark;
36     }
37     public void setRemark(String remark) {
38         this.remark = remark;
39     }
40     @Override
41     public String toString() {
42         return "Student [id=" + id + ", name=" + name + ", age=" + age
43                 + ", remark=" + remark + "]";
44     }
45     
46     
47     
48 }
View Code

4)获取sqlSession:SqlSessionFactoryUtil.java:

技术分享
 1 package com.cy.util;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 public class SqlSessionFactoryUtil {
12     private static SqlSessionFactory sqlSessionFactory;
13     
14     public static SqlSessionFactory getSqlSessionFactory(){
15         if(sqlSessionFactory==null){
16             InputStream inputStream=null;
17             try {
18                 inputStream=Resources.getResourceAsStream("mybatis_config.xml");
19                 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
20             } catch (IOException e) {
21                 e.printStackTrace();
22             }
23         }
24         
25         return sqlSessionFactory;
26     }
27     
28     public static SqlSession openSession(){
29         return getSqlSessionFactory().openSession();
30     }
31 }
View Code

5)测试代码:StudentTest.java: service层:

 1 package com.cy.service;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 
 9 import org.apache.ibatis.session.SqlSession;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import com.cy.mapper.StudentMapper;
15 import com.cy.model.Student;
16 import com.cy.util.SqlSessionFactoryUtil;
17 
18 public class StudentTest {
19     private SqlSession sqlSession=null;
20     private StudentMapper studentMapper=null;
21     
22     @Before
23     public void setUp() throws Exception {
24         sqlSession=SqlSessionFactoryUtil.openSession();
25         studentMapper=sqlSession.getMapper(StudentMapper.class);
26     }
27     
28     @After
29     public void tearDown() throws Exception {
30         sqlSession.close();
31     }
32     
33     @Test
34     public void testInsertStudent(){
35         Student student=new Student();
36         student.setName("zhangsan");
37         student.setAge(14);
38         student.setRemark("很长的文本...");
39         byte []pic=null;
40         try{
41             File file=new File("I://shoot.png");
42             InputStream inputStream=new FileInputStream(file);
43             pic=new byte[inputStream.available()];
44             inputStream.read(pic);
45             inputStream.close();
46         }catch(Exception e){
47             e.printStackTrace();
48         }
49         student.setPic(pic);
50         studentMapper.insertStudent(student);
51         sqlSession.commit();
52     }
53     
54     @Test
55     public void testGetStudentById(){
56         Student student=studentMapper.getStudentById(1);
57         System.out.println(student);
58         byte []pic=student.getPic();
59         try{
60             File file=new File("i://boy.png");
61             OutputStream outputStream=new FileOutputStream(file);
62             outputStream.write(pic);
63             outputStream.close();
64         }catch(Exception e){
65             e.printStackTrace();
66         }
67     }
68     
69     
70     
71     
72 }

执行testInsertStudent方法后,查看数据库中插入情况:

技术分享

执行testGetStudentById获取这个记录,将pic保存到I盘 boy.png,已将数据库中的图片,以流的形式写入到I盘中;

6)StudentMapper.java:

技术分享
 1 package com.cy.mapper;
 2 
 3 import com.cy.model.Student;
 4 
 5 public interface StudentMapper {
 6     
 7     //插入
 8     public int insertStudent(Student student);
 9     
10     //根据id获取student
11     public Student getStudentById(Integer id);    
12 
13 }
View Code

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配置文件:

1 log4j.rootLogger=info,appender1,appender2
2 
3 log4j.appender.appender1=org.apache.log4j.ConsoleAppender 
4 
5 log4j.appender.appender2=org.apache.log4j.FileAppender 
6 log4j.appender.appender2.File=I:/logFile.txt
7  
8 log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
9 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等。。

标签:sources   UI   zhang   图片   param   output   技术   etag   nbsp   

原文地址:http://www.cnblogs.com/tenWood/p/6676293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!