IBatis一对一,一对多,多对多

时间:2022-02-03 23:10:11

package com.zjy.ibatis.model;

import java.util.List;

public class Customer {
    private int id;
    private String username;
    private String password;
    private Account account;
    private List<Order> orders;
    private List<Activity> activitys;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
    public List<Order> getOrders() {
        return orders;
    }
    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }
    public List<Activity> getActivitys() {
        return activitys;
    }
    public void setActivitys(List<Activity> activitys) {
        this.activitys = activitys;
    }
   
}
<?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.zjy.ibatis.ICustomerDao" >
    <resultMap type="com.zjy.ibatis.model.Customer" id="customerResultMap">
         <id property="id" column="id"/>
         <result property="username" column="user_name"/>
         <result property="password" column="user_pass"/>
         <association property="account" select="com.zjy.ibatis.IAccountDao.findAccountByCustomerId" column="id"></association>
         <collection property="orders"  javaType="ArrayList" ofType="Order"
         select="com.zjy.ibatis.IOrderDao.findOrderByCustomerId" column="id"/>
         <collection property="activitys" javaType="ArrayList" ofType="Activity"
         select="com.zjy.ibatis.IActivityDao.findActivitysByCustomerId" column="id"/>
    </resultMap>
    <insert id="insert" parameterType="Customer">
         insert into customer(username,password)values(#{username},#{password})
    </insert>
    <delete id="delete" parameterType="java.lang.Integer">
         delete from customer where id=#{id}
    </delete>
    <update id="update" parameterType="Customer">
         update customer set username=#{username},password=#{password} where id=#{id}
    </update>
    <select id="findById" parameterType="java.lang.Integer"  resultType="Customer" resultMap="customerResultMap" >
         select * from customer where id=#{id}
    </select>
    <select id="query" parameterType="Customer" resultMap="customerResultMap">
         select * from customer
         <where>
            <if test="username!=null and username!=''">
                user_name like concat(concat('%',#{username}),'%')
            </if>
            <if test="password!=null and password!=''">
                or user_pass like concat(concat('%',#{password}),'%')
            </if>
         </where>
    </select>
</mapper>

本文出自 “体系架构” 博客,请务必保留此出处http://zhengjingyu.blog.51cto.com/3418943/636319