Mybatis中的关联查询分为一对一(多对一)和一对多。
一、一对一(多对一)
1.添加mabatis和mysql相关jar包
2.创建数据库Mybatis,并创建User表(包含id、name、age、bumenid四个字段)和Bumen表(包含id、bumenname两个字段)。
3.创建Mybatis的配置文件conf.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>
<!-- 对事务的管理和连接池的配置 -->
<!-- development :开发模式 -->
<!-- work:工作模式 -->
<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" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<!-- mapping 文件路径配置 -->
<mappers>
<mapper resource="com/zw/mybatis/Mapper/UserMapper.xml" />
</mappers>
</configuration>
4.创建2个model类
User类
package com.zw.mybatis.model;
public class User {
private int id;
private String name;
private int age;
private int bumenid;
//关联表查询的关键
private Bumen bumen;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getBumenid() {
return bumenid;
}
public void setBumenid(int bumenid) {
this.bumenid = bumenid;
}
public User(int id, String name, int age, int bumenid) {
super();
this.id = id;
this.name = name;
this.age = age;
this.bumenid = bumenid;
}
public User() {
super();
}
public Bumen getBumen() {
return bumen;
}
public void setBumen(Bumen bumen) {
this.bumen = bumen;
}
}
Bumen类
package com.zw.mybatis.model;
public class Bumen {
private int id;
private String bumenname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBumenname() {
return bumenname;
}
public void setBumenname(String bumenname) {
this.bumenname = bumenname;
}
public Bumen(int id, String bumenname) {
super();
this.id = id;
this.bumenname = bumenname;
}
public Bumen() {
super();
}
}
5.创建UserMapper.xml和UserMapper接口
UserMapper接口
package com.zw.mybatis.model;
public interface UserMapper {
User selectnameandbumennamebyid(int userid);
}
UserMapper.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.zw.mybatis.model.UserMapper">
<select id="selectnameandbumennamebyid" parameterType="int"
resultMap="selectMap">
select b.bumenname,a.name from User a,Bumen b where a.bumenid=b.id and
a.id=#{0}
</select>
<resultMap type="com.zw.mybatis.model.User" id="selectMap">
<result property="name" column="name" />
<association property="bumen" javaType="com.zw.mybatis.model.Bumen">
<result property="bumenname" column="bumenname" />
</association>
</resultMap>
</mapper>
6.创建测试类
package com.zw.mybatis.Test;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.zw.mybatis.model.User;
import com.zw.mybatis.model.UserMapper;
public class UserTest {
public static void main(String[] args) {
String resourse = "conf.xml";
InputStream is = UserTest.class.getClassLoader().getResourceAsStream(resourse);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession(true);
UserMapper sm=session.getMapper(UserMapper.class);
User user= sm.selectnameandbumennamebyid(1);
System.out.println("姓名:"+user.getName()+" 部门:"+user.getBumen().getBumenname());
}
}