一 DeptDao
package org.fkit.hrm.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.fkit.hrm.dao.provider.DeptDynaSqlProvider;
import org.fkit.hrm.domain.Dept;
import static org.fkit.hrm.util.common.HrmConstants.DEPTTABLE;
public interface DeptDao {
// 动态查询
@SelectProvider(type = DeptDynaSqlProvider.class, method = "selectWhitParam")
List<Dept> selectByPage(Map<String, Object> params);
@SelectProvider(type = DeptDynaSqlProvider.class, method = "count")
Integer count(Map<String, Object> params);
@Select("select * from " + DEPTTABLE + " ")
List<Dept> selectAllDept();
@Select("select * from " + DEPTTABLE + " where ID = #{id}")
Dept selectById(int id);
// 根据id删除部门
@Delete(" delete from " + DEPTTABLE + " where id = #{id} ")
void deleteById(Integer id);
// 动态插入部门
@SelectProvider(type = DeptDynaSqlProvider.class, method = "insertDept")
void save(Dept dept);
// 动态修改部门
@SelectProvider(type = DeptDynaSqlProvider.class, method = "updateDept")
void update(Dept dept);
}
二 DeptDynaSqlProvider
package org.fkit.hrm.dao.provider;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
import org.fkit.hrm.domain.Dept;
import static org.fkit.hrm.util.common.HrmConstants.DEPTTABLE;
public class DeptDynaSqlProvider {
// 分页动态查询
public String selectWhitParam(Map<String, Object> params){
String sql = new SQL(){
{
SELECT("*");
FROM(DEPTTABLE);
if(params.get("dept") != null){
Dept dept = (Dept) params.get("dept");
if(dept.getName() != null && !dept.getName().equals("")){
WHERE(" name LIKE CONCAT ('%',#{dept.name},'%') ");
}
}
}
}.toString();
if(params.get("pageModel") != null){
sql += " limit #{pageModel.firstLimitParam} , #{pageModel.pageSize} ";
}
return sql;
}
// 动态查询总数量
public String count(Map<String, Object> params){
return new SQL(){
{
SELECT("count(*)");
FROM(DEPTTABLE);
if(params.get("dept") != null){
Dept dept = (Dept) params.get("dept");
if(dept.getName() != null && !dept.getName().equals("")){
WHERE(" name LIKE CONCAT ('%',#{dept.name},'%') ");
}
}
}
}.toString();
}
// 动态插入
public String insertDept(Dept dept){
return new SQL(){
{
INSERT_INTO(DEPTTABLE);
if(dept.getName() != null && !dept.getName().equals("")){
VALUES("name", "#{name}");
}
if(dept.getRemark() != null && !dept.getRemark().equals("")){
VALUES("remark", "#{remark}");
}
}
}.toString();
}
// 动态更新
public String updateDept(Dept dept){
return new SQL(){
{
UPDATE(DEPTTABLE);
if(dept.getName() != null){
SET(" name = #{name} ");
}
if(dept.getRemark() != null){
SET(" remark = #{remark} ");
}
WHERE(" id = #{id} ");
}
}.toString();
}
}