基于一对一/多的mybatis案例
学生
1
n
老师
1
1
班级
1、建立以及pojo类
package com.briup.pojo.many2one;
/*
create table clazz_t(
c_id number(10) primary key,
c_name varchar(10),
t_id number(10) references Teacher_t(t_id)
);
insert into clazz_t values(1,'c1',1);
insert into clazz_t values(2,'c2',2);
insert into clazz_t values(3,'c3',3);
insert into clazz_t values(4,'c4',4);
*/
/*
*
* 一个老师对应一个教室
* 有教室维护关系
* */
public class Clazz {
private int id;
private String name;
private Teacher teacher;
public Clazz() {
super();
}
public Clazz(int id, String name, Teacher teacher) {
super();
this.id = id;
this.name = name;
this.teacher = teacher;
}
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 Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Clazz [id=" + id + ", name=" + name + ", teacher=" + teacher + "]";
}
}
package com.briup.pojo.many2one;
import java.util.List;
/*
create table Teacher_t(
t_id number(10) primary key,
t_name varchar(10),
s_id number(10) references Student_t(s_id)
)
insert into Teacher_t values(1,'t1_1',1);
insert into Teacher_t values(2,'t2_3',3);
insert into Teacher_t values(3,'t3_1',1);
insert into Teacher_t values(4,'t4_2',2);
insert into Teacher_t values(5,'t5_3',3);
*/
public class Teacher {
private int id;
private String name;
private int s_id;
public Teacher(int id, String name, int s_id) {
super();
this.id = id;
this.name = name;
this.s_id = s_id;
}
public Teacher() {
super();
}
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 int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", s_id=" + s_id + "]";
}
}
package com.briup.pojo.many2one;
import java.util.List;
/*
create table Student_t(
s_id number(10) primary key,
s_name varchar(10)
)
insert into student_t values(1,'s1')
insert into student_t values(2,'s2')
insert into student_t values(3,'s3')
* */
public class Student {
private int id;
private String name;
private List<Teacher> list;
public Student() {
super();
}
public Student(int id, String name, List<Teacher> list) {
super();
this.id = id;
this.name = name;
this.list = list;
}
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 List<Teacher> getList() {
return list;
}
public void setList(List<Teacher> list) {
this.list = list;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", list=" + list + "]";
}
}
2、编写db.propertise文件
db.properties
dirver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=briup
password=briup
3、编写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">
<!-- development :开发模式 work :工作模式 environments default="development"与environment
id="development"中的default和id需要一致,亲自测试,不一致不行 -->
<configuration>
<properties resource="db.properties"></properties><!-- 导入db.properties资源文件 -->
<!-- <typeHandlers> <typeHandler handler="com.briup.pojo.many2one.TeacherHandle"
javaType="com.briup.pojo.many2one.Teacher" /> </typeHandlers> -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
</transactionManager><!-- JDBC/MANAGED -->
<dataSource type="POOLED"><!--unpooled不使用连接池/pooled使用连接池 -->
<property name="driver" value="${dirver}" /><!--数据库的dirver -->
<property name="url" value="${url}" /><!--连接数据库的url -->
<property name="username" value="${username}" /><!--数据库用户名 -->
<property name="password" value="${password}" /><!--对应用户名的密码 -->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/briup/maps/My_StudentMap.xml" />
<mapper class="com.briup.maps_anntation.My_StudentMap" />
<mapper resource="com/briup/pojo/many2one/student.xml" />
<mapper resource="com/briup/pojo/many2one/clazz.xml" />
</mappers>
</configuration>
4、编写map配置文件
clazzMap.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="com.briup.pojo.many2one.Clazz">
<resultMap type="com.briup.pojo.many2one.Clazz" id="Clazz">
<id property="id" column="c_id" />
<result property="name" column="t_name" />
<association property="teacher" javaType="com.briup.pojo.many2one.Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_id" />
<result property="s_id" column="s_id" />
</association>
</resultMap>
<select id="getClass" resultMap="Clazz" parameterType="int"> select *
from clazz_t c,teacher_t t where c.t_id = t.t_id
and c.c_id = #{id}
</select>
</mapper>
student.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="com.briup.pojo.many2one.Student">
<resultMap type="com.briup.pojo.many2one.Student" id="student">
<id property="id" column="s_id" />
<result property="name" column="s_name" />
<!-- collection表示在一的一方有多方的集合, property 为集合属性名,ofType表示集合元素的类型,也就是多方的类型 -->
<collection property="list" ofType="com.briup.pojo.many2one.Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_name" />
<result property="s_id" column="s_id" />
</collection>
</resultMap>
<select id="selectStudentByid" resultMap="student"
parameterType="int">
select s.s_id,s.s_name,t.t_id,t.t_name,t.s_id from student_t s,teacher_t t where s.s_id
=t.s_id and s.s_id = #{id}
</select>
</mapper>
4、编写util类,用来获取sessionFactory对象,把其写成单例模式
package com.briup.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisSqlSessionFactory {
private static SqlSessionFactory factory;
private MybatisSqlSessionFactory() {
}
public static SqlSessionFactory getSqlSessionFactory() {
// 为了保证factory为单例,所以判断factory是否为空,如果不为空,直接返回
if (factory != null)
return factory;
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("conf.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
factory = builder.build(inputStream);
return factory;
}
// openSession为重载方法,如果为无参的,就使用默认的commit机制,也就是自动提交
public static SqlSession openSession(boolean flag) {
SqlSession session = MybatisSqlSessionFactory.getSqlSessionFactory().openSession();
session.commit(flag);
return session;
}
public static SqlSession openSession() {
SqlSession session = factory.openSession();
return session;
}
}
5、编写测试类
package com.briup.pojo.many2one;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.briup.util.MybatisSqlSessionFactory;
public class TestMany2One {
@Test
public void getStudent() {
try {
SqlSession session = MybatisSqlSessionFactory.openSession(false);
String statment = "com.briup.pojo.many2one.Student.selectStudentByid";
Student student = session.selectOne(statment, 1);
session.close();
System.out.println(student);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Test
public void getClazz() {
try {
SqlSession session = MybatisSqlSessionFactory.openSession(false);
String statment = "com.briup.pojo.many2one.Clazz.getClass";
Clazz clazz = session.selectOne(statment, 1);
session.close();
System.out.println(clazz);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
学生
1
n
老师
1
1
班级
1、建立以及pojo类
package com.briup.pojo.many2one;
/*
create table clazz_t(
c_id number(10) primary key,
c_name varchar(10),
t_id number(10) references Teacher_t(t_id)
);
insert into clazz_t values(1,'c1',1);
insert into clazz_t values(2,'c2',2);
insert into clazz_t values(3,'c3',3);
insert into clazz_t values(4,'c4',4);
*/
/*
*
* 一个老师对应一个教室
* 有教室维护关系
* */
public class Clazz {
private int id;
private String name;
private Teacher teacher;
public Clazz() {
super();
}
public Clazz(int id, String name, Teacher teacher) {
super();
this.id = id;
this.name = name;
this.teacher = teacher;
}
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 Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Clazz [id=" + id + ", name=" + name + ", teacher=" + teacher + "]";
}
}
package com.briup.pojo.many2one;
import java.util.List;
/*
create table Teacher_t(
t_id number(10) primary key,
t_name varchar(10),
s_id number(10) references Student_t(s_id)
)
insert into Teacher_t values(1,'t1_1',1);
insert into Teacher_t values(2,'t2_3',3);
insert into Teacher_t values(3,'t3_1',1);
insert into Teacher_t values(4,'t4_2',2);
insert into Teacher_t values(5,'t5_3',3);
*/
public class Teacher {
private int id;
private String name;
private int s_id;
public Teacher(int id, String name, int s_id) {
super();
this.id = id;
this.name = name;
this.s_id = s_id;
}
public Teacher() {
super();
}
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 int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", s_id=" + s_id + "]";
}
}
package com.briup.pojo.many2one;
import java.util.List;
/*
create table Student_t(
s_id number(10) primary key,
s_name varchar(10)
)
insert into student_t values(1,'s1')
insert into student_t values(2,'s2')
insert into student_t values(3,'s3')
* */
public class Student {
private int id;
private String name;
private List<Teacher> list;
public Student() {
super();
}
public Student(int id, String name, List<Teacher> list) {
super();
this.id = id;
this.name = name;
this.list = list;
}
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 List<Teacher> getList() {
return list;
}
public void setList(List<Teacher> list) {
this.list = list;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", list=" + list + "]";
}
}
2、编写db.propertise文件
db.properties
dirver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=briup
password=briup
3、编写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">
<!-- development :开发模式 work :工作模式 environments default="development"与environment
id="development"中的default和id需要一致,亲自测试,不一致不行 -->
<configuration>
<properties resource="db.properties"></properties><!-- 导入db.properties资源文件 -->
<!-- <typeHandlers> <typeHandler handler="com.briup.pojo.many2one.TeacherHandle"
javaType="com.briup.pojo.many2one.Teacher" /> </typeHandlers> -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
</transactionManager><!-- JDBC/MANAGED -->
<dataSource type="POOLED"><!--unpooled不使用连接池/pooled使用连接池 -->
<property name="driver" value="${dirver}" /><!--数据库的dirver -->
<property name="url" value="${url}" /><!--连接数据库的url -->
<property name="username" value="${username}" /><!--数据库用户名 -->
<property name="password" value="${password}" /><!--对应用户名的密码 -->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/briup/maps/My_StudentMap.xml" />
<mapper class="com.briup.maps_anntation.My_StudentMap" />
<mapper resource="com/briup/pojo/many2one/student.xml" />
<mapper resource="com/briup/pojo/many2one/clazz.xml" />
</mappers>
</configuration>
4、编写map配置文件
clazzMap.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="com.briup.pojo.many2one.Clazz">
<resultMap type="com.briup.pojo.many2one.Clazz" id="Clazz">
<id property="id" column="c_id" />
<result property="name" column="t_name" />
<association property="teacher" javaType="com.briup.pojo.many2one.Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_id" />
<result property="s_id" column="s_id" />
</association>
</resultMap>
<select id="getClass" resultMap="Clazz" parameterType="int"> select *
from clazz_t c,teacher_t t where c.t_id = t.t_id
and c.c_id = #{id}
</select>
</mapper>
student.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="com.briup.pojo.many2one.Student">
<resultMap type="com.briup.pojo.many2one.Student" id="student">
<id property="id" column="s_id" />
<result property="name" column="s_name" />
<!-- collection表示在一的一方有多方的集合, property 为集合属性名,ofType表示集合元素的类型,也就是多方的类型 -->
<collection property="list" ofType="com.briup.pojo.many2one.Teacher">
<id property="id" column="t_id" />
<result property="name" column="t_name" />
<result property="s_id" column="s_id" />
</collection>
</resultMap>
<select id="selectStudentByid" resultMap="student"
parameterType="int">
select s.s_id,s.s_name,t.t_id,t.t_name,t.s_id from student_t s,teacher_t t where s.s_id
=t.s_id and s.s_id = #{id}
</select>
</mapper>
4、编写util类,用来获取sessionFactory对象,把其写成单例模式
package com.briup.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisSqlSessionFactory {
private static SqlSessionFactory factory;
private MybatisSqlSessionFactory() {
}
public static SqlSessionFactory getSqlSessionFactory() {
// 为了保证factory为单例,所以判断factory是否为空,如果不为空,直接返回
if (factory != null)
return factory;
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("conf.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
factory = builder.build(inputStream);
return factory;
}
// openSession为重载方法,如果为无参的,就使用默认的commit机制,也就是自动提交
public static SqlSession openSession(boolean flag) {
SqlSession session = MybatisSqlSessionFactory.getSqlSessionFactory().openSession();
session.commit(flag);
return session;
}
public static SqlSession openSession() {
SqlSession session = factory.openSession();
return session;
}
}
5、编写测试类
package com.briup.pojo.many2one;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.briup.util.MybatisSqlSessionFactory;
public class TestMany2One {
@Test
public void getStudent() {
try {
SqlSession session = MybatisSqlSessionFactory.openSession(false);
String statment = "com.briup.pojo.many2one.Student.selectStudentByid";
Student student = session.selectOne(statment, 1);
session.close();
System.out.println(student);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Test
public void getClazz() {
try {
SqlSession session = MybatisSqlSessionFactory.openSession(false);
String statment = "com.briup.pojo.many2one.Clazz.getClass";
Clazz clazz = session.selectOne(statment, 1);
session.close();
System.out.println(clazz);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}