通用Mapper
MyBatis 通用Mapper中的example使用
排序:example.orderBy(DArticles.CREATED_AT).desc().orderBy(DArticles.AUTHOR_NAME).asc();
分頁:ca.andCondition("limit"," 0,10")
Example example = new Example(DArticles.class);
Example.Criteria ca = example.createCriteria();
ca.andEqualTo(DArticles.AUTHOR_NAME, "xxx");
ca.andCondition("limit"," 0,10")
example.orderBy(DArticles.CREATED_AT).desc().orderBy(DArticles.AUTHOR_NAME).asc();
List list = dArticlesMapper.selectByExample(example);
//DArticles.CREATED_AT 是自動生成的表字段對應屬性,避免硬編碼
//只需要再MBG中添加
<property name="generateColumnConsts" value="true" />
// 全部代碼:
<!-- 繼承的通用基礎Mapper -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.me.yuri.tk.common.TmsBaseMapper" />
<property name="caseSensitive" value="true" />
<property name="forceAnnotation" value="true" />
<property name="lombok" value="Data" />
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<property name="swagger" value="true" />
<property name="generateColumnConsts" value="true" />
</plugin>
//即可生成:如下
public static final String AUTHOR_NAME = "authorName";
public static final String CREATED_AT = "createdAt";
另:
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* 在自動生成的接口文件中加入自定義的SQL語句,xml中無需配置sql
*/
public interface RoleMapper extends TmsBaseMapper<Role> {
@Select("select * from role")
List<Role> queryList();
@Select("select * from role where id = #{id}")
Role queryByID(@Param("id") String id);
}
//使用:
Role role = roleMapper.queryByID("id");
如果你發現錯誤,請指出,如果你覺得有幫助,請點贊!