涉及代码:
BaseDao.java
EntityUtil.java
Query.java
MysqlQuery.java
SqlProviderTemplate.java
BaseEntity.java
等
代码地址 :https://git.oschina.net/gdzs/mvn.git
package com.cn.web.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import com.cn.web.dao.utils.SqlProviderTemplate;
/**
* MyBatis 基础接口
*
* @author love
* @param <T> 处理的Entity对象
*/
public interface BaseDao<T> {
/**
* Insert语句从SqlProviderTemplate类中生成
* 其他参数如:@SelectKey(before=true, resultType=Long.class, keyProperty="id", statement={" select acct_seq.nextval from dual "})
*
* @param entity entity实体对象
* @return 影响条数
*/
@InsertProvider(type = SqlProviderTemplate.class, method = "insert")
@Options(keyColumn="id", keyProperty="id", useGeneratedKeys=true)
public int insert(T entity);
/**
* Update语句从SqlProviderTemplate类中生成
*
* @param entity entity实体对象
* @return 影响条数
*/
@UpdateProvider(type = SqlProviderTemplate.class, method = "update")
public int update(T entity);
/**
* 逻辑删除,更新 record_status(记录状态)为0(失效)
* Update语句从SqlProviderTemplate类中生成
*
* @param entity entity实体对象
* @return 影响条数
*/
@UpdateProvider(type = SqlProviderTemplate.class, method = "delete")
public int delete(T entity);
/**
* 检索,根据ID检索
* @param id 关键字
* @param entityClass entity对象类型
* @return entity实体Map
*/
@SelectProvider(type = SqlProviderTemplate.class, method = "selectByKey")
public Map<?, ?> selectByKey(@Param("id") Long id,Class<?> entityClass);
/**
* 检索,根据entity对象以及检索条件检索
*
* @param entity entity实体对象
* @return entity实体Map的List组合
*/
@SelectProvider(type = SqlProviderTemplate.class, method = "selectByParams")
public List<Map> selectByParams(T entity);
}
package com.cn.web.dao.utils;
import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;
import java.util.Map;
/**
* MyBatis 主要的SqlProvider生成类
*
* @author love
*/
public class SqlProviderTemplate{
/**
* 根据entity实体类生成插入SQL文
*
* @param entity entity实体类
*
* @return 插入SQL文
* @throws Exception 抛出异常
*/
public String insert(Object entity) throws Exception {
BEGIN();
INSERT_INTO(EntityUtil.tablename(entity));
EntityUtil.caculationColumnList(entity);
VALUES(EntityUtil.returnInsertColumnsName(entity), EntityUtil.returnInsertColumnsDefine(entity));
return SQL();
}
/**
* 根据entity实体类生成更新SQL文
*
* @param entity entity实体类
*
* @return 更新SQL文
* @throws Exception 抛出异常
*/
public String update(Object entity) throws Exception {
BEGIN();
UPDATE(EntityUtil.tablename(entity));
EntityUtil.caculationColumnList(entity);
SET(EntityUtil.getUpdateSet(entity));
WHERE(EntityUtil.getUpdateWhere(entity));
return SQL();
}
/**
* 根据entity实体类生成逻辑删除SQL文(更新record_status、update_user、update_time三个字段)
*
* @param entity entity实体类
*
* @return 逻辑删除SQL文
* @throws Exception 抛出异常
*/
public String delete(Object entity) throws Exception {
BEGIN();
UPDATE(EntityUtil.tablename(entity));
EntityUtil.caculationColumnList(entity);
SET(EntityUtil.getDeleteSet(entity));
WHERE(EntityUtil.getUpdateWhere(entity));
return SQL();
}
/**
* 根据id和entityClass生成检索id检索SQL文
*
* @param obj 包含: Long id,Class<?> entityClass
*
* @return id检索SQL文
* @throws Exception 抛出异常
*/
public String selectByKey(Map<?, ?> obj) throws Exception {
BEGIN();
Class<?> cla = (Class<?>) obj.get("1");
SELECT(EntityUtil.getSelectColumnsName(cla));
FROM(EntityUtil.getSelectTableName(cla));
WHERE(EntityUtil.getSelectByKey());
return SQL();
}
/**
* 根据id和entityClass生成检索id检索SQL文
*
* @param obj 包含: Long id,Class<?> entityClass
*
* @return id检索SQL文
* @throws Exception 抛出异常
*/
public String selectByParams(Object entity) throws Exception {
BEGIN();
SELECT(EntityUtil.getSelectColumnsName(entity.getClass()));
FROM(EntityUtil.getSelectTableName(entity.getClass()));
WHERE(EntityUtil.getSelectSql(entity));
return SQL();
}
}
package com.cn.web.dao.utils;
/**
* 数据库Query
* 提供查询、删除、插入、更新等一系列操作数据库方法
* @author Rain
*
*/
public interface Query {
/**
* 获取sql语句
* @return
*/
public String getSql();
/**
* 添加sql语句
* @param startSql
* @return
*/
public Query appendSql(String startSql);
/**
* 设置equal的sql语句
* @param key
* @return
*/
public Query setEqual(String key);
/**
* 设置equal的sql语句
* @param key
* @return
*/
public Query setEqual(String key,String value);
/**
* 设置like的sql语句
* @param key
* @param value
* @param prefix
* @param suffix
* @return
*/
public Query setLike(String key,String value,String prefix,String suffix);
/**
* 设置in的sql语句
* @param key
* @return
*/
public Query setIn(String key,String ...values);
/**
* 设置like的sql语句
* @param key
* @param value
* @param prefix
* @param suffix
* @return
*/
public Query setLike(String key,String prefix,String suffix);
/**
* 设置大于的sql语句
* @param key
* @return
*/
public Query setGreater(String key);
/**
* 设置小于的sql语句
* @param key
* @return
*/
public Query setLess(String key);
}
package com.cn.web.dao.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.cn.web.exception.SqlProviderException;
/**
* 1.简化sql条件拼接
*
* 2.封装查询
*
* @author ygc
*
*/
public class MysqlQuery implements Query{
protected StringBuffer sql = new StringBuffer();
public String getSql() {
return sql.toString();
}
protected Map inMap = new HashMap();
//参数
protected Map paramsMap = new HashMap();
public MysqlQuery() {
sql.append(" 1 = 1 ");
}
public MysqlQuery(boolean flag) {
if(flag){
sql.append(" record_status = 1 ");
}else{
sql.append(" 1 = 1 ");
}
}
public Query appendSql(String sqlStr){
if( "".equals(sqlStr)){
throw new SqlProviderException("this sql is null!");
}
sql.append(" and ");
sql.append(sqlStr);
return this;
}
@Override
public Query setEqual(String key) {
if( "".equals(key)){
throw new SqlProviderException("this sql key is null!");
}
sql.append(" and ");
sql.append( CamelNameUtils.camelhumpToUnderline(key) +" =#{"+key+"} ");
return this;
}
@Override
public Query setEqual(String key,String value) {
if(value==null || "".equals(value)){
return this;
}
sql.append(" and ");
sql.append( CamelNameUtils.camelhumpToUnderline(key) +" =#{"+key+"} ");
return this;
}
public Query setIn(String key,String ...values){
if(values==null || values.length==0){
return this;
}
StringBuffer sb = new StringBuffer("");
for(String value:values){
if(value==null || "".equals(value)){
continue;
}
sb.append(value);
sb.append(",");
}
String inStr = sb.toString();
if(inStr.endsWith(",")){
inStr = inStr.substring(0,inStr.length()-1);
}
if(StringUtils.isNotEmpty(inStr)){
sql.append(" and ");
sql.append( CamelNameUtils.camelhumpToUnderline(key) +" in ("+inStr+") ");
return this;
}
return this;
}
public Query setLike(String key,String value,String prefix,String suffix){
if(value==null || "".equals(value)){
return this;
}
if(prefix==null)prefix="";
if(suffix==null)suffix="";
sql.append(" and ");
sql.append( CamelNameUtils.camelhumpToUnderline(key) +" like '"+prefix+value+suffix+"' ");
return this;
}
public Query setLike(String key,String prefix,String suffix){
if(prefix==null)prefix="";
if(suffix==null)suffix="";
sql.append(" and ");
//and title like CONCAT('%','${title}','%' )
sql.append( CamelNameUtils.camelhumpToUnderline(key) +" like CONCAT( '"+prefix+"','${"+key+suffix+"}','"+suffix+"') ");
return this;
}
public Query setGreater(String key){
if( "".equals(key)){
throw new SqlProviderException("this sql key is null!");
}
//大于
//and sort_index > #{sortIndex}
sql.append(" and ");
sql.append( CamelNameUtils.camelhumpToUnderline(key) +" > #{"+key+"} ");
return this;
}
public Query setLess(String key){
if( "".equals(key)){
throw new SqlProviderException("this sql key is null!");
}
//小于
//and sort_index > #{sortIndex}
sql.append(" and ");
sql.append( CamelNameUtils.camelhumpToUnderline(key) +" < #{"+key+"} ");
return this;
}
}