测试环境搭建
SQL
create table ` teacher` (
` id` int ( 10 ) not null auto_increment ,
` name` varchar ( 20 ) default null ,
primary key ( id)
) engine = innodb default charset utf8;
insert into teacher values ( default , "lyh" ) ;
create table ` student` (
` id` int ( 10 ) not null ,
` name` varchar ( 20 ) 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 values ( 1 , "mlb1" , 1 ) ;
insert into student values ( default , "mlb2" , 1 ) ;
insert into student values ( default , "mlb3" , 1 ) ;
insert into student values ( default , "mlb4" , 1 ) ;
insert into student values ( default , "mlb5" , 1 ) ;
第一步:建立实体类
建立Student类:
package org. pojo ;
public class Student {
private int id;
private String name;
private Teacher teacher;
@Override
public String toString ( ) {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", teacher=" + 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 getTeacher ( ) {
return teacher;
}
public void setTeacher ( Teacher teacher) {
this . teacher = teacher;
}
}
建立Teacher类
package org. pojo ;
public class Teacher {
private int id;
private String name;
@Override
public String toString ( ) {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
'}' ;
}
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;
}
}
第二步:建立Mapper接口
建立StudentMapper接口
package org. dao ;
public interface StudentMapper {
}
建立TeacherMapper接口
package org. dao ;
import org. apache. ibatis. annotations. Param ;
import org. apache. ibatis. annotations. Select ;
import org. pojo. Teacher ;
public interface TeacherMapper {
@Select ( "select * from teacher where id = #{tid}" )
Teacher getTeacher ( @Param ( "tid" ) int id) ;
}
第三步:建立Mapperx.XML文件
注意:在resource目录下
建立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 = " org.dao.TeacherMapper" >
</ mapper>
建立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 = " org.dao.StudentMapper" >
</ mapper>
第四步:在核心配置文件中绑定注册我们的Mapper接口或者文件!
<?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>
< settings>
< setting name = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< environments default = " development" >
< environment id = " development" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://localhost:3306/mybatis?useSSL=true& useUnicode=true& characterEncoding=utf8" />
< property name = " username" value = " root" />
< property name = " password" value = " ********" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " org/StudentMapper.xml" />
< mapper resource = " org/TeacherMapper.xml" />
</ mappers>
</ configuration>
第五步:测试类:查询是否能够成功
import org. apache. ibatis. session. SqlSession ;
import org. dao. TeacherMapper ;
import org. pojo. Teacher ;
import org. utils. MybatisUtils ;
public class MyTest {
public static void main ( String [ ] args) {
SqlSession sqlSession = null ;
try {
sqlSession = MybatisUtils . getSqlSession ( ) ;
TeacherMapper mapper = sqlSession. getMapper ( TeacherMapper . class ) ;
Teacher teacher = mapper. getTeacher ( 1 ) ;
System . out. println ( teacher) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} catch ( Error e) {
e. printStackTrace ( ) ;
} finally {
sqlSession. close ( ) ;
}
}
}
文件结构: