美文网首页MybatisPlusSpring技巧
SpringBoot MybatisPlus 执行原生Sql的三

SpringBoot MybatisPlus 执行原生Sql的三

作者: 饱饱想要灵感 | 来源:发表于2024-06-13 15:02 被阅读0次

    一、使用Spring的JdbcTemplate

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public void queryBySql(){
        List<Map<String, Object>> lstData = jdbcTemplate.queryForList("select * from t_test");
    }
    

    二、使用Mybatis-plus的SqlRunner

    1. yml文件中添加MybatisPlus配置:
        mybatis-plus:
          global-config:
            enable-sql-runner: true
    
    1. 使用SqlRunner.db()
    public void queryBySql(){
        List<Map<String, Object>> lstData = SqlRunner.db().selectList("select * from abc");
    }
    

    三、使用Mybatis-plus的Mapper

        public interface CustomMapper extends BaseMapper<Entity> {
            @Select("SELECT * FROM your_table WHERE condition = #{value}")
            List<Entity> selectByCustomSql(@Param("value") String value);
         
            @Select("<script>" +
                    "SELECT item_id,is_assign,shop_id FROM t_order_detail_items " +
                    "where shop_id = #{shopId} and is_deleted = #{status}  " +
                    "</script>")
            @Results({
                    @Result(property = "itemId", column = "item_id"),
                    @Result(property = "isAssign", column = "is_assign"),
                    @Result(property = "shopId", column = "shop_id"),
            })
            List<ShopItemVO> selectBySql(@Param("shopId") Integer shopId, @Param("status") Short status);
        
        }
    

    相关文章

      网友评论

        本文标题:SpringBoot MybatisPlus 执行原生Sql的三

        本文链接:https://www.haomeiwen.com/subject/zqhoqjtx.html