Skip to content

Commit

Permalink
级联查询,一对多
Browse files Browse the repository at this point in the history
  • Loading branch information
tian-junwei committed Jan 3, 2017
1 parent ef19fc5 commit e50ab93
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/test/java/com/tianjunwei/lazy/LazyMainTeacher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2017 tianjunwei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tianjunwei.lazy;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.tianjunwei.lazy.entity.Teacher;
import com.tianjunwei.lazy.entity.User;


/**
* @author tianjunwei
* @time 2017 上午10:24:21
*/
public class LazyMainTeacher {
public static void main(String [] args){

//mybatis的配置文件
String resource = "learn/mybatis-config.xml";
InputStream is = LazyMainTeacher.class.getClassLoader().getResourceAsStream(resource);
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = sessionFactory.openSession();
String statement = "com.tianjunwei.lazy.entity.Teacher.getTeacher";//映射sql的标识字符串
//执行查询返回一个唯一user对象的sql
Teacher teacher = (Teacher) session.selectList(statement, 1).get(0);
session.commit(true);
System.out.println(teacher.getName());
List<User> users = teacher.getUsers();
System.err.println(users.get(3).getAge());

}
}
27 changes: 27 additions & 0 deletions src/test/java/com/tianjunwei/lazy/mapper/TeacherMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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.tianjunwei.lazy.entity.Teacher">

<select id="getById" parameterType="int" resultMap="user">
select * from users where users.teacher_id=#{id}
</select>

<select id="getTeacher" resultMap="teacher">
select * from tbl_teacher where id=#{id}
</select>

<resultMap type="com.tianjunwei.lazy.entity.Teacher" id="teacher" >
<id column="id" property="id" javaType="int" jdbcType="INTEGER"></id>
<result column="name" property="name" javaType="string" jdbcType="VARCHAR"/>
<collection property="users" column="id" select="com.tianjunwei.lazy.entity.Teacher.getById" javaType="list">
</collection>
</resultMap>

<resultMap type="com.tianjunwei.lazy.entity.User" id="user" >
<id column="id" property="id" javaType="int" jdbcType="INTEGER"></id>
<result column="name" property="names" javaType="string" jdbcType="VARCHAR"/>
<result column="age" property="age" javaType="int" jdbcType="INTEGER"/>
</resultMap>

</mapper>
3 changes: 2 additions & 1 deletion src/test/reources/learn/mybatis-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="defaultExecutorType" value="SIMPLE"/> <!--SIMPLE、REUSE、BATCH-->
<setting name="aggressiveLazyLoading" value="fasle"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<environments default="development">
Expand All @@ -24,6 +24,7 @@
<mapper resource="com/tianjunwei/learn/learn1/mapper/UserMapper.xml"/>
<mapper resource="com/tianjunwei/learn/learn2/mapper/UserMapper.xml"/>
<mapper resource="com/tianjunwei/lazy/mapper/UserMapper.xml"/>
<mapper resource="com/tianjunwei/lazy/mapper/TeacherMapper.xml"/>
</mappers>

</configuration>

0 comments on commit e50ab93

Please sign in to comment.