我做了一个mybatis的demo里面有两个类:user address,user中存放着一个address,而在数据库中user表中有一个addressid的字段,用于关联address表。
public class User {
private int id;
private String name;
private String password;
private int age;
private Address address;
//get set方法
}
public class Address {
private int addid;
private String addname;
private int zipcode;
//get set方法
}
mybatis配置文件
- <!-- 延迟加载设置 -->
- <settings>
- <setting name="lazyLoadingEnabled" value="true"/>
- </settings>
-
- <!-- 别名 -->
- <typeAliases>
- <typeAlias type="com.qshun.model.User" alias="user"/>
- <typeAlias type="com.qshun.model.Address" alias="address"/>
- </typeAliases>
-
- <!-- 环境参数(数据库连接) -->
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost/ibatis" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </dataSource>
- </environment>
- </environments>
-
- <!-- 映射文件 -->
- <mappers>
- <mapper resource="com/qshun/model/AddressMapper.xml"/>
- <mapper resource="com/qshun/model/UserMapper.xml"/>
- </mappers>
usermapper的实体映射文件
- <resultMap type="user" id="UserResultMap">
- <result property="id" column="uid" />
- <result property="name" column="uname" />
- <result property="password" column="upassword" />
- <result property="age" column="uage" />
- <association property="address" column="addressid" select="getAddressById" resultMap="AddressResultMap">
- </association>
- </resultMap>
-
- <resultMap id="AddressResultMap" type="address">
- <result property="addid" column="id"/>
- <result property="addname" column="addressname"/>
- <result property="zipcode" column="zipcode"/>
- </resultMap>
-
- <select id="getUserById" resultMap="UserResultMap" parameterType="int">
- select uid,uname,upassword,uage,addressid
- from user
- where uid=#{id}
- </select>
-
- <select id="getAddressById" resultMap="AddressResultMap">
- select addid, addname, zipcode
- from addr
- where addid =#{addressid}
- </select>
测试类
private static SqlSession session = null;
private static SqlSessionFactory sqlSessionFactory = null;
static {
try {
Reader reader = Resources.getResourceAsReader("MybatisConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
session = sqlSessionFactory.openSession(true);
reader.close();
} catch (IOException e) {
throw new RuntimeException("=========" + e, e);
}
}
- public static void main(String[] args) {
-
- UserMapper mapper=session.getMapper(UserMapper.class);
- User u=mapper.getUserById(1);//理论上讲在执行此操作的时候,不会将address对象查询出来
- System.out.println(u.getName());
- System.out.println("================");
- System.out.println(u.getAddress().getAddname());//此时address才会被执行查询
- //但是事实上在执行mapper.getUserById(1)的时候,就执行了address对象的查询,延迟加载没有起效
- session.close();
- }
另附建表语句
CREATE TABLE ibatis
.add
(addid
INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,addname
VARCHAR(45),zipcode
VARCHAR(45),
PRIMARY KEY (addid
)
)
ENGINE = InnoDB;
CREATE TABLE ibatis
.user
(uid
INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,uname
VARCHAR(45),upassword
VARCHAR(45),uage
VARCHAR(45),addressid
VARCHAR(45),
PRIMARY KEY (uid
)
)
ENGINE = InnoDB;
请各位牛人,帮忙解答一下,不胜感激 :D