mybatis public CRUD
1. Specific mapper
package com.kevin.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.kevin.dto.AllType;
import com.kevin.mapper.common.CommonMapper;
@Mapper
public interface AllTypeMapper extends CommonMapper<AllType> {
}
2. Public mapper:
package com.kevin.mapper.common;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.SelectProvider;
/**
* CRUD public classes
* @author Kevin Zhang @Date 2018 Mar 20 4:55:23 PM
*/
public interface CommonMapper<T> {
@SelectProvider(type = CommonProvider.class, method = "save")
public void save(T t);
@SelectProvider(type = CommonProvider.class , method = "delete")
public void delete(Integer id, Class<T> t);
/**
* Note that before updating, the input parameter t must be read from the database, otherwise it is easy to set other fields to empty
*/
@SelectProvider(type = CommonProvider.class, method = "update")
public void update(Integer id, Map<String,Object> map, Class<T> t);
@SelectProvider(type = CommonProvider.class, method = "find")
T findOne(T t);
@SelectProvider(type = CommonProvider.class, method = "find")
List<T> find(T t);
}
3. Public CRUD
package com.kevin.mapper.common;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.jdbc.SQL;
import com.kevin.annotation.Column;
import com.kevin.annotation.Ignore;
import com.kevin.annotation.Table;
/**
* 公用crud
*
* @author Kevin Zhang @Date 2018年3月15日 下午4:43:52
*/
public class CommonProvider {
/**
* 保存对象至数据库
*/
public static <T> String save(T t) throws IllegalAccessException {
String sql = new SQL() {
{
Class<?> entityClass = t.getClass();
String tableName = tableName(entityClass);
// Each field, if not null, and not "", is used as a query condition, and is connected with and to generate SQL
Field [] fields = entityClass.getDeclaredFields();
if (fields != null && fields.length > 0) {
// generate insert
INSERT_INTO(tableName);
for (Field field : fields) {
String fieldName = field.getName();
Ignore ignore = field.getAnnotation(Ignore.class);
if (!"serialVersionUID".equals(fieldName) && ignore == null) {
field.setAccessible(true);
Column column = field.getAnnotation(Column.class);
if (column != null) { // If there is a column annotation, use the column's annotation name
fieldName = column.name();
}
Object o = field.get(t);
if(!"id".equals(fieldName) || ("id".equals(fieldName) && o != null)){
if (o instanceof String) { // if string Type, quotes should be added in SQL
VALUES(fieldName, "'" + o.toString() + "'");
} else if(o instanceof Date) { // If it is date format, convert to string: yyyy-MM-dd HH:mm:ss
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = format.format((Date)o);
VALUES(fieldName, "'" + dateString + "'");
} else { // If it is not a string type, directly fill in the value
String value = o == null ? " null" : o.toString();
VALUES(fieldName, value);
}
}
}
}
}
}
}.toString();
return sql;
}
/**
* Delete the specified table, referring to the record of row id
*/
public static <T> String delete(final Integer id, final Class<T> t) {
String sql = new SQL() {
{
String tableName = tableName(t);
DELETE_FROM(tableName);
WHERE("id = " + id);
}
}.toString();
return sql;
}
/**
* update database record
*/
public static <T> String update(Integer id, Map<String,Object> map, Class<T> t) {
String sql = new SQL() {
{
String tableName = tableName(t);
Set<String> columns = map.keySet();
if (id != null && columns != null && columns.size() > 0) {
// 生成update
UPDATE(tableName);
for (String column : columns) {
Object o = map.get(column);
if (o instanceof String) { // 如果string类型, SQL里要加引号
SET(column + "='" + o.toString() + "'");
} else { // If it is not a string type, fill in the value directly
String value = o == null ? "null" : o.toString();
SET(column + "=" + value);
}
}
//where condition
WHERE( "id = " + id);
}
}
}.toString();
return sql;
}
/**
* Find records according to query conditions: query conditions are connected with and, null or "" cannot be used as query conditions
*/
public static < T>String find(T t) throws IllegalAccessException {
return new SQL() {
{
Class<?> entityClass = t.getClass();
String tableName = tableName(entityClass);
// Each field, if not null and not "", is used as a query condition, and is connected with and to generate SQL
Field[ ] fields = entityClass.getDeclaredFields();
if (fields != null && fields.length > 0) {
SELECT("*");
FROM(tableName);
for (Field field : fields) {
String fieldName = field.getName( );
Ignore ignore = field.getAnnotation(Ignore.class);
if (!"serialVersionUID".equals(fieldName) && ignore == null) {
field.setAccessible(true);
Column column = field.getAnnotation(Column.class);
if (column != null) { // If there is a column annotation, use the column's annotation name
fieldName = column.name();
}
Object o = field.get(t );
// The field value is not null, and not "", it is used as a condition to query
if (o != null && !"".equals(o.toString())) {
if (o instanceof String) { / / If string type, add quotes in SQL
WHERE(fieldName + "='" + o + "'");
} else if(o instanceof Date){ // If Date is the query condition
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String value = format.format((Date)o);
WHERE(fieldName + "='" + value + "'");
} else { // If it is not a string type, fill in the value directly
WHERE(fieldName + "=" + o);
}
}
}
}
}
}
}.toString();
}
/**
* Get the table name according to the object
*/
private static < T> String tableName(Class<?> entityClass) {
// Take out the table name
Table a = entityClass.getAnnotation(Table.class);
String tableName = null;
if (a != null) { // There is an annotation to indicate the table name
tableName = a.name();
} else { // If there is no annotation, use the class name
String className = entityClass.getName();
tableName = className.substring(className.lastIndexOf("."));
}
return tableName;
}
}