SQL 注解 用法

注意: 从1.0.6及以上版本

通用 easyjdbc 中的 SQL 注解方法只需定义接口或继承EasyJdbcDao<泛型>例如下面:

  1. /**
  2. * 使用的注解,默认是@EasyDao 推荐@Repository
  3. */
  4. @Repository
  5. public interface UserDao {
  6. // 注解SQL方法
  7. }
  8. /**
  9. * 继承EasyJdbcDao<泛型>大量公共方法
  10. */
  11. @Repository
  12. public interface UserDao extends EasyJdbcDao<User> {
  13. // 注解SQL方法
  14. }

6.2 SQL 注解

6.2.1 @SqlSelect(与mybatis注解@Select用法相似)查询

6.2.1.1 纯接口无继承EasyJdbcDao<泛型>

  1. @SqlSelect(value = "select * from t_user",entityClass=User.class)
  2. public List<User> listUser();

6.2.1.2 继承EasyJdbcDao<泛型>

  1. @SqlSelect(value = "select * from t_user")
  2. public List<User> listUser();

6.2.1.3 @SqlParam传参

  1. @SqlSelect(value = "select * from t_user where id=#{id}",entityClass=User.class)
  2. //还可以使用 :id
  3. //@SqlSelect(value = "select * from t_user where id=:id",entityClass=User.class)
  4. public User getUserById(@SqlParam("id")Integer id);

6.2.1.4 Map传参

  1. @SqlSelect(value = "select * from t_user where id=#{id}",entityClass=User.class)
  2. public List<User> getUserByMapId(Map map);

6.2.1.5 动态映射对象

  1. @SqlSelect(value = "select * from t_user where id=:id",entityClass=UserDTO.class)
  2. public UserDTO getUserById(@SqlParam("id")Integer id);

6.2.1.6 分页

  1. @SqlSelect(
  2. value = "select * from t_user where user_name=#{userName}",
  3. entityClass=UserDTO.class
  4. )
  5. public PageInfo<UserDTO> listUser(@SqlParam("userName")String userName, @SqlParam("pageNum")Integer pageNum, @SqlParam("pageSize")Integer pageSize);

6.2.2 @SqlSelectProvider查询(与mybatis注解@SelectProvider用法相似)

6.2.2.1 查询

  1. sql 对象
  2. public class UserSqlProvider{
  3. public String listUserBySql(){
  4. return new SQL(){{
  5. SELECT("*");
  6. FROM("t_user");
  7. }
  8. }.toString();
  9. }
  10. }
  11. }
  12. @SqlSelectProvider(type= UserSqlProvider.class,method = "listUserBySql",entityClass =UserDTO.class )
  13. public List<UserDTO> listUserBySql();

6.2.2.2 @SqlParam传参

  1. sql 对象
  2. public class UserSqlProvider{
  3. public String getUserBySql(){
  4. return new SQL(){{
  5. SELECT("*");
  6. FROM("t_user");
  7. WHERE("id=#{id}").
  8. }
  9. }.toString();
  10. }
  11. }
  12. @SqlSelectProvider(type= UserSqlProvider.class,method = "getUserBySql",entityClass =UserDTO.class )
  13. public UserDTO getUserBySql((@SqlParam("id")Integer id));

6.2.2.2 Map动态传参

  1. sql 对象
  2. public class UserSqlProvider{
  3. public String listUserBySqlMap(Map map){
  4. return new SQL() {
  5. {
  6. SELECT("*");
  7. FROM("t_user");
  8. if (Maps.containsKey(map, "id")) {
  9. if (Maps.getInteger(map, "id") != null) {
  10. WHERE("id=#{id}");
  11. }
  12. }
  13. }
  14. }.toString();
  15. }
  16. }
  17. @SqlSelectProvider(type= UserSqlProvider.class,method = "listUserBySqlMap",entityClass =UserDTO.class )
  18. public List<UserDTO> listUserBySqlMap(Map map);

