映射注解,操作数据库表
public interface UtilMapper {
//检查表是否存在,注意表名 用 $
@Select("SELECT case when count(name)=0 then 'false' else 'true' end FROM sysobjects where name= '${tableName}'")
boolean existsTable(@Param("tableName") String tableName);
//执行sql语句
@Update("${sql}")
@Options(statementType = StatementType.STATEMENT)
void execute(@Param("sql") String sql);
}
映射注解,操作数据
public interface UserMapper {
@Insert("insert into sys_user(name,passWord) " +
"values(#{name},#{passWord})")
void add(UserEntity entity);
//使用其他方法生成sql
@UpdateProvider(type=UserProvider.class,method = "updateSql")
void update(UserEntity entity);
@Delete("delete from sys_user where id=#{id}")
void del(int id);
@Select("select * from sys_user where id=#{id}")
UserEntity get(int id);
@SelectProvider(type=UserProvider.class,method = "listSql")
List<Map<String,Object>> list(String key, int rows, int page);
@SelectProvider(type=UserProvider.class,method="countSql")
int count(String key);
}
sql语句生成,用于复杂sql的拼接与生成,其中有like 用法,使用concat连接
like CONCAT(’%’,#{key},’%’)
public class UserProvider {
public String updateSql(UserEntity entity){
return new SQL(){{
UPDATE("sys_user");
SET("name=#{name}");
if(StringUtils.isNotBlank( entity.getPassWord())){
SET("passWord=#{passWord}");
}
WHERE("id=#{id}");
}}.toString();
}
private String listWhere(String key){
String where="";
if(StringUtils.isNotBlank(key)){
where =" where name like CONCAT('%',#{key},'%') ";
}
return where;
}
//分页sql
public String listSql(String key,int rows,int page){
String where = listWhere(key);
Map<String,Integer> pObj = DataGridJson.compuPage(rows,page);
String sql =String.format("select * from " +
"(select ROW_NUMBER() over(order by id) rowid,id,name,dislog,dislogreason,dislogtime " +
"from sys_user %s)t where rowid between %d and %d",where,pObj.get("start"),pObj.get("end"));
return sql;
// return new SQL(){{
// SELECT("ROW_NUMBER() over(order by id) rowid,id,name,dislog,dislogreason,dislogtime");
// FROM("sys_user");
// if(StringUtils.isNotBlank(key)){
// WHERE("name like CONCAT('%',#{key},'%')");
// }
// }}.toString();
}
public String countSql(String key){
String where = listWhere(key);
String sql = String.format("select count(id) from sys_user %s",where);
return sql;
}
}
返回json列表对象封装
public class DataGridJson {
int total;
Object rows;
public int getTotal() {
return total;
}
public Object getRows() {
return rows;
}
public void setRows(Object rows) {
this.rows = rows;
}
public void setTotal(int total) {
this.total = total;
}
public DataGridJson(List list) {
this.total = list.size();
rows = list;
}
public DataGridJson(int total,List list){
this.total = total;
rows = list;
}
/**
* 计算分页
* @param rows
* @param page
* @return
*/
public static Map<String, Integer> compuPage(int rows, int page){
if (page == 0)
{
page = 1;
}
if (rows == 0)
{
rows = 20;
}
int start = (page - 1) * rows;
int end = start + rows;
start = page == 1 ? start : start + 1;
Map<String,Integer> result = new HashMap<String,Integer>();
result.put("start",start);
result.put("end",end);
return result;
}
}
分页查询调用方法:
@Override
public DataGridJson listUser(String key, int rows, int page) {
List list = userMapper.list(key,rows,page);
int count =userMapper.count(key);
DataGridJson obj = new DataGridJson(count,list);
return obj;
}