package dao;
import domain.Person;
import org.apache.ibatis.session.SqlSession;
import util.MySqlSession;
import java.util.List;
public class PersonDao {
public static void main(String[] args) {
PersonDao personDao = new PersonDao();
List<Person> personList = personDao.selectList();
for (Person person : personList) {
System.out.println(person);
}
}
SqlSession sqlSession = MySqlSession.getSqlSession(true);
public Person selectOne(int pid) {
return sqlSession.selectOne("selectOne", pid);
}
public List<Person> selectList() {
return sqlSession.selectList("selectList");
}
}
<?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.PersonDao">
<resultMap id="selectByPid" type="domain.Person">
<id property="pid" column="pid"></id>
<result property="pname" column="pname"></result>
<association property="idCard" javaType="domain.IdCard">
<id property="cardId" column="cardId"></id>
<result property="address" column="address"></result>
</association>
</resultMap>
<select id="selectOne" resultMap="selectByPid">
select * from person p inner join idcard i on p.cardid = i.cardid where p.pid = #{pid}
</select>
<select id="selectList" resultMap="selectByPid">
select * from person p inner join idcard i on p.cardid = i.cardid
</select>
<!-- <resultMap id="selectFunction" type="domain.Person">-->
<!-- <id property="pid" column="pid"></id>-->
<!-- <result property="pname" column="pname"></result>-->
<!-- <association property="idCard" javaType="domain.IdCard" select="selectCardId" column="cardId"></association>-->
<!-- </resultMap>-->
<!-- <select id="selectCardId" resultType="domain.IdCard">-->
<!-- select * from idcard where cardId = #{cardId}-->
<!-- </select>-->
<!-- <select id="selectOne" resultMap="selectFunction">-->
<!-- select pid,pname,cardId from person where pid = #{pid}-->
<!-- </select>-->
<!-- <select id="selectList" resultMap="selectFunction">-->
<!-- select pid,pname,cardId from person-->
<!-- </select>-->
</mapper>