Mybatis的注解开发
- @Select
@Select("select * from user where id = #{id}")
public User getById(@Param("id") Long id);
- @Insert
@Insert("insert into user(view_id,username,nickname,phone) values(#{viewId},#{username},#{nickname},#{phone})")
@Options(useGeneratedKeys = true)
public int insert(User user);
- @Update
@Update("update user set view_id =#{viewId},username =#{username},nickname=#{nickname},phone=#{phone} where id= #{id}")
public void update(user user);
- @Delete
@Delete("delete from user where id= #{id}")
public void delete(@Param("id")Long id);
动态SQL
- @SelectProvider
@SelectProvider(type = sqlProvider.class, method = "selectByPhone")
List<User> selectByPhone(@Param("phone")String phone);
class sqlProvider {
public static String selectByPhone(@Param("phone")final String phone) {
return new SQL() {{
SELECT("*");
FROM("user");
if (phone != null) {
WHERE("phone ="+phone);
}
ORDER_BY("id");
}}.toString();
}
}
对于比较复杂的SQL,还是推荐使用xml配置。
点击查看Mybatis的中文文档