1、创建maven工程,搭建mybatis框架环境
mybatis-config.xml配置文件以及db。properties配置
<?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"/>
<!-- 配置日志文件,注意不要写错-->
<settings>
<!-- 标准日志工厂-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!-- 1、可以给实体类起别名,实体类少使用-->
<!-- <typeAlias type="pojo.User" alias="User"/>-->
<!-- 2、可以给实体类起别名,默认别名为类名首字母小写
若有注解,则以注解为别名-->
<package name="pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 连接数据库,数据库使用的是mysql8.0-->
<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>
<!--注册StudentMapper.xml-->
<mappers>
<mapper class="dao.StudentMapper"/>
</mappers>
</configuration>
复制代码
数据库使用使用MySQL8.0
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/表名?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
username=数据库用户名
password=数据库密码
复制代码
pom.xml依赖
<dependencies>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--mybatis所需jar包-->
<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>
复制代码
2、创建stdent和teacher数据表
CREATE TABLE student
(
id
int NOT NULL AUTO_INCREMENT,
name
varchar(20) ,
tid
int NULL DEFAULT NULL,
PRIMARY KEY (id
) USING BTREE,
INDEX fktid
(tid
) USING BTREE,
CONSTRAINT fktid
FOREIGN KEY (tid
) REFERENCES teacher
(id
) ON DELETE RESTRICT ON UPDATE RESTRICT
)
CREATE TABLE teacher
(
id
int NOT NULL AUTO_INCREMENT,
name
varchar(20) ,
PRIMARY KEY (id
) USING BTREE
)
3、创建对应实体类
package pojo;
public class Student {
private int id;
private String name;
//学生关联一个老师
private Teacher teacher;
public Student() {
}
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 Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Student(int id, String name, Teacher teacher) {
this.id = id;
this.name = name;
this.teacher = teacher;
}
}
复制代码
package pojo;
import lombok.Data;
public class Teacher {
private int id;
private String name;
public Teacher() {
}
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 Teacher(int id, String name) {
this.id = id;
this.name = name;
}
}
复制代码
4、创建StudentMapper接口以
package dao;
import pojo.Student;
import java.util.List;
public interface StudentMapper {
//多个学生对一个老师
//查找所有学生信息以及对应教师
List<Student> getStudents();
}
复制代码
5、创建StudentMapper.xml配置文件,写入sql语句以及配置
方式一按结果嵌套查询
<select id="getStudents" resultMap="Students">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid=t.id
</select>
<resultMap id="Students" type="Student">
<!-- property对应实体类名,column对应数据库列名-->
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<!--复杂的属性需要单独处理
对象使用association
集合用collection-->
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
复制代码
方式二、子查询
<!-- 子查询-->
<select id="getStudents" resultMap="Student2">
select * from student
</select>
<resultMap id="Student2" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--复杂的属性需要单独处理
对象使用association
集合用collection-->
<!-- property为对应实体类名,javaType返回值类型,getTeacher为子查询绑定的id-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select name from teacher where id=#{id}
</select>
复制代码
6、创建测试类测试
package dao;
import org.apache.ibatis.session.SqlSession;
import pojo.Student;
import until.MyBatisUntil;
import java.util.List;
public class Test {
@org.junit.Test
public void test(){
SqlSession sqlSession = MyBatisUntil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = mapper.getStudents();
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
}
}
复制代码
查询成功