JAVA mybatis:Annotation 注解

来源:互联网 发布:自学电工软件app 编辑:程序博客网 时间:2024/06/06 02:06

1.常用注解:Select \ SelectProvider  \Insert \InsertProvider \Update\UpdateProvider \Delete \DeleteProvider \Result \Results \Options \One \Many \Para

见示例:

public interface UserMapper{\

  @Insert("INSERT INTO USER(name,sex,age) VALUES(#{name},#{sex},#{age})")

  @Options(useGenerateKeys=true,keyProperty="id")

  int saveUser(User user);  //方法

 

  @Update("UPDATE USER SET name = #{name},sex = #{sex},age = #{age} WHERE id = #{id}")

  int updateUser(User user);


  @Delete("DELETE FROM USER WHERE id = #{id}")

  int removeUser(@Para("id") Integer id);


  @Select("SELECT * FROM USER WHERE id = #{id}")

  @Results({

      @Result(id=true,column="id",property="id"),

      @Result(column="name",property="name"),

      @Result(column="sex",property="sex"),

      @Result(column="age",property="age"),

      @Result(column="deptid",property="deptid", //一个用户属于1个部门

         one=@One(select="org.test.mapper.DeptMapper.selectDeptById",fetchType=FetchType.EAGER)),

      @Result(column="id",property="roles",  //1个用户多个角色

         many=@Many(select="org.test.mapper.UserMapper.selectRolesById",fetchType=FetchType.LAZY))

  })

  User selectUserById(Integer id);


  @Select("select * FROM USER")

  List<User> selectAllUser();

}

2.动态SQL ,前面提到的 *Provider就是为了帮助构造动态SQL语句。

mybatis提供了一个SQL工具类 org.apache.ibatis.jdbc.SQL,该类不使用字符串拼接方式。常用方法如下:

T SELECT(String columns) ,T FROM(String table),T JOIN(String join),T INNER_JOIN(String join),LEFT_OUTER_JOIN ,RIGHT_OUT_JOIN,

T WHERE(String conditions),T OR(),T AND(),T GROUP_BY(String columns),T HAVING(String condition),T ORDER_BY(String columns),

T INSERT_INTO(String tableName) ,T VALUES(String columns,String values),DELETE_FROM(String table),T SET(String sets) 

示例:

import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.DeleteProvider;import org.apache.ibatis.annotations.InsertProvider;import org.apache.ibatis.annotations.Options;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.SelectProvider;import org.apache.ibatis.annotations.UpdateProvider;import org.fkit.domain.Employee;public interface EmployeeMapper {    // 动态查询    @SelectProvider(type=EmployeeDynaSqlProvider.class,method="selectWhitParam")    List<Employee> selectWhitParam(Map<String, Object> param);    // 动态插入    @InsertProvider(type=EmployeeDynaSqlProvider.class,method="insertEmployee")    @Options(useGeneratedKeys = true, keyProperty = "id")    int insertEmployee(Employee employee);    // 根据id查询    @SelectProvider(type=EmployeeDynaSqlProvider.class,method="selectWhitParam")    Employee selectEmployeeWithId(Map<String, Object> param);    // 动态更新    @UpdateProvider(type=EmployeeDynaSqlProvider.class,method="updateEmployee")    void updateEmployee(Employee employee);    // 动态删除    @DeleteProvider(type=EmployeeDynaSqlProvider.class,method="deleteEmployee")    void deleteEmployee(Map<String, Object> param);}

动态SQL类

import org.apache.ibatis.jdbc.SQL;import org.fkit.domain.Employee;public class EmployeeDynaSqlProvider {    public String selectWhitParam(Map<String, Object> param){        return new SQL(){            {                SELECT("*");                FROM("tb_employee");                if(param.get("id") != null){                    WHERE(" id = #{id} ");                }                if(param.get("name")!= null){                    WHERE("name = #{name}");                }                if(param.get("sex")!= null){                    WHERE("sex = #{sex}");                }                if(param.get("age")!= null){                    WHERE("age = #{age}");                }            }        }.toString();    }    public String insertEmployee(Employee employee){        return new SQL(){            {                INSERT_INTO("tb_employee");                if(employee.getName()!= null){                    VALUES("name", "#{name}");                }                if(employee.getSex()!= null){                    VALUES("sex", "#{sex}");                }                if(employee.getAge()!= null){                    VALUES("age", "#{age}");                }            }        }.toString();    }    public String updateEmployee(Employee employee){        return new SQL(){            {                UPDATE("tb_employee");                if(employee.getName()!= null){                    SET("name = #{name}");                }                if(employee.getSex()!= null){                    SET("sex = #{sex}");                }                if(employee.getAge()!= null){                    SET("age = #{age}");                }                WHERE(" id = #{id} ");            }        }.toString();    }    public String deleteEmployee(Map<String, Object> param){        return new SQL(){            {                DELETE_FROM("tb_employee");                if(param.get("id") != null){                    WHERE(" id = #{id} ");                }                if(param.get("state")!= null){                    WHERE("state = #{state}");                }            }        }.toString();    }}



需要特别提示下:SQL provider 方法至接收 无参、JAVA对象、java.util.Map


原创粉丝点击