这里有新鲜出炉的MyBatis教程,程序狗速度看过来!
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。
这篇文章重点给大家介绍mybatis多对多映射,多对多映射的例子也很常见,本文通过代码实例相结合的方式给大家介绍mybatis多对多映射,需要的朋友参考下吧
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
在上篇文章给大家介绍MyBatis一对一映射初识教程。
下面给大家说下mybatis多对多映射知识,具体详情如下所示:
多对多的例子也不少,比如课程与学生之间的关系,一个课程可以有多个学生选修,而一个学生也可以选修多门学科。老师与学生之间的关系,一个老师有多个学生,一个学生有多个老师。
以学生和课程之间的关系为例。
我们建立数据表的时候有两种方案:
第一种:
在建立student数据表的时候,存放一个课程的外键字段,
在建立course数据表的时候,存放一个学生的外键字段。
但是这样是有很大弊端的,那就是如果我要删student表,却有course表的外键字段,
同理,我想删除course表的时候,却有student表的外键字段,哎,不好办啊。
第二种:
我们建立student和course表,在两张表中分别存放各自的字段和记录,
再常见一个student_course表,作为中间表,存放student和course的外键。
这样我们删除字表的时候很方便哦,所以采用这样方案。
数据库脚本
- -- 多对多映射
- -- 删除数据库
- drop database if exists mybatis;
- -- 创建数据库
- create database if not exists mybatis default character set utf8;
- -- 选择数据库
- use mybatis;
- -- 删除数据表
- drop table if exists student;
- drop table if exists course;
- drop table if exists student_course;
- -- 创建数据表
- create table student(
- sid int(255),
- sname varchar(32),
- constraint pk_sid primary key (sid)
- );
- create table course(
- cid int(255),
- cname varchar(32),
- constraint pk_cid primary key (cid)
- );
- create table student_course(
- sid int(255),
- cid int(255),
- constraint pk_sid_cid primary key(sid,cid),
- constraint fk_sid foreign key (sid) references student(sid),
- constraint fk_cid foreign key (cid) references course(cid)
- );
- -- 测试数据
- insert into student (sid,sname) values (1,'哈哈');
- insert into student (sid,sname) values (2,'呵呵');
- insert into course (cid,cname) values (1,'java');
- insert into course (cid,cname) values (2,'.NET');
- insert into student_course (sid,cid) values (1,1);
- insert into student_course (sid,cid) values (1,2);
- insert into student_course (sid,cid) values (2,1);
- insert into student_course (sid,cid) values (2,2);
新建many2many.Course.java类
- package many2many;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 课程
- * @author Administrator
- *
- */
- @SuppressWarnings("serial") public class Course implements Serializable {
- private Integer cid;
- private String cname;
- private List < Student > students = new ArrayList < Student > ();
- public Integer getCid() {
- return cid;
- }
- public void setCid(Integer cid) {
- this.cid = cid;
- }
- public String getCname() {
- return cname;
- }
- public void setCname(String cname) {
- this.cname = cname;
- }
- public List < Student > getStudents() {
- return students;
- }
- public void setStudents(List < Student > students) {
- this.students = students;
- }
- }
新建many2many.Student.java类
- package many2many;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 学生类
- * @author Administrator
- *
- */
- @SuppressWarnings("serial") public class Student implements Serializable {
- private Integer sid;
- private String sname;
- private List < Course > courses = new ArrayList < Course > ();
- public Integer getSid() {
- return sid;
- }
- public void setSid(Integer sid) {
- this.sid = sid;
- }
- public String getSname() {
- return sname;
- }
- public void setSname(String sname) {
- this.sname = sname;
- }
- public List < Course > getCourses() {
- return courses;
- }
- public void setCourses(List < Course > courses) {
- this.courses = courses;
- }
- }
新建StudentMapper.xml文件和CourseMapper.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="studentMapper">
- <resultMap type="many2many.Student" id="studentMap">
- <id property="sid" column="sid"/>
- <result property="sname" column="sname"/>
- </resultMap>
- </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="courseNamespace">
- <resultMap type="many2many.Course" id="courseMap">
- <id property="cid" column="cid"/>
- <result property="cname" column="cname"/>
- </resultMap>
- <!-- 查询“哈哈”选修了那几门课程 -->
- <select id="findAllByName" parameterType="string" resultMap="courseMap">
- select c.cname,c.cid
- from student s,course c,student_course sc
- where s.sid = sc.sid and c.cid = sc.cid and s.sname = #{sname};
- </select>
- </mapper>
新建持久层类StudentCourseDAO.java类
- package many2many;
- import java.util.Iterator;
- import java.util.List;
- import one2many.Student;
- import org.apache.ibatis.session.SqlSession;
- import org.junit.Test;
- import util.MyBatisUtil;
- public class StudentCourseDAO {
- /**
- * 查询“哈哈”选修了那几门课程
- * @param name 学生的姓名
- * @return
- * @throws Exception
- */
- public List < Course > findAllByName(String name) throws Exception {
- SqlSession sqlSession = null;
- try {
- sqlSession = MyBatisUtil.getSqlSession();
- return sqlSession.selectList("courseNamespace.findAllByName", name);
- } catch(Exception e) {
- e.printStackTrace();
- throw e;
- } finally {
- MyBatisUtil.closeSqlSession();
- }
- }@Test public void testFindAllByName() throws Exception {
- StudentCourseDAO dao = new StudentCourseDAO();
- List < Course > courses = dao.findAllByName("哈哈");
- for (Course course: courses) {
- System.out.println(course.getCid() + ":" + course.getCname());
- }
- }
- }
在mybatis.cfg.xml文件中加载配置文件
- <!-- 加载映射文件 -->
- <mappers>
- <mapper resource="one2one/CardMapper.xml" />
- <mapper resource="one2one/StudentMapper.xml" />
- <mapper resource="one2many/GradeMapper.xml" />
- <mapper resource="one2many/StudentMapper.xml" />
- <mapper resource="many2many/StudentMapper.xml" />
- <mapper resource="many2many/CourseMapper.xml" />
- </mappers>
以上所述是小编给大家介绍的MyBatis多对多映射初识教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHPERZ网站的支持!
来源: http://www.phperz.com/article/17/1211/360160.html