ibatis3中的一对多和一对一的实现

3 篇文章 0 订阅

一、一对多
  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());
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值