db.properties配置文件
db.name=root
db.password=123456
db.url=jdbc\:mysql\://localhost\:3306/test
db.driver=com.mysql.jdbc.Driver
configuration.xml配置文件
!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<!-- 给实体 取别名 -->
<typeAliases>
<typeAlias type="com.mybatis.entity.ClassInfo" alias="classInfo"/>
<typeAlias type="com.mybatis.entity.StudentInfo" alias="studentInfo"/>
</typeAliases>
<environments default="en">
<environment id="en">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.name}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
<!-- 映射配置文件 -->
<mappers>
<mapper resource="com/mybatis/entity/classInfo.xml"/>
<mapper resource="com/mybatis/entity/studentInfo.xml"/>
</mappers>
</configuration>
studentinfo.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">
<!-- com.mybatis.entity.StudentInfo -->
<mapper namespace="com.mybatis.entity.*">
<resultMap type="com.mybatis.entity.StudentInfo" id="stu">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="classid" column="classid"/>
<!-- 一对多的配置 -->
<association property="cs" javaType="com.mybatis.entity.ClassInfo" resultMap="class" />
</resultMap>
<!-- 查询所有 -->
<select id="queryAll" resultMap="stu">
select * from studentinfo a,classinfo b where a.classid=b.classid
</select>
</mapper>
classinfo.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.mybatis.entity.*">
<resultMap type="com.mybatis.entity.ClassInfo" id="class">
<result property="classid" column="classid"/>
<result property="classname" column="classname"/>
<!-- 多对一的配置 -->
<collection property="stu" ofType="com.mybatis.entity.StudentInfo" resultMap="stu"/>
</resultMap>
<!-- 查询 -->
<select id="queryCs" parameterType="int" resultMap="class">
select * from studentinfo a, classinfo b where a.classid=b.classid and a.classid=#{classid}
</select>
</mapper>
实体类studentInfo
package com.mybatis.entity;
public class StudentInfo {
private int id;
private String name;
private int age;
private String sex;
private int classid;
private ClassInfo cs;
public StudentInfo(){}
public StudentInfo(int id, String name, int age, String sex, int classid,
ClassInfo cs) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.classid = classid;
this.cs = cs;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getClassid() {
return classid;
}
public void setClassid(int classid) {
this.classid = classid;
}
public ClassInfo getCs() {
return cs;
}
public void setCs(ClassInfo cs) {
this.cs = cs;
}
}
classInfo实体类
package com.mybatis.entity;
import java.util.List;
import java.util.Set;
public class ClassInfo {
private int classid;
private String classname;
private List<StudentInfo> stu;
public ClassInfo(){}
public ClassInfo(int classid, String classname, List<StudentInfo> stu) {
super();
this.classid = classid;
this.classname = classname;
this.stu = stu;
}
public int getClassid() {
return classid;
}
public void setClassid(int classid) {
this.classid = classid;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public List<StudentInfo> getStu() {
return stu;
}
public void setStu(List<StudentInfo> stu) {
this.stu = stu;
}
}
测试:
/**
*
* 只用于测试
* @author Administrator
*
*/
public class StudentInfoDAO implements IStudentInfoDAO {
public static SqlSession getSession(){
Reader red=null;
try {
//读取配置文件
red=Resources.getResourceAsReader("configuration.xml");
//获取一个sqlsessionfactory
SqlSessionFactory fc=new SqlSessionFactoryBuilder().build(red);
//获取sqlsession
return fc.openSession();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//关闭session
public static void closeSession(SqlSession session){
session.close();
}
//查询学生
public List<StudentInfo> queryAll() {
SqlSession session=StudentInfoDAO.getSession();
//查询所有
List<StudentInfo> list=session.selectList("queryAll");
//关闭session
StudentInfoDAO.closeSession(session);
return list;
}
//根据班级id查询学生信息
public List<ClassInfo> queryCs(){
SqlSession session=StudentInfoDAO.getSession();
//查询所有
List<ClassInfo> list=session.selectList("queryCs",1);
//关闭session
StudentInfoDAO.closeSession(session);
return list;
}
public static void main(String[] args) {
StudentInfoDAO dao=new StudentInfoDAO();
// List<StudentInfo> list=dao.queryAll();
// for(StudentInfo stu:list){
// System.out.println("学生:"+stu.getName()+">>>>>>班级:"+stu.getCs().getClassname());
// }
List<ClassInfo> list=dao.queryCs();
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getClassid());
for(int j=0;j<list.get(i).getStu().size();j++){
System.out.println(list.get(i).getStu().get(j).getName());
}
}
}
}
MyBatis关联关系配置
最新推荐文章于 2022-07-23 19:12:11 发布