mybatis 多表查询 与配置
<?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"> <!--绑定一个对应的Dao/Mapper接口--> <mapper namespace="com.liu.dao.StudentMapper"> <select id="getStudents" resultType="student"> select s.id,s.u_name,s.tid,t.t_name from student s,teacher t where s.tid=t.id; </select> <resultMap id="studentTeacher" type="Student"> <result column="id" property="id" /> <result column="u_name" property="u_name" /> <result column="tid" property="tid" /> <association property="teacherss" column="tid" javaType="Teacher" select="getTeacher" /> </resultMap> <select id="getStudents2" resultMap="studentTeacher"> select * from student; </select> <resultMap id="studentTeacher3" type="Student"> <result column="sid" property="id" /> <result column="uname" property="u_name" /> <result column="tid" property="tid" /> <association property="teacherss" javaType="Teacher"> <result property="t_name" column="tname"></result> </association> </resultMap> <select id="getStudents3" resultMap="studentTeacher3"> select s.id sid ,s.u_name uname,s.tid tid,t.t_name tname from student s,teacher t where tid=t.id; </select> <select id="getTeacher" resultType="Teacher"> select * from teacher where id= #{id}; </select> </mapper>
<?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> <properties resource="db.properties" /> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <typeAliases> <package name="com.liu.pojo" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!--绑定接口--> <mappers> <package name="com.liu.dao"/> </mappers> </configuration>