-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef19fc5
commit e50ab93
Showing
3 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/test/java/com/tianjunwei/lazy/mapper/TeacherMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters