Mybatis 笔记 019(复杂查询环境搭建)
内容来自B站【狂神说Java】Mybatis最新完整教程IDEA版通俗易懂
1. 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=UTF8;
INSERT INTO `student`(`id`, `name`, `tid`) VALUES
(1, '俏如来', 1),
(2, '雁王', 1),
(3, '欲星移', 1),
(4, '凰后', 1),
(5, '砚寒清', 1);
2. 配置文件
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"/>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 可以给实体类起别名 -->
<typeAliases>
<package name="org.example.mybatis06.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>
<package name="org.example.mybatis06.mapper"/>
</mappers>
</configuration>
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/study_mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username=root
password=root
3. 实体类
package org.example.mybatis06.pojo;
import lombok.Data;
@Data
public class Teacher {
private Integer id;
private String name;
}
package org.example.mybatis06.pojo;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private Teacher teacher;
}
4. 接口
package org.example.mybatis06.mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.mybatis06.pojo.Teacher;
public interface TeacherMapper {
@Select("select * from `teacher` where `id` = #{id}")
Teacher getTeacher(@Param("id") int id);
}
<?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="org.example.mybatis06.mapper.TeacherMapper">
</mapper>
package org.example.mybatis06.mapper;
public interface StudentMapper {
}
<?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="org.example.mybatis06.mapper.StudentMapper">
</mapper>
5. 工具类
package org.example.mybatis06.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 inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
public static SqlSession getSqlSession(boolean autoCommit) {
return sqlSessionFactory.openSession(true);
}
}
6. 测试类
package org.example.mybatis06.mapper;
import org.apache.ibatis.session.SqlSession;
import org.example.mybatis06.utils.MybatisUtils;
import org.junit.Test;
public class TeacherMapperTest {
@Test
public void testGetTeacher() {
try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
System.out.println(sqlSession.getMapper(TeacherMapper.class).getTeacher(1));
}
}
}
浙公网安备 33010602011771号