您的位置:首页 > 大数据 > 人工智能

mybatis---demo1--(1-n)----bai

2017-01-12 18:17 260 查看
实体类1:

package com.etc.entity;

import java.util.List;

public class Teacher
{
private int tid;
private String tname;
private String sex;
private List<Student> students;
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher [sex=" + sex + ", tid=" + tid + ", tname=" + tname
+ "]";
}
}
=================================================
实体类2:

package com.etc.entity;

public class Student
{
private int sid;
private String sname;
private String sex;
private Teacher teacher;//外键的关联对象
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student [sex=" + sex + ", sid=" + sid + ", sname=" + sname
+ "]";
}
}
==================================================================
dao类1:

package com.etc.dao;

import java.util.List;

import com.etc.entity.Teacher;

public interface TeacherDao {
Teacher findById(int id);
List<Teacher> findAll();
}
===========================================
dao类2:

package com.etc.dao;

import com.etc.entity.Student;

public interface StudentDao
{
Student findById(int id);
}
=============================================
工具类:

package com.etc.utils;

import java.io.InputStream;
import java.sql.Connection;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.lf5.util.Resource;

//实现获取、释放mybatis数据库连接的工具类
public class MyBatisSessionFactory {
//定义常量
private static String CONFIG_FILE_LOCATION="mybatis-config.xml";

//考虑到该工具类允许被多线程执行。因为封装1个线程池,让每个线程从线程池中获取1个连接。让1个线程对应
//1条数据库连接,这样更安全
//ThreadLocal的作用:让"线程"绑定"资源",这样就不会出现多个线程同享资源的情况。更安全。牺牲内存,换取”安全“
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();

private static InputStream is; //用于读取配置文件的流对象

private static SqlSessionFactory fac;//用于管理多个连接的工厂。一个工厂对应1个数据库。

//在该类的静态段中加载配置文件,这样可以确保只执行1次。
static
{
try {
is = Resources.getResourceAsStream(CONFIG_FILE_LOCATION);//读取配置文件
fac = new SqlSessionFactoryBuilder().build(is);//通过配置文件创建1个连接工厂
} catch (Exception e)
{
e.printStackTrace();
}

}
//获取1条连接
public static SqlSession getSession()
{
SqlSession s  = threadLocal.get(); //找线程池要1条连接
if(s==null) //第1次时,拿不到,则由工厂获取1条连接并放入线程池
{
s = fac.openSession();//由工厂获取1条连接并放入线程池
threadLocal.set(s);//放入线程池
}
return s;
}

//关闭连接
public static void closeSession()
{
SqlSession s  = threadLocal.get();//找线程池要本线程对应的连接
threadLocal.set(null);//将该连接从线程池中清除
if(s!=null)
s.close();//物理关闭连接
}
}

==================================================
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>
<typeAliases>
<typeAlias type="com.etc.entity.Student" alias="Student"/>
<typeAlias type="com.etc.entity.Teacher" alias="Teacher"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="url" value="jdbc:mysql://127.0.0.1/java?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="driver" value="com.mysql.jdbc.Driver"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/etc/mapper/Teacher-mapper.xml"/>
<mapper resource="com/etc/mapper/Student-mapper.xml"/>
</mappers>
</configuration>
===================================================
Student-mapper.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.etc.dao.StudentDao">
<resultMap type="Student" id="StudentMap">
<id column="sid" property="sid" />
<result column="sname" property="sname" />
<result column="sex" property="sex" />
<association property="teacher" resultMap="com.etc.dao.TeacherDao.TeacherMap"/>
</resultMap>
<select id="findById" parameterType="java.lang.Integer"
resultMap="StudentMap">
select t.tid,t.tname,t.sex,s.sid,s.sname,s.sex,s.tid
from
teacher t,student s where t.tid = s.tid
and t.tid = #{id}
</select>
</mapper>
=======================================
Teacher-mapper.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.etc.dao.TeacherDao">
<resultMap type="Teacher" id="TeacherMap">
<id column="tid" property="tid" />
<result column="tname" property="tname"/>
<result column="sex" property="sex"/>
<collection property="students" resultMap="com.etc.dao.StudentDao.StudentMap"/>
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="TeacherMap">
select t.tid,t.tname,t.sex,s.sid,s.sname,s.sex,s.tid
from teacher t,student s where t.tid = s.tid
and t.tid = #{id}
</select>
<select id="findAll"  resultMap="TeacherMap">
select t.tid,t.tname,t.sex,s.sid,s.sname,s.sex,s.tid
from teacher t,student s where t.tid = s.tid
</select>
</mapper>
====================================================
log4j.properties  对log4j这个jar进行文件配置:

log4j.rootLogger=WARN, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
=========================================================================
测试类:

package com.etc.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.etc.dao.TeacherDao;
import com.etc.entity.Student;
import com.etc.entity.Teacher;
import com.etc.utils.MyBatisSessionFactory;
public class TestMybatis
{
@Test
public void testTeacherDao()
{
//1 获取连接
SqlSession s = MyBatisSessionFactory.getSession();
//2 执行查询
Teacher t = s.getMapper(com.etc.dao.TeacherDao.class).findById(1);
System.out.println("老师是"+t);
System.out.println("学生列表如下:");
for(Student stu:t.getStudents())
{
System.out.println(stu);
}
/*List<Teacher> list = s.getMapper(com.etc.dao.TeacherDao.class).findAll();
for(Teacher t:list)
System.out.println(t);*/
//4 关闭
MyBatisSessionFactory.closeSession();
}
}
=========================================================================


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: