一、一对多
1. DB
人物表 t_user
id bigint
name varchar(20)
age int
物品表 t_user_goods
id bigint
goods_name varchar(20)
uid bigint
2.sqlmap-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.test.model.User" alias="user" />
<typeAlias type="com.test.model.UserGoods" alias="userGoods" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="UNPOOLED">
<property name="driver"
value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url"
value="jdbc:jtds:sqlserver://192.168.0.101:1433/guagua_vas_stock" />
<property name="username" value="guagua_web" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/test/model/User.xml" />
</mappers>
</configuration>
3.User.java
package com.test.model;
import java.io.Serializable;
import java.util.List;
public class User implements Serializable {
private static final long serialVersionUID = 8681358563101101660L;
private long id;
private String name;
private int age;
private List<UserGoods> goods;
--- getter setter省
}
4. UserGoods.java
package com.test.model;
import java.io.Serializable;
public class UserGoods implements Serializable {
private static final long serialVersionUID = -396357348123803689L;
private long id;
private String name;
private long uid;
-- getter setter省
}
5.User.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="user">
<resultMap type="user" id="userMapper">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<collection property="goods" ofType="java.util.List" select="getGoodsById" column="id"/>
</resultMap>
<resultMap type="userGoods" id="userGoodsMapper">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="name" column="goods_name" jdbcType="VARCHAR"/>
<result property="uid" column="uid" jdbcType="BIGINT"/>
</resultMap>
<!-- 获取UserGoods对象 -->
<select id="getGoodsById" parameterType="Integer" resultMap="userGoodsMapper">
select * from t_user_goods where uid = #{id}
</select>
<select id="getUserList" resultMap="userMapper">
select * from t_user
</select>
</mapper>
注意:resultMap好像不能跨文件使用
6.UserDao.java
package com.test.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.bd.dao.IbatisSessionFactory;
import com.test.model.User;
public class UserDao {
public List<User> getUserList() {
SqlSession session = IbatisSessionFactory.getSqlSession();
List<User> list = (List)session.selectList("getUserList");
session.commit();
session.close();
return list;
}
}
二、一对一
假设 t_user 与 t_user_goods是一对一的关系
1.User.java
package com.test.model;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 8681358563101101660L;
private long id;
private String name;
private int age;
private UserGoods goods; // 由List换成了UserGoods
-- getter setter略
}
2.User.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="user">
<resultMap type="user" id="userMapper">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<!-- collection property="goods" ofType="java.util.List" select="getGoodsById" column="id"/ -->
<association property="goods" column="id" select="getGoodsById" />
</resultMap>
<resultMap type="userGoods" id="userGoodsMapper">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="name" column="goods_name" jdbcType="VARCHAR"/>
<result property="uid" column="uid" jdbcType="BIGINT"/>
</resultMap>
<!-- 获取UserGoods对象 -->
<select id="getGoodsById" parameterType="Integer" resultMap="userGoodsMapper">
select * from t_user_goods where uid = #{id}
</select>
<select id="getUserById" parameterType="int" resultMap="userMapper">
select * from t_user where id = #{id}
</select>
</mapper>
3.UserDao.java
package com.test.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.bd.dao.IbatisSessionFactory;
import com.test.model.User;
public class UserDao implements UserDaoInte{
public User getUserById(Long id) {
SqlSession session = IbatisSessionFactory.getSqlSession();
User user = (User)session.selectOne("getUserById", id);
session.commit();
session.close();
return user;
}
}
4.测试代码
public static void main(String[] args) {
UserDao dao = new UserDao();
User u = dao.getUserById(2L);
System.out.println("name:" + u.getName() );
System.out.println("goods:" + u.getGoods().getName());
}
ibatis3中的一对多和一对一的实现
最新推荐文章于 2019-09-10 18:29:59 发布