一对一
一对一关系推荐使用唯一主外键关联,即两张表使用外键关联关系,由于是一对一关联,因此还需要给外键列增加unique唯一约束。
一对多
数据库中一对多关系通常使用主外键关联,外键列应该在多方,即多方维护关系。
dao
SmbmsRole getRoleAll(Integer id);
regiondao.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">
<!--namespace需要指向接口全路径-->
<mapper namespace="com.region.dao.regiondao">
<resultMap id="proMap" type="SmbmsRole">
<id property="rid" column="rid"></id>
<result property="roleName" column="roleName"></result>
<collection property="userList" ofType="SmbmsUser">
<id property="id" column="id"></id>
<result property="userName" column="userName"></result>
</collection>
</resultMap>
<select id="getRoleAll" resultMap="proMap">
SELECT u.*,r.* FROM smbms_user AS u,smbms_role AS r WHERE u.userRole=r.rid AND r.rid=#{rid}
</select>
</mapper>
test实现类
SqlSession sqlSession;
/*开始*/
@Before
public void before() throws IOException {
String source="mybatis_config.xml";
InputStream is= Resources.getResourceAsStream(source);
//步骤二:创建工厂
SqlSessionFactory factory =new SqlSessionFactoryBuilder().build(is);
//步骤三:创建sqlsession
sqlSession= factory.openSession();
}
/*一对多*/
@Test
public void getRoleAll(){
SmbmsRole roleAll = sqlSession.getMapper(regiondao.class).getRoleAll(3);
for (SmbmsUser role: roleAll.getUserList()) {
System.out.println("\t用户:"+role.getUserName());
}
}
/*关闭*/
@After
public void after(){
sqlSession.close();
}
多sql查询
在多sql查询其实就是小配置文件的不同
<?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">
<!--namespace需要指向接口全路径-->
<mapper namespace="com.region.dao.regiondao">
<resultMap id="proMap" type="SmbmsRole">
<id property="rid" column="rid"></id>
<result property="roleName" column="roleName"></result>
<!--映射多的一方property代表实体当中多的一方的属性名 ofType代表集合当中泛型类型-->
<!--select 代表执行查询的id column所应用的类 -->
<collection property="userList" ofType="SmbmsUser" select="getRoleSelAll" column="rid">
</collection>
</resultMap>
<select id="getRoleAll" resultMap="proMap">
SELECT * FROM smbms_role WHERE rid=#{rid}
</select>
<select id="getRoleSelAll" resultType="SmbmsUser">
SELECT * FROM smbms_user WHERE userRole=#{rid}
</select>
</mapper>
自关联查询
提供
<?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">
<!--namespace需要指向接口全路径-->
<mapper namespace="com.region.dao.ISsmbms_CityDao">
<resultMap id="proMap" type="city">
<id column="cid" property="cid"></id>
<result column="cName" property="cname"></result>
<result column="pid" property="pid"></result>
<collection property="childcity" ofType="City" select="getCitytoChildof" column="cid">
<id column="cid" property="cid"></id>
<result column="cName" property="cname"></result>
<result column="pid" property="pid"></result>
</collection>
</resultMap>
<select id="getCitytoChild" resultMap="proMap">
SELECT * FROM city WHERE cid=#{cid}
</select>
<!--循环调用resultMap 直到查询完所有数据为主-->
<!--通过调用resultMap在运行到collection循环回来 直到查询完所有数据,停止-->
<select id="getCitytoChildof" resultMap="proMap">
select * from city where pid=#{cid}
</select>