<?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">
<configuration>
<!-- 加载属性文件 -->
<properties resource="db.properties">
<!--properties中还可以配置一些属性名和属性值 -->
<!-- <property name="jdbc.driver" value=""/> -->
</properties>
<!-- 全局配置参数,需要时再设置 -->
<!-- 延迟加载的配置,懒加载 -->
<settings>
<!--打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!--将积极加载变为消极加载 按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!-- 别名定义 -->
<typeAliases>
<!-- 针对单个别名定义 type:类型的路径 alias:别名 -->
<!-- <typeAlias type="cn.itcast.mybatis.po.User" alias="user"/> -->
<!-- 批量别名定义 指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名(首字母大写或小写都可以) -->
<package name="mode" />
</typeAliases>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,事务控制由mybatis -->
<transactionManager type="JDBC" />
<!-- 数据库连接池,由mybatis管理 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<!-- 通过mapper接口加载映射文件:需要遵循一些规范:需要将mapper接口名称和mapper.xml映射文件保持一致,且在一个目录中 放在一个目录 ,且同名 前提是:使用的事mapper代理方式 -->
<mappers>
<package name="mapper" />
</mappers>
</configuration>
------------------------------------pojo.java-------------------------------------------------------------
public class User {
private Integer id;
private String name;
private Integer age;
private Integer addressId;
private Address address;}
------------------------------------------
public class Address {
private Integer id;
private String sheng;
private String shi;
private String xian;
-------------------------------------mapping-----------------------------------------------
<mapper namespace="mapper.AddressMapper" >
<select id="selectAddressById" parameterType="int" resultType="address">
select * from address where id = #{value}
</select>
</mapper>
-------------------------
<mapper namespace="mapper.UserMapper" >
<!-- 延迟加载的resultMap -->
<resultMap type="user" id="UserAddressLazyLoading">
<!--对用户信息进行配置 -->
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="address_id" property="addressId"/>
<!--对地址信息进行延迟加载 -->
<!-- select指的是所要加载的statement查询语句的id(关联查询的sql语句) -->
<!--column指的是 user表中 关联 address表的列 . javaType指的是关联映射的类 -->
<association column="address_id" property="address" javaType="address" select="mapper.AddressMapper.selectAddressById"></association>
</resultMap>
<!-- 查询 用户关联地址, 地址信息需要延迟加载 -->
<select id="selectUserAddressLazyLoading" resultMap="UserAddressLazyLoading">
select * from user
</select>
</mapper>
--------------------------------------------------------------------------------------
public class Demo1 {
private SqlSessionFactory sqlSessionFactory;
// 此方法是在执行testFindUserById之前执行
@Before
public void setUp() throws Exception {
// 创建sqlSessionFactory
// mybatis配置文件
String resource = "SqlMapConfig.xml";
// 得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testselectByPrimaryKey() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> users = userMapper.selectUserAddressLazyLoading();
for(User user: users){
System.out.println(user.getName());
}
System.out.println(".......................................");
System.out.println(users);
}
//根据id查询Address
@Test
public void testselectAddressById() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
Address address = addressMapper.selectAddressById(1);
System.out.println(address);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。