发布时间:2019-09-20 07:34:44编辑:auto阅读(1614)
public interface ICountryDao {
Country selectCountryById(int cid);
}
public class Mytest {
private SqlSession session;
private ICountryDao dao;
@Before
public void setUp() {
session = MyBatisUtils.getSqlSession();
dao = session.getMapper(ICountryDao.class);
}
@After
public void tearDown() {
if(session != null) {
session.close();
}
}
@Test
public void test01() {
Country country = dao.selectCountryById(1);
System.out.println(country);
}
}
1、多表连接查询方式
<mapper namespace="com.eason.mybatis.dao.ICountryDao">
<resultMap type="Country" id="countryMapper">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
<!-- 关联属性的映射文件 -->
<collection property="ministers" ofType="Minister">
<id column="mid" property="mid"/>
<result column="mname" property="mname"/>
</collection>
</resultMap>
<!-- 多表连接查询 -->
<select id="selectCountryById" resultMap="countryMapper">
select cid, cname, mid, mname from t_country, t_minister where cid=#{xxx} and cid=countryId
</select>
</mapper>
2、多表单独查询方式
<mapper namespace="com.eason.mybatis.dao.ICountryDao">
<select id="selectMinisterByCountry" resultType="Minister">
select mid, mname from t_minister where countryId=#{ooo}
</select>
<resultMap type="Country" id="countryMapper">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
<!-- 关联属性的映射文件 -->
<!-- 集合的数据来自于指定的select查询 -->
<collection property="ministers" ofType="Minister" select="selectMinisterByCountry" column="cid"></collection>
</resultMap>
<!-- 多表连接查询 -->
<select id="selectCountryById" resultMap="countryMapper">
select cid, cname from t_country where cid=#{xxx}
</select>
</mapper>
public class Minister {
private Integer mid;
private String mname;
private Country country;
//setter and getter
//toString
}
public class Country {
private Integer cid;
private String cname;
//setter and getter()
//toString
}
public interface ICountryDao {
Minister selectMinisterById(int mid);
}
@Test
public void test02() {
Minister minister = dao.selectMinisterById(2);
System.out.println(minister);
}
1、多表连接查询方式:
<mapper namespace="com.eason.mybatis.dao.ICountryDao">
<resultMap type="Minister" id="ministerMapper">
<id column="mid" property="mid"/>
<result column="mname" property="mname"/>
<association property="country" javaType="Country">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
</association>
</resultMap>
<select id="selectMinisterById" resultMap="ministerMapper">
select mid, mname, cid, cname from t_minister,t_country where mid=#{xxx} and countryId = cid
</select>
</mapper>
<mapper namespace="com.eason.mybatis.dao.ICountryDao">
<select id="selectCountryById" resultType="Country">
select * from t_country where cid=#{ooo}
</select>
<resultMap type="Minister" id="ministerMapper">
<id column="mid" property="mid"/>
<result column="mname" property="mname"/>
<association property="country" javaType="Country" select="selectCountryById" column="countryId"></association>
</resultMap>
<select id="selectMinisterById" resultMap="ministerMapper">
select mid, mname, countryId from t_minister where mid=#{xxx}
</select>
</mapper>
public class NewsLabel {
private Integer id;
private String name;
//关联属性,指定子栏目,即多方
private Set<NewsLabel> children;
//getter and setter
//toString()
}
public interface INewsLabelDao {
List<NewsLabel> selectChidrenByParentId(int pid);
}
<mapper namespace="com.eason.mybatis.dao.INewsLabelDao">
<!-- 形成递归,因为查询结果再次调用了selectChidrenByParentId查询 -->
<resultMap type="NewsLabel" id="newsLabelMapper">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="children" ofType="NewsLabel"
select="selectChidrenByParentId"
column="id"></collection>
</resultMap>
<!-- 根据pid查询其子栏目 -->
<select id="selectChidrenByParentId" resultMap="newsLabelMapper">
select id, name from t_newslabel where pid = #{xxx}
</select>
</mapper>
public class Mytest {
private SqlSession session;
private INewsLabelDao dao;
@Before
public void setUp() {
session = MyBatisUtils.getSqlSession();
dao = session.getMapper(INewsLabelDao.class);
}
@After
public void tearDown() {
if(session != null) {
session.close();
}
}
@Test
public void test02() {
List<NewsLabel> children = dao.selectChidrenByParentId(1);
for(NewsLabel newsLabel : children) {
System.out.println(newsLabel);
}
}
}
2、查询指定栏目以及所有子孙栏目
public interface INewsLabelDao {
NewsLabel selectNewsLabelById(int id);
}
修改mapper映射:
<mapper namespace="com.eason.mybatis.dao.INewsLabelDao">
<select id="selectNewsLabelByParentId" resultMap="newsLabelMapper">
select id, name from t_newslabel where pid = #{ooo}
</select>
<resultMap type="NewsLabel" id="newsLabelMapper">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="children" ofType="NewsLabel"
select="selectNewsLabelByParentId"
column="id"></collection>
</resultMap>
<select id="selectNewsLabelById" resultMap="newsLabelMapper">
select id, name from t_newslabel where id = #{xxx}
</select>
</mapper>
@Test
public void test02() {
NewsLabel newsLabel = dao.selectNewsLabelById(1);
System.out.println(newsLabel);
}
定义实体类:
public class NewsLabel {
private Integer id;
private String name;
private NewsLabel parent;
//setter and getter()
//toString
}
<mapper namespace="com.eason.mybatis.dao.INewsLabelDao">
<resultMap type="NewsLabel" id="newslabelMapper">
<id column="id" property="id"/>
<result column="name" property="name"/>
<association property="parent"
javaType="NewsLabel"
select="selectParentByParentId"
column="pid">
</association>
</resultMap>
<select id="selectParentByParentId" resultMap="newslabelMapper">
select id,name,pid from t_newslabel where id=#{xxx}
</select>
</mapper>
@Test
public void test03() {
NewsLabel newsLabel = dao.selectParentByParentId(3);
System.out.println(newsLabel);
}
public class Student {
private Integer sid;
private String sname;
private Set<Course> courses;
//setter and getter()
//toString()
}
public class Course {
private Integer cid;
private String cname;
private Set<Student> students;
//setter and getter()
//toString()
}
public interface IStudentDao {
Student selectStudentById(int id);
}
<mapper namespace="com.eason.mybatis.dao.IStudentDao">
<resultMap type="Student" id="studentMapper">
<id column="sid" property="sid"/>
<result column="sname" property="sname"/>
<collection property="courses" ofType="Course">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
</collection>
</resultMap>
<select id="selectStudentById" resultMap="studentMapper">
select sid, sname, cid, cname
from t_student, t_middle, t_course
where sid = studentId and cid = courseId and sid = #{xxx}
</select>
</mapper>
@Test
public void test05() {
Student student = dao.selectStudentById(2);
System.out.println(student);
}
<!-- 注册属性文件 -->
<properties resource="jdbc.properties"></properties>
<!-- 全局参数设置 -->
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
</settings>
<!-- 注册类的别名 -->
<typeAliases>
<package name="com.eason.mybatis.beans"/>
</typeAliases>
<!-- 全局参数设置 -->
<settings>
<!-- 延迟加载总开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 侵入式延迟加载开关 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!-- 全局参数设置 -->
<settings>
<!-- 延迟加载总开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 侵入式延迟加载开关 -->
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
上一篇: 全球CCIE人数统计(中国)(3/5/2
下一篇: OSPF-5种报文、3个阶段、3张表
47839
46383
37276
34727
29311
25969
24904
19946
19540
18019
5788°
6410°
5925°
5959°
7062°
5908°
5941°
6435°
6403°
7774°