do.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
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核心配置文件-->
<configuration>
<!-- 引入外部配置文件-->
<properties resource="db.properties">
<property name="username" value="root"/>
<!--默认优先使用外部的db.properties配置文件中的-->
<property name="password" value="111"/>
</properties>
<!-- 设置日志-->
<settings>
<!-- 标准的日志工厂实现:STDOUT_LOGGING控制台输出0-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--log4j日志-->
<!-- <setting name="logImpl" value="LOG4J"/>-->
</settings>
<!-- 可以给实体类起别名-->
<typeAliases>
<typeAlias type="com.kuang.pojo.Student" alias="Student"></typeAlias>
<typeAlias type="com.kuang.pojo.Teacher" alias="Teacher"></typeAlias>
</typeAliases>
<environments default="development"><!--default:默认的环境这里可以配置:多个环境-->
<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>
<!-- 映射器 每一个Mapper.xml文件,都需要在Mybatis核心配置文件中注册-->
<mappers>
<mapper class="com.kuang.dao.TeacherMapper"/>
<mapper class="com.kuang.dao.StudentMapper"/>
</mappers>
</configuration>
Student:使用Lombok
package com.kuang.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
//学生需要关联一个老师
private Teacher teacher;
}
Teacher:
package com.kuang.pojo;
import lombok.Data;
@Data
public class Teacher {
private int id;
private String name;
}
TeacherMapper:使用注解
package com.kuang.dao;
import com.kuang.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface TeacherMapper {
@Select("select * from teacher where id=#{tid}")
Teacher getTeacher(@Param("tid") int id);
}
StudentMapper:
package com.kuang.dao;
import com.kuang.pojo.Student;
import java.util.List;
public interface StudentMapper {
//查询所有的学生信息,以及所有的老师的信息
public List<Student> getStudent();
public List<Student> getStudent2();
}
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.kuang.dao.StudentMapper">
<!--使用insert,update,delete,select标签来写sql语句-->
<!--
思路:
1.查询所有的学生信息
2.根据查询出来的学生的id,查询对应的老师
-->
<select id="getStudent" resultType="Student" resultMap="StudentTeacher">
select * from student1
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!-- 复杂的属性,我们需要单独处理
对象:association
集合:collection
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.kuang.pojo.Teacher">
select * from teacher where id=#{tid}
</select>
<!-- ============================-->
<!-- 按照结果集查询-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student1 s,teacher t
where s.tid=t.id
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<!-- 复杂的属性,我们需要单独处理
对象:association
集合:collection
-->
<association property="teacher" javaType="Teacher">
<result property="id" column="sid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
MyTest:
import com.kuang.dao.StudentMapper;
import com.kuang.dao.TeacherMapper;
import com.kuang.pojo.Student;
import com.kuang.pojo.Teacher;
import com.kuang.utils.MybaitsUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MyTest {
public static void main(String[] args) {
SqlSession sqlSession= MybaitsUtils.getSqlSession();
TeacherMapper mapper=sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
//方法一 查询嵌套
@Test
public void testStudent(){
SqlSession sqlSession= MybaitsUtils.getSqlSession();
StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
//方法二 按照结果集查询
@Test
public void testStudent2(){
SqlSession sqlSession= MybaitsUtils.getSqlSession();
StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent2();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
}
方法一
方法二