Table of contents
Entity class (pojo) parameters
@Param annotation (named parameters)
single simple type parameter
Simple types include:
- byte short int long float double char
- Byte Short Integer Long Float Double Character
- String
- java.util.Date
- java.sql.Date
/**
* 根据学生id进行查询
* @param id
* @return
*/
Student selectById(Long id);
The corresponding mapping xml file
<select id="selectById" resultType="com.study.pojo.Student">
select * from t_student where id=#{id};
</select>
Simple types can be automatically type recognized for mybatis:
- That is to say, for mybatis, it can automatically infer the ps.setXxxx() method. ps.setString() or ps.setInt(). It can be inferred automatically.
If there is only one parameter, the content inside #{} can be written casually. For ${}, pay attention to adding single quotes.
Map parameters
Requirements: query based on name and age
/**
* 根据name和age查询
* @param paramMap
* @return
*/
List<Student> selectByParamMap(Map<String,Object> paramMap);
The corresponding mapping xml file
<select id="selectByParamMap" resultType="com.study.pojo.Student">
select * from t_student where name=#{name} and age=#{age}
</select>
Corresponding java test code
@org.junit.Test
public void test02(){
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Map map=new HashMap();
map.put("name","张三");
map.put("age","18");
List<Student> student = mapper.selectByParamMap(map);
System.out.println(student);
}
This method is to manually encapsulate the Map collection, and store each condition in the collection in the form of key and value. Then use #{key of the map collection} to get the value when using it.
Entity class (pojo) parameters
Requirement: Insert a piece of Student data
/**
* 插入学生数据
* @param student
* @return
*/
int insertStudent(Student student);
The corresponding mapping xml file
<insert id="insertStudent">
insert into t_student values (null ,#{name},#{age},#{brith})
</insert>
Corresponding java test code
@org.junit.Test
public void test03(){
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student();
student.setAge("28");
student.setBrith(new Date());
student.setName("李四");
int i = mapper.insertStudent(student);
System.out.println(i);
System.out.println(student);
}
What needs to be noted here is: #{} is written in the attribute name. This attribute name is essentially: the name of the set/get method name after removing set/get, that is, using reflection.
multi-parameter
Requirements: Query by name and sex
/**
* 根据name和age查询
* @param name
* @param age
* @return
*/
List<Student> selectByNameAndAge(String name, String age);
Corresponding mapping xml file (error demonstration)
<select id="selectByNameAndAge" resultType="com.study.pojo.Student">
select * from t_student where name=#{name} and age=#{age}
</select>
Corresponding java test code
@org.junit.Test
public void test04(){
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> student = mapper.selectByNameAndAge("张三", "18");
System.out.println(student);
}
run error
The exception information describes: the name parameter cannot be found, and the available parameters include [arg1, arg0, param1, param2]
Modify the StudentMapper.xml configuration file: try to use [arg1, arg0, param1, param2] to parameterize
Modify the xml file
<select id="selectByNameAndAge" resultType="com.study.pojo.Student">
select * from t_student where name=#{param1} and age=#{param2}
</select>
Run again to get the result
At the same time, you can also modify the xml file to:
<select id="selectByNameAndAge" resultType="com.study.pojo.Student">
select * from t_student where name=#{arg0} and age=#{arg1}
</select>
You can see through the test:
- arg0 is the first argument
- param1 is the first parameter
- arg1 is the second argument
- param2 is the second parameter
Implementation principle: In fact, a map collection will be created at the bottom of mybatis, with arg0/param1 as the key and method parameters as the value , such as the following code:
Map<String,Object> map = new HashMap<>();
map.put("arg0", name);
map.put("arg1", sex);
map.put("param1", name);
map.put("param2", sex);
// 所以可以这样取值:#{arg0} #{arg1} #{param1} #{param2}
// 其本质就是#{map集合的key}
注意:使用mybatis3.4.2之前的版本时:要用#{0}和#{1}这种形式。
@Param annotation (named parameters)
Can it be done without arg0 arg1 param1 param2? Can we customize the key of this map collection? Of course you can . Just use the @Param annotation. This enhances readability.
Requirements: query based on name and age
add annotation
/**
* 根据name和age查询
* @param name
* @param age
* @return
*/
List<Student> selectByNameAndAge(@Param("name") String name,@Param("age") String age);
Corresponding xml file
<select id="selectByNameAndAge" resultType="com.study.pojo.Student">
select * from t_student where name=#{name} and age=#{age}
</select>