mybatis 一对一关联
首先建表:
CREATE TABLE teacher( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name VARCHAR(20) ); CREATE TABLE class( c_id INT PRIMARY KEY AUTO_INCREMENT, c_name VARCHAR(20), teacher_id INT ); ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id); INSERT INTO teacher(t_name) VALUES('LS1'); INSERT INTO teacher(t_name) VALUES('LS2'); INSERT INTO class(c_name, teacher_id) VALUES('bj_a', 1); INSERT INTO class(c_name, teacher_id) VALUES('bj_b', 2);
再cn.beam包下,简历两个Class类。分别是Classes.java和Teacher.java
public class Classes { private int id; private String name; private Teacher teacher; } public class Teacher { private int id; private String name; }
然后分别构造方法,做get和set方法。
配置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="db.properties"/> <typeAliases> <typeAlias type="cn.bean.Classes" alias="Classes"/> <typeAlias type="cn.bean.Teacher" alias="Teacher"/> </typeAliases> <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="cn/test5/ClassMapper.xml"/> </mappers> </configuration>
配置db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=root
配置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="cn.test5.ClassMapper"> <!-- 根据班级id查询班级信息(带老师的信息) --> <select id="getClass" parameterType="int" resultMap="classResultMap"> SELECT * FROM class c ,teacher t WHERE c.c_id = t.t_id and c.c_id=#{id} </select> <resultMap type="Classes" id="classResultMap"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> </resultMap> </mapper>
做一个utils
import java.io.InputStream; import junit.framework.Test; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtils { public static SqlSessionFactory getFactory(){ String resource = "conf.xml"; InputStream is = Test.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); return factory; } }
做一个测试,测试结果:
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import cn.bean.Classes; import cn.utils.MyBatisUtils; public class Test5 { @Test public void selectTeacherByCid(){ SqlSessionFactory factory = MyBatisUtils.getFactory(); SqlSession session = factory.openSession(true); String statement = "cn.test5.ClassMapper.getClass"; Classes c = session.selectOne(statement, 1); System.out.println(c); } }
结果如下:
Classes [id=1, name=bj_a, teacher=Teacher [id=1, name=LS1]]