mybatis对多关联查询:
练习用代码:
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">
<!-- 通过这个配置文件完成mybatis与数据库的连接 -->
<configuration>
<!-- 引入 database.properties 文件-->
<properties resource="database.properties"/>
<!-- 配置mybatis的log实现为LOG4J -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<!-- 设置别名 -->
<typeAliases>
<package name="com.bdqn.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!--配置事务管理,采用JDBC的事务管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 将mapper文件加入到配置文件中 -->
<mappers>
<mapper resource="com/bdqn/dao/GroupMapper.xml"/>
<mapper resource="com/bdqn/dao/PersonMapper.xml"/>
<!-- <mapper resource="com/bdqn/dao/UMapper.xml"/> -->
</mappers>
</configuration>
两个实体类:
package com.bdqn.entity;
import java.io.Serializable;
import java.util.List;
public class Person implements Serializable{
private int pid; //用户ID
private String pname; //用户名
private List<Group> groups; //用于对多查询(关联Group类)
//groups集合名用于PersonMapper.xml,collection property="?"
@Override
public String toString() {
return "Person [pid=" + pid + ", pname=" + pname + "]";
}
public Person() {
}
public Person(int pid, String pname, List<Group> groups) {
this.pid = pid;
this.pname = pname;
this.groups = groups;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public List<Group> getGroups() {
return groups;
}
public void setGroups(List<Group> groups) {
this.groups = groups;
}
}
package com.bdqn.entity;
import java.io.Serializable;
import java.util.List;
public class Group implements Serializable{
private int gid; //组ID
private String gname; //组名
private List<Person> persons; //用于对多查询(关联Person类)
//persons集合名用于GroupMapper.xml,collection property="?"
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
public Group(int gid, String gname, List<Person> persons) {
this.gid = gid;
this.gname = gname;
this.persons = persons;
}
public Group() {
}
@Override
public String toString() {
return "Group [gid=" + gid + ", gname=" + gname + "]";
}
}
工具类:
package com.bdqn.utils;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class GetSqlSession {
private static GetSqlSession getSqlSession = null;
private SqlSessionFactory factory = null;
private SqlSession session=null;
private InputStream is =null;
private GetSqlSession(){
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static synchronized GetSqlSession getConfig(){
if (getSqlSession==null) {
synchronized (GetSqlSession.class) {
if (getSqlSession==null) {
getSqlSession = new GetSqlSession();
}
}
}
return getSqlSession;
}
public SqlSession getSession(){
session = factory.openSession();
return session;
}
public void closeAll(){
if (session!=null) {
session.close();
}if (is!=null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
dao包:
GroupMapper接口
package com.bdqn.dao;
import java.util.List;
import com.bdqn.entity.Group;
public interface GroupMapper {
List<Group> getGroups();
}
PersonMapper 接口:
package com.bdqn.dao;
import java.util.List;
import com.bdqn.entity.Person;
public interface PersonMapper {
//左查询
List<Person> getPersons();
//右查询
List<Person> getPersonsR();
}
GroupMapper.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.bdqn.dao.GroupMapper">
<resultMap type="Group" id="getGroupPerson">
<id property="gid" column="gid"/>
<result property="gname" column="gname"/>
<collection property="persons" ofType="Person">
<id property="pid" column="pid"/>
<result property="pname" column="pname"/>
</collection>
</resultMap>
<select id="getGroups" resultMap="getGroupPerson">
select * from t_group g left join t_person_group pg on g.gid=pg.groupid
left join t_person p on p.pid=pg.personid
</select>
</mapper>
PersonMapper.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.bdqn.dao.PersonMapper">
<!-- 左查询 -->
<resultMap type="Person" id="getPersonGroup">
<id property="pid" column="pid"/>
<result property="pname" column="pname"/>
<collection property="groups" ofType="Group">
<id property="gid" column="gid"/>
<result property="gname" column="gname"/>
</collection>
</resultMap>
<!-- 右查询 -->
<resultMap type="Person" id="getPGRight">
<id property="pid" column="pid"/>
<result property="pname" column="pname"/>
<collection property="groups" ofType="Group">
<id property="gid" column="gid"/>
<result property="gname" column="gname"/>
</collection>
</resultMap>
<!-- 左查询 -->
<select id="getPersons" resultMap="getPersonGroup">
select * from t_person p left join t_person_group pg on p.pid=pg.personid
left join t_group g on g.gid=pg.groupid
</select>
<!-- 右查询 -->
<select id="getPersonsR" resultMap="getPGRight">
select * from t_group g right join t_person_group pg on g.gid=pg.groupid
right join t_person p on p.pid=pg.personid
</select>
</mapper>
spring:
applicationContext.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="springTest" class="com.bdqn.spring.SpringBean">
<property name="who">
<value>123456</value>
</property>
</bean>
</beans>
SpringBean.java:
package com.bdqn.spring;
public class SpringBean {
private String who;
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
public void show(){
System.out.println(this.who);
}
}
Test类:
package com.bdqn.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bdqn.spring.SpringBean;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringBean springBean = (SpringBean) applicationContext.getBean("springTest");
springBean.show();
}
}