MyBatis3文件类型与多个参数传入
程序员文章站
2022-07-13 16:15:23
...
1. MyBatis3处理CLOB、BLOB类型数据
2. MyBatis3传入多个输入参数
在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); } } }
上一篇: MyBatis3注解的关系映射
下一篇: MyBatis3动态SQL