MyBatis注解关联关系配置

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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值