目录
通过parameterType指定输入参数的类型,类型可以是基本数据类型、自定义类型、pojo的包装类型。
<!--根据id查找学生-->
<select id="findStudentById" parameterType="int" resultType="Student">
select * from student where id=#{id}
</select>
<!--修改学生信息-->
<update id="updateStudent" parameterType="Student">
update student set studentName=#{studentName},gender=#{gender},address=#{address},email=#{email},remark=#{remark} where id=#{id}
</update>
????????创建Student类
public class Student {
private int id;
private String studentName;
private String gender;
private String address;
private String email;
private String remark;
private List<Order> listOrder;
public Student() {
}
public Student(String studentName, String gender, String address,String remark) {
this.studentName = studentName;
this.gender = gender;
this.address = address;
this.remark = remark;
}
public List<Order> getListOrder() {
return listOrder;
}
public void setListOrder(List<Order> listOrder) {
this.listOrder = listOrder;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
? ? ? ? 定义StudentMapper接口
public interface StudentMapper {
//通过id查询学生
public Student findStudentById(int id);
}
? ? ? ? 编写StudentMapper.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="com.batis.mapper.StudentMapper">
<select id="findStudentById" parameterType="Student" resultType="com.batis.domain.Student">
SELECT * FROM student WHERE id=#{id};
</select>
</mapper>
? ? ? ? 测试
public class TestDemo {
SqlSessionFactory ssf = null;
@Before
public void creatFactory(){
InputStream input = null;
try {
input = Resources.getResourceAsStream("SqlMapConfig.xml");
} catch (IOException e) {
e.printStackTrace();
}
ssf = new SqlSessionFactoryBuilder().build(input);
}
@Test
public void testMapper(){
SqlSession session =ssf.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.findStudentById(1);
System.out.println(student.getStudentName());
session.commit();
session.close();
}
输出映射有两种,resultType和resultMap。resultType也有很多种对象类型,当我们使用resultType时,查询出来的列名必须和pojo中对应的属性名要一致才能正确的映射成功,否则,就会出现映射出错这种现象。如果属性名不一致时使用resultMap自定义映射
这里我主要介绍resultMap自定义映射
????????创建Exams实例(其中的eid,examdate,uid属性与数据库中的字段名不一致)
public class Exams {
private int eid;
private Date examdate;
private int uid;
private String score;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public Date getExamdate() {
return examdate;
}
public void setExamdate(Date examdate) {
this.examdate = examdate;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
? ? ? ? 创建ExamMapper接口
import com.batis.domain.Exams;
import java.util.List;
public interface ExamMapper {
//查询所有信息
public List<Exams> findExamAll();
}
? ? ? ? 创建ExamMapper.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="com.batis.mapper.ExamMapper">
<resultMap id="examMapper" type="com.batis.domain.Exams">
<!--与数据库里的字段名进行映射-->
<id column="e_id" property="eid"></id>
<result column="exam_date" property="examdate"></result>
<result column="user_id" property="uid"></result>
</resultMap>
<!--resultMap里的名称与resultMap标签里的id里的名称一致-->
<select id="findExamAll" resultMap="examMapper">
select * from exams;
</select>
</mapper>
? ? ? ? 测试
@Test
public void testMapper3(){
SqlSession sqlSession = ssf.openSession();
ExamMapper mapper = sqlSession.getMapper(ExamMapper.class);
List<Exams> examAll = mapper.findExamAll();
for (Exams exam : examAll) {
System.out.println(exam.getEid()+","+exam.getExamdate());
}
}
? ? ? ? 运行结果