iBATIS入门知识进阶四
一、关联数据的存储查询方式,以班级和学生为例,
在学生多方添加班级一方的引用,班级一端增加对学生集合的引用,建立双向关联
实体类
public class Classes {private int id;private String name; private List students; //不可以用set否则会出错public List getStudents() {return students;}public void setStudents(List students) {this.students = students;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}public class Student {private int id;private String name;private String sex;private Classes classes;public Classes getClasses() {return classes;}public void setClasses(Classes classes) {this.classes = classes;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}
<typeAlias alias="Classes" type="com.lzh.ibatis.Classes"/><insert id="saveClasses" parameterkeyProperty="id">select @@identity as id//存储时必须将id返回,以备学生所用</selectKey></insert><resultMap id="classes-result"><result property="id"/><result property="name" column="cname"/><result property="students" column="id" select="findStudentByCid"/></resultMap><select id="findClassesById" resultMap="classes-result" parameterresultMap="classes-result" parametername="code"><typeAlias alias="Student" type="com.lzh.ibatis.Student"/> <insert id="saveStudent" parameterid="findStudent-result"><result property="id" column="id"/><result property="name" column="name"/><result property="sex" column="sex"/><result property="classes" column="cid" select="findClassesById"/></resultMap><select id="findStudent" resultMap="findStudent-result" parameterresultMap="findStudent-result" parametername="code"> <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMapConfig SYSTEM "http://ibatis.apache.org/dtd/sql-map-config-2.dtd" ><sqlMapConfig><settings lazyLoadingEnabled="true"/><transactionManager type="JDBC" commitRequired="false"><dataSource type="SIMPLE"><property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/><property name="JDBC.ConnectionURL" value="jdbc:mysql://127.0.0.1/ibatis"/><property name="JDBC.Username" value="root"/> <property name="JDBC.Password" value="123"/></dataSource></transactionManager> <sqlMap resource="com/lzh/ibatis/Student.xml"/> <sqlMap resource="com/lzh/ibatis/Classes.xml"/></sqlMapConfig>
public void testSaveStudent()throws Exception{SqlMapClient sqlMapper = IbatisSQLMapConfig.getSqlMapInstance();Classes classes = new Classes();classes.setName("测试班级"+ new Random().nextInt(9999));sqlMapper.insert("saveClasses", classes);Student student = new Student();student.setName("学生"+new Random().nextInt(9999));student.setSex("男");student.setClasses(classes);sqlMapper.insert("saveStudent",student);}public void testFindClasses()throws Exception{SqlMapClient sqlMapper = IbatisSQLMapConfig.getSqlMapInstance();Classes cla = (Classes)sqlMapper.queryForObject("findClasses",1);System.out.println(cla.getName()+"有学生人数:"+cla.getStudents().size());} public void testFindStudent()throws Exception{SqlMapClient sqlMapper = IbatisSQLMapConfig.getSqlMapInstance();Student stu = (Student)sqlMapper.queryForObject("findStudent",1);System.out.println(stu.getName()+"所属班级是:"+stu.getClasses().getName());}
CREATE TABLE t_student(id integer not null auto_increment,name varchar(255),sex varchar(255),cid integer,primary key(id));CREATE TABLE t_classes(id integer not null auto_increment,cname varchar(255),primary key(id))
/** * 基类 * @author Administrator * */public class Animal {private int id;private String name;private String sex;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}/** * Bird子类 * @author Administrator */public class Bird extends Animal{private int height;public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}}/** * Pig子类 * @author Administrator */public class Pig extends Animal{private int weight;public int getWeight() {return weight;}public void setWeight(int weight) {this.weight = weight;}}下面贴出Animal.xml配置文件的信息:
<sqlMap><typeAlias alias="Animal" type="com.lzh.ibatis.Animal"/><typeAlias alias="Bird" type="com.lzh.ibatis.Bird"/><typeAlias alias="Pig" type="com.lzh.ibatis.Pig"/><insert id="insertAnimal" parameterparameterparameterid="animal"><result property="id" column="id"/><result property="name" column="name"/><result property="sex" column="sex"/><discriminator column="animal_type" javaType="string"><subMap value="Bird" resultMap="bird"/><subMap value="Pig" resultMap="pig"/></discriminator></resultMap><resultMap id="bird" extends="animal"><result property="height" column="height"/></resultMap><resultMap id="pig" extends="animal"><result property="weight" column="weight"/></resultMap><select id="findAllAnimals" resultMap="animal">select * from t_animal</select></sqlMap>
<sqlMap resource="com/lzh/ibatis/Animal.xml"/>
public void testSaveAnimal() throws Exception{//从配置文件中得到SqlMapClient对象 SqlMapClient sqlMapper = IbatisSQLMapConfig.getSqlMapInstance();Animal a = new Animal();a.setName("动物");a.setSex("1");sqlMapper.insert("insertAnimal", a);Bird b = new Bird();b.setName("鸟");b.setSex("0");b.setHeight(1000);sqlMapper.insert("insertBird", b);Pig p = new Pig();p.setName("猪");p.setSex("1");p.setWeight(500);sqlMapper.insert("insertPig", p);}public void testFindAllAnimals() throws Exception{//从配置文件中得到SqlMapClient对象 SqlMapClient sqlMapper = IbatisSQLMapConfig.getSqlMapInstance();;List animals = sqlMapper.queryForList("findAllAnimals");for (Iterator iterator = animals.iterator(); iterator.hasNext();) {Animal a = (Animal) iterator.next();System.out.println(a.getName()+","+a);}}
create table t_animal(ID integer not null auto_increment,NAME varchar(255),SEX varchar(255),HEIGHT integer,WEIGHT integer,ANIMAL_TYPE varchar(20),primary key(ID));