mybatis-config.xml
<?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"/>
<!--配置别名-->
<typeAliases>
<package name="com.banana.mabatis关联映射.pojo"/>
</typeAliases>
<!--配置环境-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<mapper resource="com/banana/mabatis关联映射/mapper/StuCardMapper.xml"/>
<mapper resource="com/banana/mabatis关联映射/mapper/StuMapper.xml"/>
<mapper resource="com/banana/mabatis关联映射/mapper/StuClassMapper.xml"/>
</mappers>
</configuration>
映射文件:
<?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="student">
<select id="findStudentById" parameterType="Integer"
resultType="com.banana.pojo.Student">
select *from test where id = #{id}
</select>
<select id="findStudentByName" parameterType="String"
resultType="com.banana.pojo.Student">
select * from test where name like '%${value}%'
</select>
<insert id="addStudent" parameterType="com.banana.pojo.Student">
insert into test values(#{id},#{name},#{sex},#{age})
</insert>
<update id="updateStudent" parameterType="com.banana.pojo.Student">
update test set name = #{name},sex = #{sex},age = #{age} where id = #{id}
</update>
<delete id="deleteStudent" parameterType="Integer">
delete from test where id = #{id}
</delete>
</mapper>
其中,结果集查询可用resultMap
<!--映射查询--> <select id="findStuBySid" parameterType="Integer" resultMap="stuResultsMap"> select *,c.balance from stu s,stu_card c where s.cardid=c.cid and s.sid = #{sid} </select> <!--结果集处理--> <resultMap id="stuResultsMap" type="stu" > <!--id用于映射主键--> <id column="sid" property="sid"/> <!--result映射其他字段--> <result column="sname" property="sname"/> <result column="age" property="age"/> <result column="course" property="course"/> <!--用于映射stu表和stu_card表的关联关系--> <association property="sc" javaType="StuCard"> <id column="cid" property="cid"/> <result column="balance" property="balance"/> </association> </resultMap>
association是用于一对一和多对一,而collection是用于一对多的关系
<resultMap id="stuClassResultsMap" type="stuClass">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
<result column="sum" property="sum"/>
<collection property="stuInfoList" ofType="stuInfo">
<id column="sid" property="sid"/>
<result column="sname" property="sname"/>
<result column="age" property="age"/>
<result column="course" property="course"/>
</collection>
</resultMap>
测试
public class TestOneToMany {
public static void main(String[] args) throws IOException {
//读取xml
InputStream in = Resources.getResourceAsStream("mybatis-config_关联映射.xml");
//创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
SqlSession sqlSession = factory.openSession();
//执行查询
StuClass stuClass = sqlSession.selectOne("stuClass.findStuClassByCid",1);
System.out.println(stuClass);
List<StuInfo> stuInfos = stuClass.getStuInfoList();
for(StuInfo stuInfo:stuInfos){
System.out.println(stuInfo);
}
sqlSession.close();
}
}