`

MyBatis3文件类型与多个参数传入

阅读更多
1. MyBatis3处理CLOB、BLOB类型数据

在t_student表增加pic和remark字段
alter table t_student add pic longblob;
alter table t_student add remark longtext;


2. MyBatis3传入多个输入参数

使用map或者hashmap的key-value形式


package com.andrew.model;
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private byte[] pic;
    private String remark;
    public Student() {
        super();
    }
    public Student(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Student(Integer id, String name, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age
                + ", remark=" + remark + "]";
    }
    // getter and setter ...
}


package com.andrew.mappers;
import java.util.List;
import java.util.Map;
import com.andrew.model.Student;
public interface StudentMapper {
    public List<Student> searchStudents(Map<String,Object> map);
    public List<Student> searchStudents2(Map<String,Object> map);
    public List<Student> searchStudents3(Map<String,Object> map);
    public List<Student> searchStudents4(Map<String,Object> map);
    public List<Student> searchStudents5(Map<String,Object> map);
    public int updateStudent(Student student);
    public int insertStudent(Student student);
    public Student getStudentById(Integer id);
    public List<Student> searchStudents6(String name,int age);
}


<?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.andrew.mappers.StudentMapper">
    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
    </resultMap>
...
    <insert id="insertStudent" parameterType="Student">
        insert into t_student values(null,#{name},#{age},1,1,#{pic},#{remark});
    </insert>
    <select id="getStudentById" parameterType="Integer" resultType="Student">
        select * from t_student where id = #{id}
    </select>
    <select id="searchStudents6" resultMap="StudentResult">
        select * from t_student where name like #{param1} and age = #{param2}
    </select>
</mapper>


package com.andrew.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.andrew.mappers.StudentMapper;
import com.andrew.model.Student;
import com.andrew.util.SqlSessionFactoryUtil;
public class StudentClobTest {
    private static Logger logger = Logger.getLogger(StudentClobTest.class);
    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 testSearchStudents() {
        Student student = new Student();
        student.setName("张图片1");
        student.setAge(14);
        student.setRemark("很长的本文...");
        byte[] pic = null;
        try {
            File file = new File("F://boy.jpg");
            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(4);
        System.out.println(student);
        byte[] pic = student.getPic();
        try {
            File file = new File("F://boy2.jpg");
            OutputStream outputStream = new FileOutputStream(file);
            outputStream.write(pic);
            outputStream.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testSearchStudents6() {
        List<Student> studentList = studentMapper.searchStudents6("%六%", 26);
        for (Student student : studentList) {
            System.out.println(student);
        }
    }
}
分享到:
评论

相关推荐

    MyBatis传入多个参数的问题

    ### MyBatis传入多个参数的问题 在使用MyBatis框架进行数据库操作时,经常会遇到需要向SQL查询语句传入多个参数的情况。本文将详细介绍几种常见的多参数传递方法,并结合具体的代码示例来帮助读者更好地理解和应用...

    Mybatis多参数查询与列表查询不同方式实现

    当我们需要传递多个参数时,可以使用Map对象。在Mapper XML文件中,可以通过`&lt;foreach&gt;`标签遍历Map的键值对。例如,查询用户根据用户名和年龄: ```xml SELECT * FROM user WHERE username = #{username} AND ...

    详解mybatis中association和collection的column传入多个参数问题

    本篇文章将详细探讨如何在 `association` 和 `collection` 的 `column` 属性中传入多个参数,以实现更复杂的查询需求。 首先,`association` 用于表示一对一的关系,而 `collection` 用于表示一对多的关系。在映射...

    MyBatis3DTD约束

    5. **parameterMap**:虽然在MyBatis3中已不再推荐使用,但在旧版本或复杂场景下,它用于定义参数集合,通过name属性与传入的参数对象关联。 6. **sql**:这是一个可重用的SQL片段,可以通过include元素在其他地方...

    postgresql + mybatis传入时间参数的问题.md

    我们看到, 直接在可视化工具里用SQL写 ccf.last_update_timestamp between TIMESTAMP '2019-12-13' AND TIMESTAMP '2019-12-13...但是在mapper文件中这么写就不可以了, 它会提示你varchar类型不能和日期类型进行比较

    mybatis 3 中文 指南

    不使用XML配置文件的情况下,可以使用`SqlSessionFactoryBuilder`的`build(Configuration configuration)`方法,传入一个已经配置好的`Configuration`对象来创建`SqlSessionFactory`。 #### SqlSession的获取与使用...

    mybatis3--3.xml细节

    在"Mybatis3--3.xml"这个配置文件中,我们可以看到多个关键元素,它们构成了MyBatis的配置体系。首先,`&lt;configuration&gt;`是整个配置文件的根元素,它包含了许多子元素,如`&lt;properties&gt;`、`&lt;settings&gt;`、`...

    Mybatis框架 mapper.xml文件中parameterType传递参数常用的几种方式.pdf

    可以通过HashMap将多个参数作为键值对传递: ```java public User queryUser(Map, Object&gt; map); ``` 在XML中,可以使用键名来引用HashMap中的值: ```xml select * from user where user_name = #{...

    关于MyBatis参数传入#{index}的问题的解决方案【源码】

    在使用MyBatis进行数据库操作时,我们经常会遇到需要传递多个参数的情况。在这个问题中,开发者遇到了一个关于如何正确传入参数#{index}的问题。在MyBatis中,#{index}是参数占位符,它用于动态SQL的拼接,但具体...

    MyBatis3 教程 中文完整版

    MyBatis支持两种参数映射方式:传入一个Map对象,其中key是参数名,value是参数值;使用注解@Param标记参数。 6. **结果映射** 结果映射主要用于复杂的数据结构映射,例如一对一、一对多、多对多关系的映射,以及...

    MyBatis3用户指南(附JavaDB实例

    MyBatis支持多种参数映射方式,如传入单个参数、多个参数、Map对象以及POJO类。使用`@Param`注解可指定参数别名,`@ResultMap`用于定义结果集映射,方便复用。 **6. 结果集映射** MyBatis提供了灵活的结果集映射...

    第三章 MyBatis的核心配置.docx

    例如,新建一个db.properties配置文件,写上数据库信息,接着在Mybatis文件中配置属性,通过resource引用,最后修改数据库连接信息。 3.2.3 元素 元素用于配置MyBatis的全局参数,常用参数包括: * cacheEnabled...

    MyBatis-3文档整理.pdf

    - settings元素包含多个配置项,用于控制MyBatis运行时行为。 - typeAliases元素可以为Java类型定义别名,简化映射文件中的类型。 - typeHandlers是处理Java类型和JDBC类型之间转换的类。 - objectFactory负责...

    springmybatis

    3. 在User.xml 文件里面 主要是定义各种SQL 语句,以及这些语句的参数,以及要返回的类型等. 开始测试 在test_src 源码目录下建立com.yihaomen.test这个package,并建立测试类Test: 程序代码 程序代码 package ...

    mybatis3.4.5的jar包与源码

    3. **动态SQL**:MyBatis的动态SQL功能强大,可以在映射文件或注解中编写条件语句,根据传入的参数动态地生成SQL。 4. **结果映射**:MyBatis提供了自动类型转换和结果集映射功能,使得Java对象和数据库记录之间的...

    Mybatis参数传递1

    3. 多个参数传入 当有多个参数时,不能直接使用`parameterType`属性,因为MyBatis无法识别多个基础类型的参数。可以创建一个包含所有参数的类,然后将这些参数封装到这个类的实例中。例如: ```java public class ...

    mybatis-3-mybatis-3.2.5.zip

    5. **参数映射**:MyBatis支持多种参数映射方式,包括传入单个参数、Map参数和多个参数。可以使用`#{}`来引用参数,自动处理参数类型。 6. **结果映射**:MyBatis提供了丰富的结果映射机制,可以将查询结果自动转换...

    mybatis generator jar带configure配置文件

    首先,`configure`文件是MyBatis Generator的核心配置文件,它包含了所有必要的参数来指定数据库连接信息、生成的目标位置、生成的文件类型等。一个标准的`generatorConfig.xml`文件通常包括以下部分: 1. **...

    MyBatis传入集合 list 数组 map参数的写法

    使用Map参数的一个好处是,可以将多个参数封装在一起,通过Map的键来区分不同的参数。例如,你可以同时传入`departmentId`和`jobId`,并根据需求在映射文件中使用它们。 在实际应用中,MyBatis会自动将方法参数转换...

    mybatis笔记

    #### 十一、MyBatis传入多个参数时的处理方式 - **参数处理**:可以使用`@Param`注解为每个参数指定名称,也可以直接使用位置参数或Map类型。 #### 十二、列名与属性名不同时的解决方案 - **解决方案**:当数据库...

Global site tag (gtag.js) - Google Analytics