一、使用Spring的JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
public void queryBySql(){
List<Map<String, Object>> lstData = jdbcTemplate.queryForList("select * from t_test");
}
二、使用Mybatis-plus的SqlRunner
- yml文件中添加MybatisPlus配置:
mybatis-plus:
global-config:
enable-sql-runner: true
- 使用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);
}
网友评论