myBatis中的输入不同参数:parameterType

本文介绍了MyBatis中简单类型和对象类型参数的处理方式,包括字面量传参、动态字段排序,以及如何处理嵌套对象的级联查询。重点讲解了#{和}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、输入参数类型为简单类型

byte int short long double float char boolean String
1. 不同之处
 a.简单类型传入参数名任意写 #{***} (自动类型转换)
 b.${value} , 其中的标识符只能是value (适合用于动态排序,动态字段)
  #{}:会自动给String类型加上‘’
  ${}:原样输出
 c.#{}可以防止SQL注入
   ${}不防止SQL注入
  ${}动态排序的用法

	<!--
	当使用#{column} 
		此时的sql语句为select stuno,stuname,stuage from student order by 'stuno' desc
		order by 的关键字为字符串常量,此时排序失效
	当使用${value}时
		此时的sql语句为select stuno,stuname,stuage from student order by stuno desc
		order by的关键字为变量 stuno 可以正常排序
	-->
	<select id="queryStudentOrderByColumn"  parameterType="String"	resultType="student">
		select stuno,stuname,stuage from student order by ${value} desc
	</select>

2. 相同之处
 a.都可以获取对象的值(嵌套类型对象)

	<select id="queryStudentByStuname"	parameterType="string"	 resultType="student">
		select stuno,stuname,stuage from student where stuname = ${value}
		<!--此时的SQL语句为 select stuno,stuname,stuage from student where stuname = zs
		此时若要正常使用,需要自己添加-‘’- 
		例如select stuno,stuname,stuage from student where stuname = ‘${value}-->
	</select>
	<select id="queryStudentByStuname"	parameterType="string"	 resultType="student">
		select stuno,stuname,stuage from student where stuname = #{stuName}
		<!--此时的SQL语句为 select stuno,stuname,stuage from student where stuname = 'zs'-->
	</select>

   示例

	<!--传入参数为w 进行模糊查询-->
	<select id="queryStudentBystuageOrstuName"  parameterType="student"	resultType="student">
		select stuno,stuname,stuage from student where stuage = #{stuAge} or stuname like #{stuName}
	</select>
	<!--或者是-->
	<select id="queryStudentBystuageOrstuName"  parameterType="student"	resultType="student">
		select stuno,stuname,stuage from student where stuage = #{stuAge} or stuname like '%${stuName}%'
	</select>

二、对象类型

 a.#{属性名}
 b.${属性名}

三、嵌套对象类型

Student类中嵌套一个Address类
myBatis支持级联数据 可用address.homeaddress和address.schooladdress获取到数据

package person.demo.entity;

public class Student {
	private int stuNo;
	private String stuName;
	private int stuAge;
	private String graName;
	private boolean stuSex;
	private Address address;
	
	public Student() {
	}
	
	public Student(int stuNo, String stuName, int stuAge, String graName) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuAge = stuAge;
		this.graName = graName;
	}
	
	public Student(int stuNo, String stuName, int stuAge, String graName, boolean stuSex) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuAge = stuAge;
		this.graName = graName;
		this.stuSex = stuSex;
	}

	public int getStuNo() {
		return stuNo;
	}
	public void setStuNo(int stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	public String getGraName() {
		return graName;
	}
	public void setGraName(String graName) {
		this.graName = graName;
	}
	public boolean isStuSex() {
		return stuSex;
	}
	public void setStuSex(boolean stuSex) {
		this.stuSex = stuSex;
	}
	
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", stuAge=" + stuAge + ", graName=" + graName
				+ ", stuSex=" + stuSex + "]\n";
	}
	
}	

package person.demo.entity;

public class Address {
	private String homeAddress;
	private String schoolAddress;
	public Address() {
		super();
	}
	public Address(String homeAddress, String schoolAddress) {
		super();
		this.homeAddress = homeAddress;
		this.schoolAddress = schoolAddress;
	}
	public String getHomeAddress() {
		return homeAddress;
	}
	public void setHomeAddress(String homeAddress) {
		this.homeAddress = homeAddress;
	}
	public String getSchoolAddress() {
		return schoolAddress;
	}
	public void setSchoolAddress(String schoolAddress) {
		this.schoolAddress = schoolAddress;
	}
	@Override
	public String toString() {
		return "Address [homeAddress=" + homeAddress + ", schoolAddress=" + schoolAddress + "]\n";
	}
}

  mapper.xml文件为

	<select id="queryStudentByaddress"  parameterType="student"	resultType="student">
		select stuno,stuname,stuage from student where homeaddress = #{address.homeAddress} or schooladdress = '${address.schoolAddress}'
	</select>

四、myBatis传入参数为HashMap

  1. 在mapper.xml中编写sql语句
      parameterType为传入参数类型:HashMap
      resultType为返回值参数类型:此处为Student
<select id="queryStudentBystuageOrstuNameWithHashMap"  parameterType="HashMap"	resultType="student">
		select stuno,stuname,stuage from student 
		where stuage = #{stuAge} or stuname like #{stuName}
	</select>
  1. mapper.java接口中
	List<Student> queryStudentBystuageOrstuNameWithHashMap(Map<String, Object> map);
  1. 测试类:
      Map<String , Object> studentMap = new HashMap<String, Object>();//初始化一个HashMap用于存放查询数据
      用map中的key(stuAge)匹配#{stuAge}中的占位符,如果匹配成功,就用map的value替换占位符。
//根据姓名和年龄查找学生---传入参数类型为HashMap
		public static void queryStudentBystuageOrstuNameWithHashMap() throws IOException {
			Reader reader = Resources.getResourceAsReader("conf.xml");
			SqlSessionFactory sessionFactoty = new SqlSessionFactoryBuilder().build(reader);
			SqlSession session = sessionFactoty.openSession();
			StudentMapper studentMapper = session.getMapper(StudentMapper.class);
			Map<String , Object> studentMap = new HashMap<String, Object>();//初始化一个HashMap用于存放查询数据
			studentMap.put("stuAge", 22);	//用key-value存储具体数据
			studentMap.put("stuName", "ls");
			List<Student> students = studentMapper.queryStudentBystuageOrstuNameWithHashMap(studentMap);
			System.out.println(students.toString());
			session.close();
		}		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值