作为一个半自动的ORM框架,mybatis确实足够强大,那么平时在运用mybatis框架的时候,你是不是也清楚它的一些重要关系呢?比如说一对多是什么?有什么作用呢?有兴趣的小伙伴可以跟小编一起来看看哦。
比如说有这样一个需求:一个班级有多个学生,查询java学科有哪些学生信息 ;首先设计数据库表:
create table grades( gid int(5) primary key, gname varchar(10) ); create table students( sid int(5) primary key, sname varchar(10), sgid int(5), constraint sgid_fk foreign key(sgid) references grades(gid) ); insert into grades(gid,gname) values(1,'java'); insert into students(sid,sname,sgid) values(1,'哈哈',1); insert into students(sid,sname,sgid) values(2,'呵呵',1); select * from grades; select * from students;
实体类:
package zhongfucheng; import java.util.ArrayList; import java.util.List; /** * 学科(单方) */ public class Grade { private Integer id; private String name; private ListstudentList = new ArrayList(); //关联属性 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 ListgetStudentList() { return studentList; } public void setStudentList(ListstudentList) { this.studentList = studentList; } } package zhongfucheng; /** * 学生(多方) */ 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; } }
映射文件SQL语句如下所示:
<mapper namespace="studentNamespace"> <resultMap type="zhongfucheng2.Student" id="studentMap"> <id property="id" column="sid"/> <result property="name" column="sname"/> </resultMap> <!--查询选修的java学科有多少位学生--> <!--由于我们只要查询学生的名字,而我们的实体studentMap可以封装学生的名字,那么我们返回studentMap即可,并不需要再关联到学科表--> <select id="findByGrade" parameterType="string" resultMap="studentMap"> select s.sname,s.sid from zhongfucheng.students s,zhongfucheng.grades g WHERE s.sgid=g.gid and g.gname=#{name}; </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="gradeNamespace"> <resultMap type="zhongfucheng2.Grade" id="gradeMap"> <id property="id" column="gid"/> <result property="name" column="gname"/> </resultMap> </mapper>
DAO层:
public ListfindByGrade(String grade) throws Exception { //得到连接对象 SqlSession sqlSession = MybatisUtil.getSqlSession(); try { return sqlSession.selectList("studentNamespace.findByGrade", grade); /* sqlSession.commit();*/ } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); throw e; } finally { MybatisUtil.closeSqlSession(); } } public static void main(String[] args) throws Exception { StudentDao studentDao = new StudentDao(); Liststudent = studentDao.findByGrade("java"); for (Student student1: student) { System.out.println(student1.getName()); } }
好了,以上就是本篇文章的所有内容了,如果还想了解更多java架构师相关信息,记得马上来关注本站消息,更多精彩内容等你来解锁!