前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mybatis学习之CLOB、BLOB处理及多参数方法映射

mybatis学习之CLOB、BLOB处理及多参数方法映射

作者头像
用户1141560
发布2017-12-26 11:44:39
3.6K0
发布2017-12-26 11:44:39
举报
文章被收录于专栏:西安-晁州

CLOB数据mysql对应数据类型为longtext、BLOB类型为longblob:

model实体:

代码语言:javascript
复制
...
private Integer id;
private String name;
private int age;

private byte[] pic; // 映射blob
private String remark; // 映射longtext
...

1、blob、clob数据插入:

代码语言:javascript
复制
<insert id="insertStudent" parameterType="Student">
    insert into t_student values(null,#{name},#{age},#{pic},#{remark})
</insert>

对应Dao接口:

代码语言:javascript
复制
/**
* 插入学生
* @param student
* @return
*/
public int insertStudent(Student student);

junit测试:

代码语言:javascript
复制
@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[]):

代码语言:javascript
复制
<select id="getStudentById" parameterType="Integer" resultType="Student">
    select * from t_student where id = #{id}
</select>

Dao接口部分:

代码语言:javascript
复制
@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接口部分:

代码语言:javascript
复制
/**
     * 根据姓名和年龄进行查询(mybatis多参数查询)
     * @param name
     * @param age
     * @return
     */
    public List<Student> getStudentsBy2Args(String name,Integer age);

对应mapper映射:

代码语言:javascript
复制
<select id="getStudentsBy2Args" resultMap="StudentResult">
    select * from t_student where name like #{param1} and age = #{param2}        <!-- 与方法的参数顺序一一对应 -->
</select>

junit测试:

代码语言:javascript
复制
@Test
    public void testGetStudentsBy2Args() throws Exception {
        List<Student> students = studentDao.getStudentsBy2Args("%晁%", 24);
        for (Student student : students) {
            System.out.println("####### "+student);
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-07-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档