MyBatis学习总结(三)注解

 使用MyBatis注解开发,可以省去类配置文件,简洁方便。但是比较复杂的SQL和动态SQL还是建议书写类配置文件。

注解还是不推荐使用的。只是了解了解!简单的CRUD可以使用注解。简单写写。

    把之前的例子改成使用注解的。

UserMapper.java

package com.cy.mybatis.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cy.mybatis.beans.UserBean;

public interface UserMapper {
    // 简单的增删改查可以使用注解
    // 注解+配置文件
    
    /**
     * 新增用戶
     * @param user
     * @return
     * @throws Exception
     */
    @Insert("insert into t_user value (null,user.username,user.password,user.account)")
    @Options(useGeneratedKeys=true,keyProperty="user.id")
    public int insertUser(@Param("user")UserBean user) throws Exception;
    
    
    /**
     * 修改用戶
     * @param user
     * @param id
     * @return
     * @throws Exception
     */
    @Update(" update t_user set username=#{u.username},password=#{u.password},account=#{u.account} where id=#{id}")
    public int updateUser (@Param("u")UserBean user,@Param("id")int id) throws Exception;
    
     /**
      * 刪除用戶
      * @param id
      * @return
      * @throws Exception
      */
    @Delete(" delete from t_user where id=#{id}  ")
    public int deleteUser(int id) throws Exception;
    
    
    /**
     * 根据id查询用户信息
     * @param id
     * @return
     * @throws Exception
     */
    
    @Select(" select * from t_user where id=#{id}")
    @Results({
        
        @Result(id=true,property="id",column="id",javaType=Integer.class),
        @Result(property="username",column="username",javaType=String.class),
        @Result(property="password",column="password",javaType=String.class),
        @Result(property="account",column="account",javaType=Double.class)
    })
    public UserBean selectUserById(int id) throws Exception;
     /**
      * 查询所有的用户信息
      * @return
      * @throws Exception
      */
    
    @Select(" select * from t_user")
    @ResultMap("userMap")
    public List selectAllUser() throws Exception;
    
    
    /**
     * 批量增加
     * @param user
     * @return
     * @throws Exception
     */
   public int batchInsertUser(@Param("users")List user) throws Exception;
   
   /**
    * 批量删除
    * @param list
    * @return
    * @throws Exception
    */
   public int batchDeleteUser(@Param("list")List list) throws Exception;
   
   
   /**
    * 分页查询数据
    * @param parma
    * @return
    * @throws Exception
    */
   public List pagerUser(Map parmas) throws Exception;
   
   /**
    * 
    * 分页统计数据
    * @param parma
    * @return
    * @throws Exception
    */
    public int countUser(Map parmas) throws Exception;  
    
}

UserMapper.xml





   
        
        
        
        
    
    
    
    
    
       insert into t_user values 
        
         (null,#{users.username},#{users.password},#{users.account})
        
    
    
    
    
       delete from t_user where id in (
         
          #{id}
         
       )
    
    
    
    
    
    
    
    
    
        
    

简单的一个一对一的使用注解的。

package com.lovo.mybatis.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.lovo.mybatis.beans.HusbandBean;
import com.lovo.mybatis.beans.WifeBean;

public interface HusbandMapper {
    
    /**
     * 保存丈夫
     * @param husband
     * @return
     */
    @Insert("insert into t_husband values (null,#{h.name})")
    @Options(useGeneratedKeys=true,keyProperty="h.id")
    public int saveHusband(@Param("h")HusbandBean husband);
    
    
    /**
     * 根据ID查询丈夫资料
     * @param id
     * @return
     */
    @Select("select * from t_husband where id=#{id}")
    @ResultType(HusbandBean.class)
    public HusbandBean findHusbandById(int id);
    
    
    
    /**
     * 根据ID查询丈夫与妻子资料
     * @param id
     * @return
     */
    @Select("select * from t_husband where id=#{id}")
    @Results({
        @Result(id=true,property="id",column="id",javaType=Integer.class),
        @Result(property="name",column="name",javaType=String.class),
        @Result(property="wife",column="id",javaType=WifeBean.class,one=@One(select="com.lovo.mybatis.mapper.WifeMapper.findWifeByHusbandId"))
    })
    public HusbandBean findHusbandAndWife(int id);
    
    
}
package com.cy.mybatis.mapper;

import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import com.cy.mybatis.beans.WifeBean;

public interface WifeMapper {
    
    
    @Select("select * from t_wife where fk_husband_id = #{id}")
    @ResultType(WifeBean.class)
    public WifeBean selectWifeByHusbandId(int id)throws Exception;

}

注意:使用resultType时,一定要保证,你属性名与字段名相同;如果不相同,就使用resultMap 。

转载:http://www.cnblogs.com/hellokitty1/p/5229060.html

你可能感兴趣的:(Java)