Mybatis一對多關係java
1、數據庫與數據表的創建mysql
學生與班級是一對多的關係,首先建立一個學生表sql
create table tb_student( -> id int not null auto_increment, -> name varchar(10), -> sex bool default true, -> age int, -> address varchar(100), -> telphone varchar(100), -> classmateid int not null, -> primary key(id) -> );
添加學生信息數據庫
insert into tb_student(name,age,address,telphone) values('張三丰',108,'武當山','13262183425'); insert into tb_student(name,age,address,telphone) values('張無忌',24,'光明頂','13423678543');
其次,建立班級表apache
create table tb_classmate( -> id int not null auto_increment, -> name varchar(20), -> primary key(id) -> );
添加測試數據session
insert into tb_classmate(name) values('武功速成171班');
2、建立Java項目mybatis
1.配置數據庫鏈接信息app
1)mybatis-config.xml文件,內容以下:測試
<?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> <properties resource="msqldatasource.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <!-- <mapper resource="com/sky/demo/dao/AccountMapper.xml" /> --> <package name="com.sky.dao"/> </mappers> </configuration>
數據庫鏈接信息msqldatasource.properties內容以下:url
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatisstudy username=root password=123456
3、建立Java bean
1.Student.java內容以下
public class Student { private int id; private String name; private boolean sex; private int age; private String address; private String telphone; private int classmateid; private ClassMate classMate; //省略其setter和getter方法 }
2.ClassMate.java內容以下:
public class ClassMate { private int id; private String name; private List<Student> students; //省略其setter和getter方法 }
4、建立數據訪問層
1.建立Student數據訪問層接口
package com.sky.dao; import com.sky.bean.Student; import java.util.List; public interface StudentMapper { /** * 根據班級編號取得全部的學生信息 * @param id 班級編號 * @return */ public List<Student> getStudentsByClassMateId(int id); }
2.建立Student的SQL映射
<?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.sky.dao.StudentMapper"> <resultMap id="studentMap" type="com.sky.bean.Student"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="sex" property="sex"/> <result column="age" property="age"/> <result column="address" property="address"/> <result column="telphone" property="telphone"/> </resultMap> <select id="getStudentsByClassMateId" resultMap="studentMap"> select * from tb_student where classmateid = #{id} </select> </mapper>
3.建立ClassMate數據訪問層接口
package com.sky.dao; import com.sky.bean.ClassMate; public interface ClassMateMapper { /** * 根據班級id取得班級的信息 * @param id 班級id * @return 返回該班級的信息 */ public ClassMate getClassMateById(int id); }
4.建立ClassMate的SQL映射
<?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.sky.dao.ClassMateMapper"> <resultMap id="classMateMap" type="com.sky.bean.ClassMate"> <id column="id" property="id"/> <result column="name" property="name"/> <collection property="students" column="id" ofType="Student" select="com.sky.dao.StudentMapper.getStudentsByClassMateId"> </collection> </resultMap> <select id="getClassMateById" resultMap="classMateMap"> select * from tb_classmate where id = #{id} </select> </mapper>
此是一對多關係的實現,在ClassMate的映射中,
<collection property="students" column="id" ofType="Student" select="com.sky.dao.StudentMapper.getStudentsByClassMateId"> </collection>
爲取得學生表中的信息,其中property的值與ClassMateMapper.java中的屬性名相同,column的值,由於取得學生信息,是根據班級表的id信息取得的,因此此處的值爲 id,select的值,注意要寫全類名,不然,會找不到相應的映射信息。
最後,執行結果以下:
package com.sky; import com.sky.bean.ClassMate; import com.sky.dao.ClassMateMapper; import com.sky.util.DBUtil; import org.apache.ibatis.session.SqlSession; import java.util.List; public class MyDemo1 { public static void main(String[] args) { SqlSession session=DBUtil.getSession(); ClassMateMapper mateMapper=session.getMapper(ClassMateMapper.class); ClassMate classMate=mateMapper.getClassMateById(1); System.out.println(classMate.toString()); } } ClassMate{id=1, name='武功速成171班', students=[Student{id=1, name='張三丰', sex=true, age=108, address='武當山', telphone='13262183425', classmateid=1, classMate=null}, Student{id=2, name='張無忌', sex=true, age=24, address='光明頂', telphone='13423678543', classmateid=1, classMate=null}]}
5、多對一的關係
1.修改Student的接口與映射文件
package com.sky.dao; import com.sky.bean.Student; import java.util.List; public interface StudentMapper { /** * 根據班級編號取得全部的學生信息 * @param id 班級編號 * @return */ public List<Student> getStudentsByClassMateId(int id); /** * 根據學生編號取得學生信息 * @param id 學生編號 * @return 學生信息 */ public Student getStudentById(int id); } <?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.sky.dao.StudentMapper"> <resultMap id="studentMap" type="com.sky.bean.Student"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="sex" property="sex"/> <result column="age" property="age"/> <result column="address" property="address"/> <result column="telphone" property="telphone"/> <association property="classMate" column="classmateid" select="com.sky.dao.ClassMateMapper.getClassMateById"> </association> </resultMap> <select id="getStudentsByClassMateId" resultMap="studentMap"> select * from tb_student where classmateid = #{id} </select> <select id="getStudentById" resultMap="studentMap"> select * from tb_student where id=#{id} </select> </mapper>
運行結果以下:
package com.sky; import com.sky.bean.ClassMate; import com.sky.bean.Student; import com.sky.dao.ClassMateMapper; import com.sky.dao.StudentMapper; import com.sky.util.DBUtil; import org.apache.ibatis.session.SqlSession; import java.util.List; public class MyDemo1 { public static void main(String[] args) { SqlSession session=DBUtil.getSession(); StudentMapper studentMapper=session.getMapper(StudentMapper.class); Student stu=studentMapper.getStudentById(1); ClassMate classMate=stu.getClassMate(); System.out.println(stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress()); System.out.println(classMate.getName()); /* ClassMateMapper mateMapper=session.getMapper(ClassMateMapper.class); ClassMate classMate=mateMapper.getClassMateById(1); System.out.println(classMate.toString()); */ } } 張三丰 108 武當山 武功速成171班