方式一
后端代码
TenantController.java
/**
* 分页查询数据
*
* @param query 查询对象
* @return PageList 分页对象
*/
@RequestMapping(value = "/pagelist", method = RequestMethod.POST)
public PageList<Tenant> json(@RequestBody TenantQuery query) {
//自定义方法
return tenantService.selectForPage(query);
}
ITenantService.java
/**
* <p>
* 服务类
* </p>
*
* @author snailisbull
* @since 2019-09-26
*/
public interface ITenantService extends IService<Tenant> {
PageList<Tenant> selectForPage(TenantQuery query);
void settled(Employee employee);
}
TenantServiceImpl .java
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
@Autowired
EmployeeMapper employeeMapper;
@Override
public PageList<Tenant> selectForPage(TenantQuery query) {
//总条数
Long total = baseMapper.selectForCount(query);
if (total==0){
return new PageList<>();
}
//列表
return new PageList<Tenant>(total,baseMapper.selectForList(query));
}
}
TenantMapper.java
public interface TenantMapper extends BaseMapper<Tenant> {
Long selectForCount(TenantQuery query);
List<Tenant> selectForList(TenantQuery query);
TenantMapper.xml
<sql id="whereSql">
<where>
<if test="keyword != null">
and (companyName like concat('%',#{keyword},'%')
or companyNum like concat('%',#{keyword},'%')
or state like concat('%',#{keyword},'%')
or address like concat('%',#{keyword},'%')
)
</if>
</where>
</sql>
<!--resultType="java.lang.Long"不写也是可以返回的-->
<!--查询一共有多少条-->
<select id="selectForCount" resultType="java.lang.Long">
select count(id) from t_tenant
<include refid="whereSql"/>
</select>
<select id="selectForList" resultType="TenantQuery" resultMap="BaseResultMap">
select id,
tenant_type,
companyName,
companyNum,
registerTime,
state,
address,
logo
from t_tenant
<include refid="whereSql"/>
limit #{start},#{rows}
</select>
方式二
需要使用mabatisplus
pom文件依赖包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.2.0</version>
</dependency>
<!--模板引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
需要借助代码生成工具
package cn.itsource.hrm;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.*;
/**
* 生成代码的主类
*/
public class GenteratorCode {
public static void main(String[] args) throws InterruptedException {
if(true) return;
//用来获取Mybatis-Plus.properties文件的配置信息
ResourceBundle rb = ResourceBundle.getBundle("mybatiesplus-config"); //不要加后缀
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(rb.getString("OutputDir"));
gc.setFileOverride(true);
gc.setActiveRecord(true);// 开启 activeRecord 模式
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setAuthor(rb.getString("author"));
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setTypeConvert(new MySqlTypeConvert());
dsc.setDriverName(rb.getString("jdbc.driver"));
dsc.setUsername(rb.getString("jdbc.user"));
dsc.setPassword(rb.getString("jdbc.pwd"));
dsc.setUrl(rb.getString("jdbc.url"));
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix(new String[] {
"t_" });// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel