create tablet_student(
idint primary keyauto_increment,
namevarchar(20),
ageint,
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 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
View Code
3)Student.java model:
1 packagecom.cy.model;2
3 public classStudent {4 privateInteger id;5 privateString name;6 privateInteger age;7 private byte[] pic;8 privateString remark;9
10 publicInteger getId() {11 returnid;12 }13 public voidsetId(Integer id) {14 this.id =id;15 }16 publicString getName() {17 returnname;18 }19 public voidsetName(String name) {20 this.name =name;21 }22 publicInteger getAge() {23 returnage;24 }25 public voidsetAge(Integer age) {26 this.age =age;27 }28 public byte[] getPic() {29 returnpic;30 }31 public void setPic(byte[] pic) {32 this.pic =pic;33 }34 publicString getRemark() {35 returnremark;36 }37 public voidsetRemark(String remark) {38 this.remark =remark;39 }40 @Override41 publicString toString() {42 return "Student [id=" + id + ", name=" + name + ", age=" +age43 + ", remark=" + remark + "]";44 }45
46
47
48 }
View Code
4)获取sqlSession:SqlSessionFactoryUtil.java:
1 packagecom.cy.util;2
3 importjava.io.IOException;4 importjava.io.InputStream;5
6 importorg.apache.ibatis.io.Resources;7 importorg.apache.ibatis.session.SqlSession;8 importorg.apache.ibatis.session.SqlSessionFactory;9 importorg.apache.ibatis.session.SqlSessionFactoryBuilder;10
11 public classSqlSessionFactoryUtil {12 private staticSqlSessionFactory sqlSessionFactory;13
14 public staticSqlSessionFactory getSqlSessionFactory(){15 if(sqlSessionFactory==null){16 InputStream inputStream=null;17 try{18 inputStream=Resources.getResourceAsStream("mybatis_config.xml");19 sqlSessionFactory = newSqlSessionFactoryBuilder().build(inputStream);20 } catch(IOException e) {21 e.printStackTrace();22 }23 }24
25 returnsqlSessionFactory;26 }27
28 public staticSqlSession openSession(){29 returngetSqlSessionFactory().openSession();30 }31 }
View Code
5)测试代码:StudentTest.java: service层:
1 packagecom.cy.service;2
3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileOutputStream;6 importjava.io.InputStream;7 importjava.io.OutputStream;8
9 importorg.apache.ibatis.session.SqlSession;10 importorg.junit.After;11 importorg.junit.Before;12 importorg.junit.Test;13
14 importcom.cy.mapper.StudentMapper;15 importcom.cy.model.Student;16 importcom.cy.util.SqlSessionFactoryUtil;17
18 public classStudentTest {19 private SqlSession sqlSession=null;20 private StudentMapper studentMapper=null;21
22 @Before23 public void setUp() throwsException {24 sqlSession=SqlSessionFactoryUtil.openSession();25 studentMapper=sqlSession.getMapper(StudentMapper.class);26 }27
28 @After29 public void tearDown() throwsException {30 sqlSession.close();31 }32
33 @Test34 public voidtestInsertStudent(){35 Student student=newStudent();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=newFileInputStream(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 @Test55 public voidtestGetStudentById(){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=newFileOutputStream(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 packagecom.cy.mapper;2
3 importcom.cy.model.Student;4
5 public interfaceStudentMapper {6
7 //插入
8 public intinsertStudent(Student student);9
10 //根据id获取student
11 publicStudent getStudentById(Integer id);12
13 }
View Code
7)StudentMapper.xml mapper映射文件:
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into t_student values(null,#{name},#{age},#{pic},#{remark});
select * from t_student where id=#{id}
二、项目中使用log4j:
在项目中加入log4j的jar包,以及配置log4j.properties:
1)log4j.properties配置文件:
1 log4j.rootLogger=info,appender1,appender22
3 log4j.appender.appender1=org.apache.log4j.ConsoleAppender4
5 log4j.appender.appender2=org.apache.log4j.FileAppender6 log4j.appender.appender2.File=I:/logFile.txt7
8 log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout9 log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout
而且,发现配置文件名必须为log4j.properties;
上面的输入到两个地方:
console;
I盘下面的logFile.txt;
2)测试代码:StudentTest.java:
packagecom.cy.service;importorg.apache.log4j.Logger;importorg.junit.Test;public classStudentTest {private static Logger logger = Logger.getLogger(StudentTest.class);
@Testpublic voidtestLogger(){
logger.info("测试log4j.....");
}
@Testpublic voidtestOtherLogger(){
logger.info("测试other log4j.....");
}
}
运行后console输出:这样信息:
[main] INFO com.cy.service.StudentTest - 测试other log4j.....
查看I盘下面logFile.txt: