Article directory
DAO proxy implements database operations
DAO proxy implements database operations
1. Remove the Dao interface implementation class
2. getMapper gets the proxy object
Just call the getMapper() method of SqlSession to get the implementation class object of the specified interface. The parameter of this method is the class value of the specified Dao interface class.
SqlSession session = factory.openSession();
StudentDao dao = session.getMapper(StudentDao.class);
Use tools
StudentDao studentDao =
MyBatisUtil.getSqlSession().getMapper(StudentDao.class);
The object created by getMapper() is the StudentDaoImpl class we created instead of ourselves
3. Use the Dao proxy object method to execute the sql statement
select method to query
@Test
public void testSelect() throws IOException {
final List<Student> studentList = studentDao.selectStudents();
studentList.forEach( stu -> System.out.println(stu));
}
insert method to insert
@Test
public void testInsert() throws IOException {
Student student = new Student();
student.setId(1006);
student.setName("林浩");
student.setEmail("[email protected]");
student.setAge(26);
int nums = studentDao.insertStudent(student);
System.out.println("使用 Dao 添加数据:"+nums);
}
4. In-depth understanding of parameters
Pass parameters to mapper.xml file from java code.
parameterType
parameterType: The type of the method parameter in the interface, the fully qualified name or alias of the type. This property is optional because MyBatis can infer the parameters of the specific incoming statement, and the default value is unset. The parameters of the method in the interface are passed from the java code to the sql statement of the mapper file.
- int or java.lang.Integer
- hashmap java.util.HashMap
- list or java.util.ArrayList
- student or com.bjpowernode.domain.Student
<select>, <insert>, <update>, <delete> can use parameterType to specify the type.
eg:
<delete id="deleteStudent" parameterType="int">
delete from student where id=#{studentId}
</delete>
等同于
<delete id="deleteStudent" parameterType="java.lang.Integer">
delete from student where id=#{studentId}
</delete>
a simple parameter
The parameters of the method in the Dao interface have only one simple type (java basic type and String), the placeholder #{any character}, which has nothing to do with the parameter name of the method.
interface method
Student selectById(int id);
mapper file
<select id="selectById" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where id=#{studentId}
</select>
#{studentId} , studentId is a custom variable name, not related to the method parameter name.
testing method
@Test
public void testSelectById(){
//一个参数
Student student = studentDao.selectById(1005);
System.out.println("查询 id 是 1005 的学生:"+student);
}
Use @Param
When the Dao interface method has multiple parameters, the parameters need to be used by name. Add @Param("custom parameter name") in front of the method parameter, and use #{custom parameter name} in the mapper file.
例如定义 List<Student> selectStudent( @Param(“personName”)
String name ) { … }
mapper 文件 select * from student where name =
#{ personName}
interface method
List<Student> selectMultiParam(@Param("personName") String name,
@Param("personAge") int age);
Mapper file
<select id="selectMultiParam" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where name=#{personName} or age
=#{personAge}
</select>
testing method
@Test
public void testSelectMultiParam(){
List<Student> stuList = studentDao.selectMultiParam("李力",20);
stuList.forEach( stu -> System.out.println(stu));
}
user target audience
Use java object to pass parameters, the attribute value of java is the parameter value required by sql. Each property is a parameter.
Syntax format: #{ property,javaType=java in the data type name
jdbcType=data type name} javaType, the type of jdbcType can be detected by MyBatis and generally does not need to be set. Common format #{ property }
Create an object QueryParam that holds parameter values
package com.bjpowernode.vo;
public class QueryParam {
private String queryName;
private int queryAge;
//set ,get 方法
}
interface method
List<Student> selectMultiObject(QueryParam queryParam);
Mapper file
<select id="selectMultiObject" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where name=#{queryName} or age
=#{queryAge}
</select>
或
<select id="selectMultiObject" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student
where name=#{queryName,javaType=string,jdbcType=VARCHAR}
or age =#{queryAge,javaType=int,jdbcType=INTEGER}
</select>
testing method
@Test
public void selectMultiObject(){
QueryParam qp = new QueryParam();
qp.setQueryName("李力");
qp.setQueryAge(20);
List<Student> stuList = studentDao.selectMultiObject(qp);
stuList.forEach( stu -> System.out.println(stu));
}