springBoot注解SQL 更多详细⽤法,可以参考mybatis中⽂⽹的专门介绍
1、在我们写的动态sql前⾯添加@Seclect
@Select("select * from d_technician")
@Results(id = "techniciansMap",
value = {
@Result(id = true, property = "id", column = "id"),
@Result(property = "userId",  column = "user_id"),
@Result(property = "skilful",  column = "skilful"),
@Result(property = "company",  column = "company"),
@Result(property = "workYear",  column = "work_year"),
@Result(property = "resume",  column = "resume"),
@Result(property = "star",  column = "star"),
@Result(property = "deal",  column = "deal"),
@Result(property = "likeNum",  column = "like_num"),
})
List<Technician> getTechnicians();
条件查询,对条件进⾏⾮空判断,在sql语句中拼接<script></script>
@Select("<script>select * from user <if test=\"id !=null \">where id = #{id} </if></script>")
public List<User> findUserById(User user);
2.可以在接⼝中 创建内部类实现,当然也可以新建⼀个类
class TechnicianMapperProvider{
//结构化sql
public String selectTechnicianByPara(Technician technician) {
return new SQL() {
{
SELECT("*");
FROM("d_technician");
if (Skilful() != null) {
WHERE("skilful = #{skilful}");
}
if (WorkYear() != null) {
WHERE("work_year = #{workYear}");
}
if (Star() != null) {
WHERE("star = #{star}");
}
}
springboot中文}.toString();
}
}
//增改删对应@InsertProvider、@UpdateProvider、@DeleteProvider
@SelectProvider(type = TechnicianMapperProvider.class,method = "selectTechnicianByPara")
List<Technician> getTechnicians();
//结构化sql 看着⾼⼤上,但是多表查询貌似不⾏,ON 字段没有(如果有遇到,⽤上⾯可以实现的,欢迎评论)public String selectTechnicianByPara(Technician technician) {
String sql = "select * from d_technician";
if (Skilful() != null) {
sql+="where skiful = #{skiful}";
}
return sql;
}
//如果觉得⿇烦,可以直接编写xml脚本,可能更⽅便些
关于Mapper接⼝的注解
注意:使⽤注解版需要在类上加上@Mapper注解,让SpringBoot⾃动扫描能识别
@Mapper
public interface UserMapper {
⼀次⾏扫描所有mapper 在程序启动⼊⼝加⼊注解:
@MapperScan(basePackages= {"com.lqy.springboot.mapper"})
@SpringBootApplication
public class SpringbootApplication {
对于上⾯Mapper结果的映射
注意:@Results 在@Select前后都没有关系
声明时给id赋值为techniciansMap
在其他 ⽅法中,重复使⽤id为techniciansMap的结果映射@ResultMap("techniciansMap")
如果是通过xml⽂件来配置的话,只需要开启驼峰命名转换<setting name="mapUnderscoreToCamelCase" value="true"/>
mybatis:
configuration:
map-underscore-to-camel-case: true

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。