MyBatis 接口注解方式代替mapper.xml

https://blog.csdn.net/m0_38068812/article/details/86566929

 

spring boot(8)-mybatis三种动态sql  或者 这个

 1. 代码实现字段的映射

public interface IUserMapper {
@Select({"SELECT * from user" } )
//@ResultMap("basicMap") // 此处可以直接使用mapper.xml中
@Results({
@Result(column = "d_id",property = "id"),
@Result(column = "d_username",property = "userName"),
@Result(column = "d_password",property = "password"),
@Result(column = "d_age",property = "age"),
@Result(column = "d_name",property = "name")
})
List<User> getList1();
}
 

 2. 动态SQL语句


@Mapper
public interface IUserMapper {
//使用UserProvider类的findUserById方法来生成sql
@SelectProvider(type = UserProvider.class, method = "findUserById")
public List<User> findUserById(User user);

class UserProvider {
  public String findUserById(User user) {
    String sql = "SELECT * FROM user";
   if(user.getId()!=null){
      sql += " where id = #{id}";
   }
    return sql;
   }
}

 

3. 结构化SQL

public String findUserById(User user) {    
      return new SQL(){{    
                SELECT("id,name");    
                SELECT("other");    
                FROM("user");    
                if(user.getId()!=null){    
                    WHERE("id = #{id}");    
                }    
                if(user.getName()!=null){    
                    WHERE("name = #{name}");    
                } 
       }}.toString(); //从这个toString可以看出,其内部使用高效的StringBuilder实现SQL拼接    
} 

 

mybatis语句构造器
 https://mybatis.org/mybatis-3/zh/statement-builders.html

  

mybatis plus 的条件构造器

https://mp.baomidou.com/guide/wrapper.html#abstractwrapper

posted @ 2019-09-23 09:40  Daniel_Lu  阅读(1422)  评论(0编辑  收藏  举报