首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

MyBatis引文关联关系配置

2012-09-01 
MyBatis注解关联关系配置package com.zjy.ibatis.annonimport java.util.Listimport org.apache.ibatis.

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

热点排行