标签:array pack bat oct try from his 持久 sel
学科
package cn.itcast.javaee.mybatis.one2many; import java.util.ArrayList; import java.util.List; /** * 学科(单方) * @author AdminTC */ public class Grade { private Integer id; private String name; private List<Student> studentList = new ArrayList<Student>();//关联属性 public Grade(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } }
学生
package cn.itcast.javaee.mybatis.one2many; /** * 学生(多方) * @author AdminTC */ public class Student { private Integer id; private String name; private Grade grade;//关联属性 public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } }
GradeMapper.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="gradeNamespace"> <resultMap type="cn.itcast.javaee.mybatis.one2many.Grade" id="gradeMap"> <id property="id" column="gid"/> <result property="name" column="gname"/> </resultMap> <!-- 查询哈哈是哪个学科的 --> <select id="findByName" parameterType="string" resultMap="gradeMap"> select g.gname from students s inner join grades g on s.sgid = g.gid and s.sname = #{xxxxx} </select> </mapper>
StudentMapper.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="studentNamespace"> <resultMap type="cn.itcast.javaee.mybatis.one2many.Student" id="studentMap"> <id property="id" column="sid"/> <result property="name" column="sname"/> </resultMap> <!-- 查询java学科有哪些学生信息 --> <select id="findAllByName" parameterType="string" resultMap="studentMap"> select s.sid,s.sname from students s inner join grades g on s.sgid = g.gid and g.gname = #{name} </select> </mapper>
GradeStudentDao
package cn.itcast.javaee.mybatis.one2many; import java.util.List; import org.apache.ibatis.session.SqlSession; import cn.itcast.javaee.mybatis.util.MybatisUtil; /** * 持久层 * @author AdminTC */ public class GradeStudentDao { /** * 查询java学科有哪些学生信息 * @param name 表示学科名 */ public List<Student> findAllByName(String name) throws Exception{ SqlSession sqlSession = null; try{ sqlSession = MybatisUtil.getSqlSession(); return sqlSession.selectList("studentNamespace.findAllByName",name); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MybatisUtil.closeSqlSession(); } } /** * 查询哈哈是哪个学科的 * @param name 表示学生姓名 */ public Grade findByName(String name) throws Exception{ SqlSession sqlSession = null; try{ sqlSession = MybatisUtil.getSqlSession(); return sqlSession.selectOne("gradeNamespace.findByName",name); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MybatisUtil.closeSqlSession(); } } public static void main(String[] args) throws Exception{ GradeStudentDao dao = new GradeStudentDao(); List<Student> studentList = dao.findAllByName("java"); System.out.println("java学科有"+studentList.size()+"个学生,它们信息如下:"); for(Student s : studentList){ System.out.println(s.getId()+":"+s.getName()); } System.out.println("-----------------------------------------------------------"); Grade grade = dao.findByName("哈哈"); System.out.println("哈哈是"+grade.getName()+"学科的"); System.out.println("-----------------------------------------------------------"); grade = dao.findByName("呵呵"); System.out.println("呵呵是"+grade.getName()+"学科的"); } }
标签:array pack bat oct try from his 持久 sel
原文地址:https://www.cnblogs.com/abernhong/p/9131420.html