6.2.3 SqlInsert,SqlInsertProvider 新增注解

6.2.3.1 @SqlInsert

  1. //对象传参
  2. @SqlInsert("INSERT INTO t_user(id,user_name) VALUES(#{user.id},#{user.userName})")
  3. Integer saveUser(@SqlParam("user") User user);
  4. //单值传参
  5. @SqlInsert("INSERT INTO t_user(id,user_name) VALUES(#{id},#{userName})")
  6. Integer saveUser(@SqlParam("id") Integer id,@SqlParam("userName") String userName);
  7. //返回主键值
  8. @SqlInsert("INSERT INTO t_user(id,user_name) VALUES(#{id},#{userName})")
  9. @SqlOptions(useGeneratedKeys = true)
  10. Integer saveUser(@SqlParam("id") Integer id,@SqlParam("userName") String userName);

6.2.3.2 @SqlInsertProvider

  1. //SQL对象
  2. public class UserProvider {
  3. //SQL对象方法参数Map map可传可不传 如果通过MAP动态判断 就必须传
  4. public String saveUserBySql(Map map){
  5. SQL sql=new SQL();
  6. sql.INSERT_INTO("t_user").
  7. VALUES("id,user_name","#{id},#{userName}");
  8. return sql.toString();
  9. }
  10. }
  11. @SqlInsertProvider(type = UserProvider.class,method = "saveUserBySql")
  12. Integer saveUserBySql(Map map);

6.2.4 SqlUpdate,SqlUpdateProvider修改注解

6.2.4.1 @SqlUpdate

  1. //对象传参
  2. @SqlUpdate("UPDATE t_user SET user_name=#{user.userName} WHERE id=#{user.id})")
  3. Integer updateUser(@SqlParam("user") User user);
  4. //单值传参
  5. @SqlUpdate("UPDATE t_user SET user_name=#{userName} WHERE id=#{id})")
  6. Integer updateUser(@SqlParam("id") Integer id,@SqlParam("userName") String userName);

6.2.4.1 @SqlUpdateProvider

  1. //SQL对象
  2. public class UserProvider {
  3. //SQL对象方法参数Map map可传可不传 如果通过MAP动态判断 就必须传
  4. public String updateUserBysql(Map map){
  5. SQL sql=new SQL();
  6. sql.UPDATE("t_user").
  7. SET("user_name=#{userName}").WHERE("id=#{id}");
  8. return sql.toString();
  9. }
  10. }
  11. //MAP
  12. @SqlUpdateProvider(type = UserProvider.class,method = "updateUserBysql")
  13. Integer updateUserBysql(Map map);
  14. //
  15. @SqlUpdateProvider(type = UserProvider.class,method = "updateUserBysql")
  16. Integer updateUserBysql(@SqlParam("id") Integer id,@SqlParam("userName") String userName);

6.2.5 SqlDelete,SqlDeleteProvider删除注解

6.2.5.1 @SqlDelete

  1. @SqlDelete("DELETE FROM t_user WHERE id=#{id})")
  2. Integer deleteUser(@SqlParam("id") Integer id);

6.2.5.1 @SqlDeleteProvider

  1. //SQL对象方法参数Map map可传可不传 如果通过MAP动态判断 就必须传
  2. public String deleteUserBysql(Map map){
  3. SQL sql=new SQL();
  4. sql.DELETE_FROM("t_user").
  5. WHERE("id=#{id}");
  6. return sql.toString();
  7. }
  8. @SqlDeleteProvider(type = UserProvider.class,method = "deleteUserBysql")
  9. Integer deleteUserBysql(Map map);
  10. @SqlDeleteProvider(type = UserProvider.class,method = "deleteUserBysql")
  11. Integer deleteUserBysql(@SqlParam("id") Integer id);

原文: https://github.com/xphsc/easyjdbc/wiki/3.4-SQL-annotation