Spring Boot学习篇(四)
1 BLOB(二进制大类型)
1.1 创建tb_blob表,其sql语句如下所示
CREATE TABLE tb_blob(
id number primary key ,
fname VARCHAR2( 50 ) NOT NULL ,
f blob
)
1.2 在entity包下面创建TbBlob实体类
package com. zlz. entity ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import javax. persistence. Id ;
import javax. persistence. Table ;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TbBlob {
@Id
private Integer id;
private String fname;
private byte [ ] f;
}
1.3 在com包.zlz包.mapper包下创建TbBlobMapper接口
package com. zlz. mapper ;
import com. zlz. entity. TbBlob ;
import tk. mybatis. mapper. common. Mapper ;
public interface TbBlobMapper extends Mapper < TbBlob > {
int add ( TbBlob b) ;
}
1.4 在mappers文件夹下创建TbBlobMapper.xml
1.4.1 创建后的目录结构如下所示
1.4.2 TbBlobMapper.xml内容如下
<?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.zlz.mapper.TbBlobMapper" >
< insert id = " add" >
insert into tb_blob values(#{id},#{fname},
#{f,typeHandler=org.apache.ibatis.type.BlobTypeHandler})
</ insert>
</ mapper>
1.5 两种使用场景
1.5.1 将图片写入到数据库中
a 示例代码
@Autowired
TbBlobMapper tbBlobMapper;
@Test
public void blobTest ( ) throws Exception {
InputStream inputStream= new FileInputStream ( "C:\\Users\\SSS翱翔万里\\Pictures\\Camera Roll\\1.jpg" ) ;
byte [ ] bs= new byte [ inputStream. available ( ) ] ;
inputStream. read ( bs) ;
TbBlob tbBlob = new TbBlob ( 10 , "a.png" , bs) ;
tbBlobMapper. add ( tbBlob) ;
}
b.控制台运行截图
c 数据库内容截图1
c.数据库内容截图2(点击了小圆点后出现的图片)
1.5.2 将图片从数据库从读出到指定目录中
a 示例代码
@Test
public void blobRead ( ) throws Exception {
TbBlob tbBlob = tbBlobMapper. selectByPrimaryKey ( 10 ) ;
OutputStream os = new FileOutputStream ( "d:\\" + tbBlob. getFname ( ) ) ;
os. write ( tbBlob. getF ( ) ) ;
os. close ( ) ;
}
b.控制台运行截图
c 最终效果图
2 CLOB(文本类型)
2.1 创建tb_clob表,其sql语句如下所示
CREATE TABLE tb_clob(
id number primary key ,
fname VARCHAR2( 50 ) NOT NULL ,
f clob
)
2.2 在entity包下面创建TbClob实体类
package com. zlz. entity ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import javax. persistence. Id ;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TbClob {
@Id
private Integer id;
private String fname;
private String f;
}
2.3 在com包.zlz包.mapper包下创建TbClobMapper接口
package com. zlz. mapper ;
import com. zlz. entity. TbBlob ;
import com. zlz. entity. TbClob ;
import tk. mybatis. mapper. common. Mapper ;
public interface TbClobMapper extends Mapper < TbClob > {
}
2.4 两种使用场景
2.4.1 将txt文本写入到数据库中
a 示例代码
@Autowired
TbClobMapper tbClobMapper;
@Test
public void clob ( ) throws Exception {
InputStream inputStream= new FileInputStream ( "d:\\clobTest.txt" ) ;
Reader reader= new InputStreamReader ( inputStream) ;
BufferedReader bufferedReader= new BufferedReader ( reader) ;
StringBuilder stringBuffer = new StringBuilder ( ) ;
String s= null ;
while ( ( s= bufferedReader. readLine ( ) ) != null ) {
stringBuffer. append ( s+ "\r\n" ) ;
}
TbClob tbClob = new TbClob ( 10 , "test.txt" , stringBuffer. toString ( ) ) ;
tbClobMapper. insertSelective ( tbClob) ;
}
b.控制台运行截图
c 数据库内容截图1
c.数据库内容截图2(点击了小圆点后出现的文本内容)
2.4.2 将文本内容从数据库读出到指定目录中
a 示例代码
@Autowired
TbClobMapper tbClobMapper;
@Test
public void clobRead ( ) throws Exception {
TbClob tbClob = tbClobMapper. selectByPrimaryKey ( 10 ) ;
OutputStream os= new FileOutputStream ( "d:\\" + tbClob. getFname ( ) ) ;
OutputStreamWriter osw= new OutputStreamWriter ( os) ;
osw. write ( tbClob. getF ( ) ) ;
osw. close ( ) ;
}
b.控制台运行截图
c 生成的文件的位置
d.生成文件的内容
3.调用存储过程
3.1 存储过程的出参为String类型
3.1.1 创立存储过程zz
CREATE or replace procedure zz
( zh1 number, zh2 in number, money number, jg out varchar2)
as
vzh1sal number;
vcount number:= 0 ;
begin
select sal into vzh1sal from emp where empno= zh1;
if vzh1sal< money then
jg:= '余额不足' ;
return ;
end if ;
update emp set sal= sal- money where empno= zh1;
vcount:= vcount+ sql % rowcount ;
update emp set sal= sal+ money where empno= zh2;
vcount:= vcount+ sql % rowcount ;
if vcount= 2 then
jg:= '转账成功' ;
commit ;
else
jg:= '转账失败' ;
rollback ;
end if ;
end ;
3.1.2 在zlz包.mapper包下创建TbBlobMapper接口,其目录结构如下所示
3.1.3 在TbBlobMapper接口中创建zz方法
package com. zlz. mapper ;
import tk. mybatis. mapper. common. Mapper ;
import java. util. Map ;
public interface TbBlobMapper extends Mapper < TbBlob > {
void zz ( Map < String , Object > map) ;
}
3.1.4 在resource包.mappers包下创建TbBlobMapper.xml文件,其目录结构如下所示
3.1.5 在TbBlobMapper.xml里面创建zz方法所对应的select标签
<?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.zlz.mapper.TbBlobMapper" >
< select id = " zz" statementType = " CALLABLE" >
call zz(#{zh1},#{zh2},#{money},#{jg,mode=OUT,jdbcType=VARCHAR})
</ select>
</ mapper>
3.1.6 测试
a 转账失败
a.1 测试代码
@Autowired
TbBlobMapper tbBlobMapper;
@Test
public void t1 ( ) {
Map < String , Object > map= new HashMap ( ) ;
map. put ( "zh1" , 7499 ) ;
map. put ( "zh2" , 1111 ) ;
map. put ( "money" , 100 ) ;
map. put ( "jg" , null ) ;
tbBlobMapper. zz ( map) ;
System . out. println ( map. get ( "jg" ) ) ;
}
a.2 控制台运行截图
b 转账成功
b.1 测试代码
@Autowired
TbBlobMapper tbBlobMapper;
@Test
public void t1 ( ) {
Map < String , Object > map= new HashMap ( ) ;
map. put ( "zh1" , 7369 ) ;
map. put ( "zh2" , 7499 ) ;
map. put ( "money" , 100 ) ;
map. put ( "jg" , null ) ;
tbBlobMapper. zz ( map) ;
System . out. println ( map. get ( "jg" ) ) ;
}
b.2 控制台运行截图
b.3 转账前数据表的数据
b.4 转账后数据表的数据
3.2 存储过程的出参为Cursor游标类型
3.2.1 创立存储过程p3
create or replace procedure p3
( losal number, hisal number, jg out sys_refcursor)
as
begin
open jg for select * from emp where sal
between losal and hisal;
end ;
3.2.2 在zlz包.mapper包下创建EmpMapper接口,其目录结构如下所示
3.2.3 在EmpMapper接口中创建p3方法
package com. zlz. mapper ;
import com. zlz. entity. Emp ;
import tk. mybatis. mapper. common. Mapper ;
import java. util. Map ;
public interface EmpMapper extends Mapper < Emp > {
void p3 ( Map < String , Object > map) ;
}
3.2.4 在resource包.mappers包下创建EmpMapper.xml文件,其目录结构如下所示
3.2.5 在EmpMapper.xml里面创建p3方法所对应的select标签
<?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.zlz.mapper.EmpMapper" >
< select id = " p3" statementType = " CALLABLE" >
call p3(#{a},#{b},#{c,mode=OUT,jdbcType=CURSOR,resultMap=myemp,javaType=ResultSet})
</ select>
< resultMap id = " myemp" type = " emp" >
</ resultMap>
</ mapper>
3.2.6 测试
a 测试代码
@Test
public void p3 ( ) {
Map < String , Object > map= new HashMap < > ( ) ;
map. put ( "a" , 2000 ) ;
map. put ( "b" , 3000 ) ;
map. put ( "c" , null ) ;
em. p3 ( map) ;
List < Emp > list = ( List < Emp > ) map. get ( "c" ) ;
for ( Emp emp : list) {
System . out. println ( emp) ;
}
}
b 控制台运行截图