不知道是哪里出了错误,自己排查了好久还是找不出来
package com.imooc.student;
import com.imooc.student.entity.Classes1;
import com.imooc.student.utils.StudentUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class Students1Testor {
@Test
public void testOneToMany() throws Exception{
SqlSession session=null;
try{
session= StudentUtils.openSession();
List<Classes1> list=session.selectList("classes1.selectOneToMany","一班");
for(Classes1 c:list){
System.out.println(c.getName()+" "+c.getStudents1s().toString());
}
}catch (Exception e){
throw e;
}finally {
StudentUtils.closeSession(session);
}
}
}
package com.imooc.student.entity;
import java.util.List;
public class Classes1 {
private Integer id;
private String classNo;
private String name;
private String major;
private List<Students1> students1s;
public List<Students1> getStudents1s() {
return students1s;
}
public void setStudents1s(List<Students1> students1s) {
this.students1s = students1s;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getClassNo() {
return classNo;
}
public void setClassNo(String classNo) {
this.classNo = classNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
package com.imooc.student.entity;
public class Students1 {
private Integer id;
private String stuNo;
private String stuName;
private String sex;
private String classNo;
private Classes1 classes1;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getClassNo() {
return classNo;
}
public void setClassNo(String classNo) {
this.classNo = classNo;
}
public Classes1 getClasses1() {
return classes1;
}
public void setClasses1(Classes1 classes1) {
this.classes1 = classes1;
}
}
<?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="students1">
<select id="selectByClassNo" parameterType="String" resultType="com.imooc.student.entity.Students1">
select * from students1 where class_no=#{value}
</select>
</mapper>
<?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="classes1">
<resultMap id="rmClasses1" type="com.imooc.student.entity.Classes1">
<id column="id" property="id"/>
<result column="class_no" property="classNo"/>
<collection property="students1" select="selectByClassNo" column="class_no"></collection>
</resultMap>
<select id="selectOneToMany" resultMap="rmClasses1">
select * from classes1 where name=#{value}
</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>
<settings>
<!--goods_id ==> goodsId驼峰命名转换-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--设置默认指向的数据库-->
<environments default="dev">
<!--配置环境,不同的环境不同的id名字,名字最好是有意义的-->
<environment id="dev">
<!--采用JDBC方式对数据库事物进行commit/rollback-->
<transactionManager type="JDBC"></transactionManager>
<!--采用连接池方式管理数据库连接-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="154672"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/goods.xml"/>
<mapper resource="mappers/student.xml"/>
<mapper resource="mappers/goods_detail.xml"/>
<mapper resource="mappers/classes1.xml"/>
<mapper resource="mappers/students1.xml"/>
</mappers>
</configuration>
package com.imooc.student.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
public class StudentUtils {
private static SqlSessionFactory sqlSessionFactory = null;
static {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
}catch (IOException e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static SqlSession openSession(){
return sqlSessionFactory.openSession();
}
public static void closeSession(SqlSession session){
if(session!=null){
session.close();
}
}
}
32
收起
正在回答 回答被采纳积分+1
相似问题
登录后可查看更多问答,登录/注册
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星