结论
- 传递单个参数,参数名可以是任意字符串
但是,按照规范,建议使用形参变量名 - 传递多个参数,参数名不能直接使用形参名,
只能是从arg0开始的索引,或从param1开始的变量
也可以在接口使用@Param(“参数名”)指定参数名,这样参数就可以直接使用了
代码
StudentDao.xml【接口映射的xml】
<?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="dao.StudentDao">
<!-- id对应接口中的方法名;resultType返回值类型-->
<select id="search" resultType="entity.Student">
select * from student
</select>
<!-- Mybatis不允许重构,即,方法名不能相同 -->
<select id="searchById" resultType="entity.Student">
select * from student where id=#{id}
</select>
<!-- 传递单个参数,参数名可以是任意字符串,但是,按照规范,建议使用形参变量名 -->
<select id="searchById2" resultType="entity.Student">
select * from student where id=#{xxx}
</select>
<!-- 传递多个参数,参数名不能是形参名 -->
<!-- 报错 -->
<select id="searchByAgeAndGender" resultType="entity.Student">
select * from student where age=#{age} and gender=#{gender}
</select>
<!-- 传递多个参数,参数名不能是形参名,只能是从arg0开始的索引,或从param1开始的变量 -->
<!-- 也可以在接口指定参数名,这样参数就可以直接使用了 -->
<!-- 正确写法1 -->
<select id="searchByAgeAndGender2" resultType="entity.Student">
select * from student where age=#{arg0} and gender=#{arg1}
</select>
<!-- 正确写法2 -->
<select id="searchByAgeAndGender3" resultType="entity.Student">
select * from student where age=#{param1} and gender=#{param2}
</select>
<!-- 正确写法3 -->
<select id="searchByAgeAndGender4" resultType="entity.Student">
select * from student where age=#{age} and gender=#{gender2}
</select>
</mapper>
studentDao.java【接口】
package dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import entity.Student;
public interface StudentDao {
public boolean add(Student stu);
public List<Student> search();
//Mybatis不允许重构
public Student searchById(int id);
public Student searchById2(int id);
//public Student searchByAgeAndGender(int age, String gender);
public Student searchByAgeAndGender2(int age, String gender);
public Student searchByAgeAndGender3(int age, String gender);
//@Param注解定义后,传递多个参数时,可以在xml中直接使用指定的参数名
public Student searchByAgeAndGender4(@Param("age")int age, @Param("gender2")String gender);
}
Mybatis.xml【配置文件】
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost/school?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/StudentDao.xml" />
</mappers>
</configuration>
Test.java【测试文件】
package test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import dao.StudentDao;
import entity.Student;
public class Test {
public static void main(String[] args) {
SqlSession sqlSession=MybatisSqlSession.getSqlSession();
StudentDao stuDao=sqlSession.getMapper(StudentDao.class);
//查询所有,不传参
List<Student> list=stuDao.search();
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i).getName());
}
System.out.println("1----------------------------");
//查询单个,传一个参数
Student stu=stuDao.searchById(4);
System.out.println(stu.getName());
System.out.println("2----------------------------");
//查询单个,传一个参数
Student stu2=stuDao.searchById2(4);
System.out.println(stu2.getName());
System.out.println("3----------------------------");
//查询单个,传多个参数
//Student stu3=stuDao.searchByAgeAndGender(20,"女");//报错
//System.out.println(stu.getName());
//System.out.println("----------------------------");
//查询单个,传多个参数
Student stu4=stuDao.searchByAgeAndGender2(20,"女");//
System.out.println(stu4.getName());
System.out.println("4----------------------------");
//查询单个,传多个参数
Student stu5=stuDao.searchByAgeAndGender3(20,"女");//
System.out.println(stu5.getName());
System.out.println("5----------------------------");
//查询单个,传多个参数
Student stu6=stuDao.searchByAgeAndGender4(20,"女");//
System.out.println(stu6.getName());
System.out.println("6----------------------------");
}
}
执行结果
页面整体结构