mybatis中写sql的三种方式

1. 在mapper文件中配置SQL





	


2. 注解式的SQL定义

public interface UserMapper {   
    @Select("select * from user ")
    List AnnotationGetUserList();
}

 如果想要的是动态SQL,那么就加上") List AnnotationGetUserById(@Param("id")String id); }

3.通过@SelectProvider来声明sql提供类

public interface UserMapper {
    @SelectProvider(type=SqlProvider.class,method="getUserById")
    List AnnotationProviderGetUserById(String id);
}
public class SqlProvider {
    public String getUserById(String id) {
        String sql = "select * from user ";
        if (id!=null) {
            sql += "    where id="+id;
        }
        return sql;
    }
}

 

你可能感兴趣的:(mybatis,mybatis)