sql语句
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
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`)
) ENGINE=INNODB DEFAULT CHARSET=utf8INSERT 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');
dao
studentMapper和teacherMapper
package com.kuang.pojo; import lombok.Data; @Data public class Teacher { private int id; private String name; }
package com.kuang.dao; import com.kuang.pojo.Teacher; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface teacherMapper { Teacher getTeacher(@Param("id") int id); }
pojo
Student
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; }
MybatisUtils
package com.kuang.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.IOException; import java.io.InputStream; public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { String resource="mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { throw new RuntimeException(e); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
studentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.dao.studentMapper"> <select id="selectAll" resultMap="StudentTeacher"> select * from student </select> <resultMap id="StudentTeacher" type="Student"> <result property="id" column="id"/> <result property="name" column="name"/> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from teacher where id=#{id} </select> <!-- 方式二 --> <select id="selectAll1" resultMap="Studen"> select s.id as sid,s.`name` as sname,t.`name` as tname from student s,teacher t where s.tid=t.id </select> <resultMap id="Studen" type="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" javaType="Teacher"> <result property="name" column="tname"/> </association> </resultMap>
teacherMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.dao.teacherMapper"> </mapper>
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> <package name="com.kuang.pojo"/> </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 class="com.kuang.dao.studentMapper"/> <mapper class="com.kuang.dao.teacherMapper"/> </mappers> </configuration>
db.properties
driver=com.mysql.jdbc.Driver username=root password=111 url=jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
MybatisUtils
package com.lisai; import com.kuang.dao.studentMapper; import com.kuang.dao.teacherMapper; import com.kuang.pojo.Student; import com.kuang.pojo.Teacher; import com.kuang.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.testng.annotations.Test; import java.util.List; public class MybatisTest { @Test public void selectAll(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); studentMapper mapper = sqlSession.getMapper(studentMapper.class); List<Student> students = mapper.selectAll1(); for (Student student : students) { System.out.println(student); } } @Test public void selectAll1(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); studentMapper mapper = sqlSession.getMapper(studentMapper.class); List<Student> students = mapper.selectAll(); for (Student student : students) { System.out.println(student); } } }