package com.zjy.ibatis.annon;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zjy.hibernate.model.annotation.Account;
import com.zjy.ibatis.annon.model.Customer;

public interface CustomerMapper {

    @Select(“select * from customer where id=#{id}”)
    @Results(value={@Result(property=”id”,column=”id”),
                    @Result(property=”username”,column=”username”),
                    @Result(property=”password”,column=”password”),
                    @Result(property=”account”,column=”id”,javaType=Account.class,
                            one=@One(select=”com.zjy.ibatis.annon.AccountMapper.findByCustomerId”)),
                    @Result(property=”orders”,column=”id”,javaType=List.class,
                            many=@Many(select=”com.zjy.ibatis.annon.OrderMapper.getOrdersByCustomerId”)),
                    @Result(property=”activities”,column=”id”,javaType=List.class,
                            many=@Many(select=”com.zjy.ibatis.annon.ActivityMapper.getActivitiesByCustomerId”))})
    public Customer findById(int id);
    @Update(value={“update customer set username=#{username},password=#{password} where id=#{id}”})
    public void update(Customer customer);
    @Delete(value=”delete from customer where id=#{id}”)
    public void delete(Customer customer);
    @Insert(value=”insert into customer(username,password)values(#{username},#{password})”)
    public void save(Customer customer);
    @Select(“select * from customer”)
    public List<Customer> findAll();
    @Select(“select * from customer limit #{start},#{length}”)
    public List<Customer> getPage(int start,int length);
}
package com.zjy.ibatis;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.zjy.ibatis.annon.model.Customer;
import com.zjy.ibatis.annon.model.CustomerBuilder;

public class CustomerSqlBuilder extends SqlBuilder{

    public String makeSelect(CustomerBuilder c){

        SELECT(“c.id,c.username,c.password”);
        FROM(“customer c”);
        if(c.getUsername()!=null){

            WHERE(“c.username=#{username}”);
        }
       
        if(c.getPassword()!=null){

            OR();
            WHERE(“c.password=#{password}”);
        }
        if(c.getId()!=0){

            AND();
            WHERE(“c.id=#{customer.id}”);
        }
        return SQL();
    }
    public String makeUpdate(){

        UPDATE(“customer”);
        SET(“id=#{c.id}”);
        SET(“username=#{username}”);
        SET(“password=#{password}”);
        WHERE(“id=#{id}”);
        return SQL();
    }
    public String makeInsert(){

        INSERT_INTO(“customer”);
        VALUES(“id,username,password”, “#{id},#{username},#{password}”);
        return SQL();
    }
    public String makeDelete(){

        DELETE_FROM(“customer”);
        WHERE(“id=#{id}”);
        return SQL();
    }
    public String makeSelectMap(Map<String,Object> map){

        CustomerBuilder cb=(CustomerBuilder)map.get(“customer”);
        return makeSelect(cb);
    }
}

package com.zjy.ibatis.annon;

import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

import com.zjy.ibatis.CustomerSqlBuilder;
import com.zjy.ibatis.annon.model.CustomerBuilder;
public interface CustomerSqlBuilderMapper {

    @SelectProvider(type=CustomerSqlBuilder.class,method=”makeSelectMap”)
    public CustomerBuilder findById(@Param(“customer”) CustomerBuilder c,String userName);
    @InsertProvider(type=CustomerSqlBuilder.class,method=”makeInsert”)
    public void insert(CustomerBuilder c);
    @UpdateProvider(type=CustomerSqlBuilder.class,method=”makeUpdate”)
    public void update(CustomerBuilder c);
    @DeleteProvider(type=CustomerSqlBuilder.class,method=”makeDelete”)
    public void delete(CustomerBuilder c);
}