通用mapper、分页助手的BaseService
2016-07-23 15:00
369 查看
依赖:
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 开启驼峰匹配 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- <typeAliases>
设置别名
<package name="cn.itcast.usermanage.pojo" />
</typeAliases> -->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
</plugin>
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!--主键自增回写方法,默认值MYSQL,详细说明请看文档-->
<property name="IDENTITY" value="MYSQL"/>
<!--通用Mapper接口,多个通用接口用逗号隔开-->
<property name="mappers" value="com.github.abel533.mapper.Mapper"/>
</plugin>
</plugins>
</configuration>
BasePojo:
BaseService:
<mapper-version>2.3.4</mapper-version> <pagehelper-version>3.7.5</pagehelper-version> <jsqlparser-version>0.9.1</jsqlparser-version> <!-- 分页插件依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper-version}</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>${jsqlparser-version}</version> </dependency> <!-- 通用mapper --> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>${mapper-version}</version> </dependency>
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 开启驼峰匹配 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- <typeAliases>
设置别名
<package name="cn.itcast.usermanage.pojo" />
</typeAliases> -->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
</plugin>
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!--主键自增回写方法,默认值MYSQL,详细说明请看文档-->
<property name="IDENTITY" value="MYSQL"/>
<!--通用Mapper接口,多个通用接口用逗号隔开-->
<property name="mappers" value="com.github.abel533.mapper.Mapper"/>
</plugin>
</plugins>
</configuration>
BasePojo:
package com.taotao.manage.pojo; import java.util.Date; public abstract class BasePojo { private Date created; private Date updated; public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getUpdated() { return updated; } public void setUpdated(Date updated) { this.updated = updated; } }
BaseService:
package com.taotao.manage.service; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.github.abel533.entity.Example; import com.github.abel533.mapper.Mapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.taotao.manage.pojo.BasePojo; public abstract class BaseService<T extends BasePojo> { /** * 由子类实现该方法,返回具体的Mapper的实现类 */ // public abstract Mapper<T> getMapper(); /** * 这样写是因为使用Spring4.x的新特性,根据泛型类型注入对象 */ @Autowired public Mapper<T> mapper; public Mapper<T> getMapper() { return mapper; } /** * 根据id查询数据 * * @param id * @return */ public T queryById(Long id) { return this.mapper.selectByPrimaryKey(id); } /** * 查询所有数据 * * @return */ public List<T> queryAll() { return this.mapper.select(null); } /** * 根据条件查询数据集合 * * @param t * @return */ public List<T> queryByWhere(T t) { return this.mapper.select(t); } /** * 根据条件查询一条数据 */ public T queryOne(T t) { return this.mapper.selectOne(t); } /** * 分页查询数据 */ public PageInfo<T> queryPageListBywhere(T t, Integer page, Integer rows) { PageHelper.startPage(page, rows, true);// 设置分页参数 // 查询数据 List<T> lists = this.queryByWhere(t); return new PageInfo<T>(lists); } /** * 自定义查询条件,分页查询 * @param example * @param page * @param rows * @return */ public PageInfo<T> queryPageListByExample(Example example, Integer page, Integer rows) { PageHelper.startPage(page, rows, true);// 设置分页参数 // 查询数据 List<T> lists = this.mapper.selectByExample(example); return new PageInfo<T>(lists); } /** * 新增数据 */ public Integer save(T t) { t.setCreated(new Date()); t.setUpdated(t.getCreated()); return this.mapper.insert(t); } /** * 新增数据,使用不为null的字段 */ public Integer saveSelective(T t) { t.setCreated(new Date()); t.setUpdated(t.getCreated()); return this.mapper.insertSelective(t); } /** * 更新数据 */ public Integer update(T t) { t.setUpdated(new Date()); return this.mapper.updateByPrimaryKey(t); } /** * 更新数据,使用不为null的字段 */ public Integer updateSelective(T t) { t.setUpdated(new Date()); return this.mapper.updateByPrimaryKeySelective(t); } /** * 根据id删除一条记录 */ public Integer deleteById(Long id){ return this.mapper.deleteByPrimaryKey(id); } /** * 根据ids批量删除 */ public Integer deleteByIds(List<Object> ids,String property,Class<T> clazz){ Example example=new Example(clazz); example.createCriteria().andIn(property, ids); return this.mapper.deleteByExample(example); } }
相关文章推荐
- more、less 和 most 的区别
- AJAX实现瀑布流触发分页与分页触发瀑布流的方法
- 十万条Access数据表分页的两个解决方法
- sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查询计划】
- 高效的mysql分页方法及原理
- asp又一个分页的代码例子
- SqlServer 2000、2005分页存储过程整理第1/3页
- JQuery的Pager分页器实现代码
- ADO存取数据库时如何分页显示
- 透彻掌握ASP分页技术很详细的分析
- jQuery实现的简单分页示例
- Android ListView分页功能实现方法
- 在ASP.NET 2.0中操作数据之二十四:分页和排序报表数据
- ajax无刷新分页的简单实现
- 一条SQL语句搞定Sql2000 分页
- 分页 SQLServer存储过程
- 实现SQL分页的存储过程代码
- sql分页查询几种写法
- SQL行号排序和分页(SQL查询中插入行号 自定义分页的另类实现)
- mysql 分页优化解析