注解操作mysql
@Mapper
public interface TickMapper {
@Insert("insert into tick (id,name,exchange,ask,bid,time) values (#{id},#{name},#{exchange},#{ask},#{bid},#{time})")
void insertTick(Tick tick);
@SelectProvider(type = TickProvider.class , method = "listTickByCondition")
List<Tick> listTickByCondition(@Param(value = "name")String name , @Param(value = "exchange") String exchange , @Param(value = "startDate") LocalDate startDate,@Param(value = "endDate")LocalDate endDate);
@Select("select t.id , t.`name` , t.exchange , t.ask , t.bid , t.time from tick as t")
List<Tick> listTickAll();
@SelectProvider(type = TickProvider.class , method = "listTickForPage")
List<Tick> listTickForPage(@Param(value = "name")String name , @Param(value = "exchange") String exchange , @Param(value = "startDate") LocalDate startDate,@Param(value = "endDate")LocalDate endDate,@Param(value = "startPage") Integer startPage, @Param(value = "pageSize") Integer pageSize);
}
public class TickProvider {
private final static String LIST_TICK_SQL ="select t.id , t.`name` , t.exchange , t.ask , t.bid , t.time from tick as t where 1=1 ";
/**
*
*
* @param name
* @param exchange
* @param startDate
* @param endDate
* @return
*/
public String listTickByCondition(@Param(value = "name")String name , @Param(value = "exchange") String exchange , @Param(value = "startDate") LocalDate startDate, @Param(value = "endDate")LocalDate endDate){
StringBuilder sb = new StringBuilder(LIST_TICK_SQL);
if(!StringUtils.isEmpty(name)){
sb.append(" and t.name = #{name} ");
}
if(!StringUtils.isEmpty(exchange)){
sb.append(" and t.exchange = #{exchange} ");
}
if(startDate != null && endDate != null){
sb.append(" and t.time between #{startDate} and #{endDate} ");
}
return sb.toString();
}
public String listTickForPage(@Param(value = "name")String name , @Param(value = "exchange") String exchange , @Param(value = "startDate") LocalDate startDate,@Param(value = "endDate")LocalDate endDate,@Param(value = "startPage") Integer startPage, @Param(value = "pageSize") Integer pageSize){
String sql = listTickByCondition(name,exchange,startDate,endDate);
StringBuilder sb = new StringBuilder(sql);
sb.append(" order by id ");
if(startPage!=null && pageSize !=null){
sb.append(" limit #{startPage} , #{pageSize} ");
}
return sb.toString();
}
}
标题:注解操作mysql
作者:Nick1407
地址:https://nick1407.com/articles/2019/07/08/1562566714118.html
0 0