MyBatis provides a number of annotations such as: @InsertProvider, @UpdateProvider, @DeleteProvider and @SelectProvider, these are the methods of creating dynamic languages and letting MyBatis execute these languages using annotation-based mybatis, which are quite easy to use.
One first is to define mapper, @ SelectProvider to declare class ( CitySqlProvide ) and method name ( findByState ),
public interface CityMapper { @SelectProvider(type = CitySqlProvide.class, method = "findByState") List<City> findByState(@Param("state") String state); }
2 When using String to construct query statements, it is difficult and error-prone. Therefore, MyBatis provides SQL public method construction methods , and does not need to write a complete String statement . Let's see how to use the public methods of org.apache.ibatis.jdbc.SQL.
import org.apache.ibatis.jdbc.SQL; public class CitySqlProvide { public String findByState(String state) { return new SQL() {{ this.SELECT("*").FROM("city").WHERE("state = #{state}"); }}.toString(); } }