还是延续之前的空maven工程,新建moudle,命名为mybatis-08
项目总pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shrimpking</groupId>
<artifactId>mybatistest02</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis-01</module>
<module>mybatis-01-self</module>
<module>mybatis-02</module>
<module>mybatis-03</module>
<module>mybatis-04</module>
<module>mybatis-05</module>
<module>mybatis-05</module>
<module>mybatis-06</module>
<module>mybatis-07</module>
<module>mybatis-08</module>
<module>mybatis-09-blog</module>
</modules>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
项目具体情况截图
下面依次贴出代码
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="db.properties"/>
<typeAliases>
<typeAlias type="com.shrimpking.pojo.Student" alias="Student"/>
<typeAlias type="com.shrimpking.pojo.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="com/shrimpking/dao/TeacherMapper.xml"/>
<mapper resource="com/shrimpking/dao/StudentMapper.xml"/>
</mappers>
</configuration>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username=root
password=mysql123
MyBatisUtils.java
package com.shrimpking.utils;
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 java.io.InputStream;
/**
* @author user1
*/
public class MyBatisUtils
{
private static SqlSessionFactory sqlSessionFactory;
static {
try
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static SqlSession getSqlSession()
{
return sqlSessionFactory.openSession();
}
}
Student.java
package com.shrimpking.pojo;
public class Student
{
private int id;
private String name;
private int tid;
public Student()
{
}
public Student(int id, String name, int tid)
{
this.id = id;
this.name = name;
this.tid = tid;
}
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 getTid()
{
return tid;
}
public void setTid(int tid)
{
this.tid = tid;
}
@Override
public String toString()
{
return "Student{" + "id=" + id + ", name='" + name + '\'' + ", tid=" + tid + '}';
}
}
Teacher.java
package com.shrimpking.pojo;
import java.util.List;
public class Teacher
{
private int id;
private String name;
private List<Student> students;
public Teacher()
{
}
public Teacher(int id, String name, List<Student> students)
{
this.id = id;
this.name = name;
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 List<Student> getStudents()
{
return students;
}
public void setStudents(List<Student> students)
{
this.students = students;
}
@Override
public String toString()
{
return "Teacher{" + "id=" + id + ", name='" + name + '\'' + ", students=" + students + '}';
}
}
数据库:
create table user(
id int(20) not null,
name varchar(30) default null,
pwd varchar(30) default null,
primary key(id)
);
insert into user values(1,'张三','123456');
insert into user values(2,'李四','123456');
insert into user values(3,'王五','123456');
create table teacher(
id int(10) not null,
name varchar(30) default null,
primary key(id)
);
insert into teacher(id,name) values(1,"啥老师");
create table student(
id int(10) not null,
name varchar(30) default null,
tid int(10) default null,
primary key(id),
key fktid(tid),
constraint fktid foreign key(tid) references teacher(id)
);
insert into student(id,name,tid) values(1,"小明",1);
insert into student(id,name,tid) values(2,"小花",1);
insert into student(id,name,tid) values(3,"小妞",1);
insert into student(id,name,tid) values(4,"小宁",1);
insert into student(id,name,tid) values(5,"小王",1);
select
s.id as sid,
s.name as sname,
s.tid as stid,
t.id as tid,
t.name as tname
from student s,teacher t where s.tid = t.id
select
s.id as sid,
s.name as sname,
t.id as tid,
t.name as tname
from teacher t,student s
where t.id = s.tid
create table blog(
id varchar(50) not null primary key,
title varchar(100) not null comment '标题',
author varchar(30) not null comment '作者',
create_time datetime not null comment '创建时间',
views int(30) not null comment '浏览量'
);
StudentMapper.java
package com.shrimpking.dao;
import com.shrimpking.pojo.Student;
import java.util.List;
public interface StudentMapper
{
}
TeacherMapper.java
package com.shrimpking.dao;
import com.shrimpking.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface TeacherMapper
{
List<Teacher> getTeacherList();
Teacher getTeacher(@Param("tid") int id);
}
StudentMapper.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.shrimpking.dao.StudentMapper">
</mapper>
TeacherMapper.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.shrimpking.dao.TeacherMapper">
<select id="getTeacherList" resultType="Teacher">
select * from teacher;
</select>
<!--
方式二,嵌套查询
-->
<select id="getTeacher" resultMap="teacherStudent2">
select * from teacher;
</select>
<resultMap id="teacherStudent2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select * from student where tid = #{tid}
</select>
<!--
按结果
<select id="getTeacher" resultMap="teacherStudent">
select
s.id as sid,
s.name as sname,
t.id as tid,
t.name as tname
from teacher t,student s
where t.id = s.tid and t.id=#{tid}
</select>
<resultMap id="teacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
-->
</mapper>
UserDaoTest.java
package com.shrimpking.dao;
import com.shrimpking.pojo.Student;
import com.shrimpking.pojo.Teacher;
import com.shrimpking.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
import java.util.zip.DeflaterOutputStream;
public class UserDaoTest
{
@Test
public void test()
{
SqlSession sqlSession = MyBatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
List<Teacher> teacherList = mapper.getTeacherList();
for (Teacher teacher : teacherList)
{
System.out.println(teacher);
}
sqlSession.close();
}
@Test
public void test2()
{
SqlSession sqlSession = MyBatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
@Test
public void test3()
{
SqlSession sqlSession = MyBatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
}