1.类型为简单类型(8个基本类型+String)
#{}、${}的区别
区别1
#{任意值}
${value} ,其中的标识符只能是value
<select id="queryStudentByStuname" parameterType="string" resultMap="studentResult">
<!-- select * from student where stuname = #{stuname} -->
select * from student where stuname = '${value}'
</select>
区别2
#{}自动给String类型加上’ ’ (自动类型转换)
${}原样输出,但是适合于动态排序(动态字段)
<select id="queryStudentOrderByColumn" parameterType="string" resultMap="studentResult">
select * from student order by ${value} desc
</select>
区别3
#{}防止SQL注入
${}不能防止SQL注入
2.对象类型
#{属性名}
${属性名}
<?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="nuc.hzb.mapper.studentMapper">
<select id="queryStudentByAddress" parameterType="address" resultType="student">
select * from student where homeaddress = #{homeAddress} or schooladdress like '${schoolAddress}'
</select>
<select id="queryStudentByAddress1" parameterType="student" resultType="student">
select * from student where homeaddress = #{address.homeAddress} or schooladdress like '${address.schoolAddress}'
</select>
<select id="queryStudentByStuAgeOrStuNameWithHashMap" parameterType="hashmap" resultType="student">
select * from student where stuage = #{stuAge} or stuname like '%${stuName}%'
</select>
</mapper>
package nuc.hzb.mapper;
import java.util.List;
import java.util.Map;
import nuc.hzb.entity.Address;
import nuc.hzb.entity.Student;
public interface studentMapper {
List<Student> queryStudentByAddress(Address address);
List<Student> queryStudentByAddress1(Student student);
List<Student> queryStudentByStuAgeOrStuNameWithHashMap(Map<String, Object> map);
}
package nuc.hzb.test;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import nuc.hzb.entity.Address;
import nuc.hzb.entity.Student;
import nuc.hzb.mapper.studentMapper;
public class TestMyBatis {
public static void queryStudentByAddress() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
studentMapper studentmapper = session.getMapper(studentMapper.class);
Address address = new Address();
address.setHomeAddress("HH");
address.setSchoolAddress("%Z%");
List<Student> students = studentmapper.queryStudentByAddress(address);
System.out.println(students);
session.close();
}
public static void queryStudentByAddress1() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
studentMapper studentmapper = session.getMapper(studentMapper.class);
Address address = new Address();
address.setHomeAddress("HH");
address.setSchoolAddress("%Z%");
Student student = new Student();
student.setAddress(address);
List<Student> students = studentmapper.queryStudentByAddress1(student);
System.out.println(students);
session.close();
}
public static void queryStudentByStuAgeOrStuNameWithHashMap() throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
studentMapper studentmapper = session.getMapper(studentMapper.class);
Map<String, Object> studenMap = new HashMap<String, Object>();
studenMap.put("stuAge", 18);
studenMap.put("stuName", "hzb");
List<Student> students = studentmapper.queryStudentByStuAgeOrStuNameWithHashMap(studenMap);
System.out.println(students);
session.close();
}
public static void main(String args[]) throws IOException {
queryStudentByAddress();
queryStudentByAddress1();
queryStudentByStuAgeOrStuNameWithHashMap();
}
}