├── README.md ├── pom.xml ├── src └── main │ ├── java │ └── com │ │ └── svili │ │ └── portal │ │ ├── crud │ │ ├── CrudServiceImpl.java │ │ ├── CrudServiceInter.java │ │ ├── CrudTest.java │ │ ├── GeneralDaoProvider.java │ │ ├── GeneralQueryParam.java │ │ ├── SQLColumn.java │ │ ├── common │ │ │ ├── DateUtil.java │ │ │ ├── NumberUtil.java │ │ │ ├── PersistentUtil.java │ │ │ ├── StringUtil.java │ │ │ ├── TypeCastUtil.java │ │ │ └── TypeIdentifyUtil.java │ │ └── utils │ │ │ ├── DateFieldReflectUtil.java │ │ │ ├── EnumFieldReflectUtil.java │ │ │ ├── FieldReflectUtil.java │ │ │ ├── GeneralMapperReflectUtil.java │ │ │ ├── NumberFieldReflectUtil.java │ │ │ └── SQLColumnFactory.java │ │ ├── dao │ │ └── GeneralDao.java │ │ ├── pojo │ │ └── User.java │ │ └── type │ │ └── DataState.java │ ├── resources │ ├── configs │ │ ├── db.properties │ │ ├── log4j.properties │ │ ├── mybatis-config.xml │ │ ├── spring-mybatis.xml │ │ ├── spring-servlet.xml │ │ └── spring.xml │ └── mapping │ │ └── GeneralMapper.xml │ └── webapp │ └── WEB-INF │ ├── errorpage │ ├── 400.jsp │ ├── 404.jsp │ ├── 405.jsp │ └── 500.jsp │ └── web.xml └── test.txt /README.md: -------------------------------------------------------------------------------- 1 | # portal 2 |   ## version:V1.0 3 | ## 实现功能 4 | 1、对单条数据的查询、插入、更新、删除。 5 | 2、批量操作数据:批量插入,根据条件删除,根据条件更新指定的列名-字段值。 6 | 3、高级查询:可设置查询列,查询条件,排序,分页 7 | **attention** 8 | 1.MySQL支持所有操作,Oracle目前只支持增(insert)、删、查。 9 | 2.java 实体类字段名为驼峰风格,数据库字段名为下划线风格。 10 | 3.每张表都必须要有一个主键字段,字段类型为Integer或String。 11 | 4.必须在实体类中注明主键,表名(可选),使用JPA注解。 12 | 5.对于枚举类型的处理,以枚举ordinal()值,存取到数据库中。 13 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.svili 5 | mybatis-crud 6 | war 7 | 0.0.1-SNAPSHOT 8 | mybatis-crud Maven Webapp 9 | http://maven.apache.org 10 | 11 | UTF-8 12 | 13 | 4.1.8.RELEASE 14 | 15 | 3.2.6 16 | 17 | 1.0.26 18 | 19 | 1.7.7 20 | 1.2.17 21 | 22 | 23 | 24 | 25 | 26 | junit 27 | junit 28 | 4.11 29 | test 30 | 31 | 32 | 33 | 34 | javax 35 | javaee-api 36 | 7.0 37 | 38 | 39 | 40 | 41 | org.springframework 42 | spring-core 43 | ${spring.version} 44 | 45 | 46 | org.springframework 47 | spring-web 48 | ${spring.version} 49 | 50 | 51 | org.springframework 52 | spring-oxm 53 | ${spring.version} 54 | 55 | 56 | org.springframework 57 | spring-tx 58 | ${spring.version} 59 | 60 | 61 | org.springframework 62 | spring-jdbc 63 | ${spring.version} 64 | 65 | 66 | org.springframework 67 | spring-webmvc 68 | ${spring.version} 69 | 70 | 71 | org.springframework 72 | spring-aop 73 | ${spring.version} 74 | 75 | 76 | org.springframework 77 | spring-context-support 78 | ${spring.version} 79 | 80 | 81 | org.springframework 82 | spring-test 83 | ${spring.version} 84 | 85 | 86 | 87 | 88 | jstl 89 | jstl 90 | 1.2 91 | 92 | 93 | 94 | 95 | org.mybatis 96 | mybatis 97 | ${mybatis.version} 98 | 99 | 100 | 101 | 102 | org.mybatis 103 | mybatis-spring 104 | 1.2.2 105 | 106 | 107 | 108 | 109 | mysql 110 | mysql-connector-java 111 | 5.1.30 112 | 113 | 114 | 115 | 116 | commons-dbcp 117 | commons-dbcp 118 | 1.2.2 119 | 120 | 121 | 122 | 123 | com.alibaba 124 | druid 125 | ${druid-version} 126 | 127 | 128 | 129 | 130 | org.codehaus.jackson 131 | jackson-mapper-asl 132 | 1.9.13 133 | 134 | 135 | 136 | org.codehaus.jackson 137 | jackson-core-asl 138 | 1.9.13 139 | 140 | 141 | 142 | 143 | org.aspectj 144 | aspectjweaver 145 | 1.7.3 146 | 147 | 148 | 149 | org.aspectj 150 | aspectjrt 151 | 1.7.3 152 | 153 | 154 | 155 | 156 | javax.persistence 157 | persistence-api 158 | 1.0.2 159 | 160 | 161 | 162 | 163 | 164 | log4j 165 | log4j 166 | ${log4j-version} 167 | 168 | 169 | 170 | org.slf4j 171 | slf4j-api 172 | ${slf4j-version} 173 | 174 | 175 | 176 | org.slf4j 177 | slf4j-log4j12 178 | ${slf4j-version} 179 | 180 | 181 | 182 | 183 | org.bgee.log4jdbc-log4j2 184 | log4jdbc-log4j2-jdbc4.1 185 | 1.16 186 | 187 | 188 | 189 | 190 | 191 | mybatis-crud 192 | 193 | 194 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/CrudServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import javax.annotation.Resource; 9 | 10 | import org.springframework.stereotype.Service; 11 | 12 | import com.svili.portal.crud.common.PersistentUtil; 13 | import com.svili.portal.crud.utils.GeneralMapperReflectUtil; 14 | import com.svili.portal.dao.GeneralDao; 15 | 16 | /** 17 | * 通用Crud 数据访问层接口实现类 18 | * 19 | * @author svili 20 | * @date 2016年11月11日 21 | * 22 | */ 23 | @Service("crudService") 24 | public class CrudServiceImpl implements CrudServiceInter { 25 | 26 | @Resource(name = "generalDao") 27 | private GeneralDao dao; 28 | 29 | @Override 30 | public T selectByPrimaryKey(Class clazz, Object primaryValue) throws Exception { 31 | Map param = new HashMap(); 32 | 33 | String tableName = PersistentUtil.getTableName(clazz); 34 | String primaryKey = PersistentUtil.getPrimaryKey(clazz); 35 | List queryColumn = GeneralMapperReflectUtil.getAllColumnNames(clazz); 36 | 37 | param.put("tableName", tableName); 38 | param.put("primaryKey", primaryKey); 39 | param.put("primaryValue", primaryValue); 40 | param.put("queryColumn", queryColumn); 41 | 42 | return GeneralMapperReflectUtil.parseToBean(dao.selectByPrimaryKey(param), clazz); 43 | } 44 | 45 | @Override 46 | public int insertSelective(T t) throws Exception { 47 | Map param = new HashMap(); 48 | 49 | Class clazz = t.getClass(); 50 | 51 | String tableName = PersistentUtil.getTableName(clazz); 52 | List SQLColumns = GeneralMapperReflectUtil.getSQLColumnsExceptNull(t); 53 | 54 | param.put("tableName", tableName); 55 | param.put("SQLColumns", SQLColumns); 56 | 57 | return dao.insert(param); 58 | } 59 | 60 | @Override 61 | public int insert(T t) throws Exception { 62 | Map param = new HashMap(); 63 | 64 | Class clazz = t.getClass(); 65 | 66 | String tableName = PersistentUtil.getTableName(clazz); 67 | List SQLColumns = GeneralMapperReflectUtil.getAllSQLColumns(t); 68 | 69 | param.put("tableName", tableName); 70 | param.put("SQLColumns", SQLColumns); 71 | return dao.insert(param); 72 | } 73 | 74 | @Override 75 | public int insertBatch(List list) throws Exception { 76 | Map param = new HashMap(); 77 | 78 | String tableName = ""; 79 | List columnNames = new ArrayList(); 80 | 81 | List> dataList = new ArrayList>(list.size() + 1); 82 | 83 | for (T t : list) { 84 | if (tableName.equals("")) { 85 | Class clazz = t.getClass(); 86 | tableName = PersistentUtil.getTableName(clazz); 87 | } 88 | if (columnNames.size() == 0) { 89 | Class clazz = t.getClass(); 90 | columnNames = GeneralMapperReflectUtil.getAllColumnNames(clazz); 91 | } 92 | List SQLColumns = GeneralMapperReflectUtil.getAllSQLColumns(t); 93 | dataList.add(SQLColumns); 94 | } 95 | 96 | param.put("tableName", tableName); 97 | param.put("columnNames", columnNames); 98 | param.put("dataList", dataList); 99 | 100 | return dao.insertBatch(param); 101 | } 102 | 103 | @Override 104 | public int deleteByPrimaryKey(Class clazz, Object primaryValue) { 105 | Map param = new HashMap(); 106 | 107 | String tableName = PersistentUtil.getTableName(clazz); 108 | String primaryKey = PersistentUtil.getPrimaryKey(clazz); 109 | 110 | param.put("tableName", tableName); 111 | param.put("primaryKey", primaryKey); 112 | param.put("primaryValue", primaryValue); 113 | 114 | return dao.deleteByPrimaryKey(param); 115 | } 116 | 117 | @Override 118 | public int deleteByCondition(Class clazz, String conditionExp, Map conditionParam) { 119 | Map param = new HashMap(); 120 | 121 | String tableName = PersistentUtil.getTableName(clazz); 122 | 123 | param.put("tableName", tableName); 124 | param.put("conditionExp", conditionExp); 125 | param.put("conditionParam", conditionParam); 126 | 127 | return dao.deleteByCondition(param); 128 | } 129 | 130 | @Override 131 | public int updateByPrimaryKey(T t) throws Exception { 132 | Map param = new HashMap(); 133 | 134 | Class clazz = t.getClass(); 135 | 136 | String tableName = PersistentUtil.getTableName(clazz); 137 | String primaryKey = PersistentUtil.getPrimaryKey(clazz); 138 | 139 | List SQLColumns = GeneralMapperReflectUtil.getSQLColumns(t,false,true); 140 | 141 | Object primaryValue = GeneralMapperReflectUtil.getPrimaryValue(t); 142 | 143 | param.put("tableName", tableName); 144 | param.put("primaryKey", primaryKey); 145 | param.put("primaryValue", primaryValue); 146 | param.put("SQLColumns", SQLColumns); 147 | 148 | return dao.updateByPrimaryKey(param); 149 | } 150 | 151 | @Override 152 | public int updateByPrimaryKeySelective(T t) throws Exception { 153 | Map param = new HashMap(); 154 | 155 | Class clazz = t.getClass(); 156 | 157 | String tableName = PersistentUtil.getTableName(clazz); 158 | String primaryKey = PersistentUtil.getPrimaryKey(clazz); 159 | 160 | List SQLColumns = GeneralMapperReflectUtil.getSQLColumns(t,false,false); 161 | 162 | Object primaryValue = GeneralMapperReflectUtil.getPrimaryValue(t); 163 | 164 | param.put("tableName", tableName); 165 | param.put("primaryKey", primaryKey); 166 | param.put("primaryValue", primaryValue); 167 | param.put("SQLColumns", SQLColumns); 168 | 169 | return dao.updateByPrimaryKey(param); 170 | } 171 | 172 | @Override 173 | public int updateByConditionSelective(Class clazz, Map columnValueMapping, 174 | String conditionExp, Map conditionParam) throws Exception { 175 | Map param = new HashMap(); 176 | 177 | String tableName = PersistentUtil.getTableName(clazz); 178 | 179 | param.put("tableName", tableName); 180 | param.put("columnValueMapping", columnValueMapping); 181 | param.put("conditionExp", conditionExp); 182 | param.put("conditionParam", conditionParam); 183 | 184 | return dao.updateByConditionSelective(param); 185 | } 186 | 187 | @Override 188 | public List selectAdvanced(Class clazz, GeneralQueryParam queryParam) throws Exception { 189 | List result = new ArrayList(); 190 | 191 | queryParam.setQueryColumn(GeneralMapperReflectUtil.getAllColumnNames(clazz)); 192 | 193 | List> list = selectAdvancedByColumn(clazz, queryParam); 194 | 195 | if (list != null && list.size() != 0) { 196 | for (Map mapping : list) { 197 | result.add(GeneralMapperReflectUtil.parseToBean(mapping, clazz)); 198 | } 199 | } 200 | return result; 201 | } 202 | 203 | @Override 204 | public List> selectAdvancedByColumn(Class clazz, GeneralQueryParam queryParam) 205 | throws Exception { 206 | Map param = new HashMap(); 207 | 208 | String tableName = PersistentUtil.getTableName(clazz); 209 | 210 | param.put("tableName", tableName); 211 | param.put("queryColumn", queryParam.getQueryColumn()); 212 | param.put("conditionExp", queryParam.getConditionExp()); 213 | param.put("conditionParam", queryParam.getConditionParam()); 214 | param.put("orderExp", queryParam.getOrderExp()); 215 | 216 | if (queryParam.getPageSize() != null && queryParam.getPageNo() != null) { 217 | Map page = new HashMap(); 218 | page.put("pageSize", queryParam.getPageSize()); 219 | page.put("startRowNo", (queryParam.getPageNo() - 1) * queryParam.getPageSize()); 220 | param.put("page", page); 221 | } 222 | 223 | return dao.selectAdvanced(param); 224 | } 225 | 226 | } 227 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/CrudServiceInter.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | /** 7 | * 通用Crud 数据访问层接口 8 | * 9 | * @author svili 10 | * @date 2016年11月11日 11 | * 12 | */ 13 | public interface CrudServiceInter { 14 | 15 | /** 16 | * 根据主键查询 17 | * 18 | * @param 19 | * pojo类 20 | * @param clazz 21 | * pojo类-class对象 22 | * @param primaryValue 23 | * 主键值 24 | * @return pojo对象 25 | * @throws Exception 26 | */ 27 | T selectByPrimaryKey(Class clazz, Object primaryValue) throws Exception; 28 | 29 | /** 30 | * 插入数据
31 | * 包括null值 32 | * 33 | * @param 34 | * pojo类 35 | * @param t 36 | * pojo对象 37 | * @return 数据条数 38 | * @throws Exception 39 | */ 40 | int insert(T t) throws Exception; 41 | 42 | /** 43 | * 插入数据
44 | * 不包括null值 45 | * 46 | * @param 47 | * pojo类 48 | * @param t 49 | * pojo对象 50 | * @return 数据条数 51 | * @throws Exception 52 | */ 53 | int insertSelective(T t) throws Exception; 54 | 55 | /** 56 | * 批量插入
57 | * 包括null值 58 | * 59 | * @param list 60 | * 数据集 61 | * @return 数据 62 | * @throws Exception 63 | */ 64 | int insertBatch(List list) throws Exception; 65 | 66 | /** 67 | * 删除
68 | * 根据主键删除 69 | * 70 | * @param 71 | * pojo类 72 | * @param clazz 73 | * pojo类-class对象 74 | * @param primaryValue 75 | * 主键值 76 | * @return 数据条数 77 | */ 78 | int deleteByPrimaryKey(Class clazz, Object primaryValue); 79 | 80 | /** 81 | * 删除
82 | * 根据条件删除 83 | * 84 | * @param 85 | * pojo类 86 | * @param clazz 87 | * pojo类-class对象 88 | * @param conditionExp 89 | * 查询条件 where 表达式 @see GeneralQueryParam 90 | * 91 | * @param conditionParam 92 | * 查询条件 where 表达式中的参数集 @see GeneralQueryParam 93 | * @return 数据条数 94 | */ 95 | int deleteByCondition(Class clazz, String conditionExp, Map conditionParam); 96 | 97 | /** 98 | * 更新
99 | * 根据主键更新
100 | * 更新pojo的所有字段,包括空值(null值)字段 101 | * 102 | * @param 103 | * pojo类 104 | * @param t 105 | * pojo对象 106 | * @return 数据条数 107 | * @throws Exception 108 | */ 109 | int updateByPrimaryKey(T t) throws Exception; 110 | 111 | /** 112 | * 更新
113 | * 根据主键更新
114 | * 更新pojo的非空字段 115 | * 116 | * @param 117 | * pojo类 118 | * @param t 119 | * pojo对象 120 | * @return 数据条数 121 | * @throws Exception 122 | */ 123 | int updateByPrimaryKeySelective(T t) throws Exception; 124 | 125 | /** 126 | * 更新
127 | * 根据条件更新
128 | * 更新pojo的指定字段集 129 | * 130 | * @param clazz 131 | * pojo类-class对象 132 | * @param columnValueMapping 133 | * 需要更新的列名-值,注意列名必须与数据库中列名一致 134 | * @param conditionExp 135 | * 查询条件 where 表达式 @see GeneralQueryParam 136 | * 137 | * @param conditionParam 138 | * 查询条件 where 表达式中的参数集 @see GeneralQueryParam 139 | * @return 数据条数 140 | * @throws Exception 141 | */ 142 | int updateByConditionSelective(Class clazz, Map columnValueMapping, String conditionExp, 143 | Map conditionParam) throws Exception; 144 | 145 | /** 146 | * 高级查询 147 | * 148 | * @param clazz 149 | * pojo类-class对象 150 | * @param queryParam 151 | * 查询参数 152 | * @return 153 | * @throws Exception 154 | */ 155 | List selectAdvanced(Class clazz, GeneralQueryParam queryParam) throws Exception; 156 | 157 | /** 158 | * 高级查询,指定返回列 159 | * 160 | * @param clazz 161 | * pojo类-class对象 162 | * @param queryParam 163 | * 查询参数 164 | * @return 165 | * @throws Exception 166 | */ 167 | List> selectAdvancedByColumn(Class clazz, GeneralQueryParam queryParam) throws Exception; 168 | 169 | } 170 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/CrudTest.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Date; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import javax.annotation.Resource; 10 | 11 | import org.springframework.stereotype.Controller; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.ResponseBody; 14 | 15 | import com.svili.portal.dao.GeneralDao; 16 | import com.svili.portal.pojo.User; 17 | import com.svili.portal.type.DataState; 18 | 19 | @Controller 20 | public class CrudTest { 21 | 22 | @Resource(name = "crudService") 23 | private CrudServiceInter service; 24 | 25 | @Resource(name = "generalDao") 26 | private GeneralDao dao; 27 | 28 | @RequestMapping(value = "/test/crud/selectByPrimaryKey.json") 29 | @ResponseBody 30 | public User test_select() throws Exception { 31 | 32 | /*Map result = new HashMap(); 33 | 34 | result = service.selectByPrimaryKey(User.class, 1);*/ 35 | User user = service.selectByPrimaryKey(User.class, 1); 36 | 37 | return user; 38 | } 39 | 40 | @RequestMapping(value = "/test/crud/insertSelective.json") 41 | @ResponseBody 42 | public Map test_insert() throws Exception { 43 | Map result = new HashMap(); 44 | 45 | User user = new User(); 46 | user.setDeptId(1); 47 | user.setEmail("123456789@.com"); 48 | user.setJob("java programer"); 49 | user.setLoginName("LittleNewbie"); 50 | user.setMobilePhone("18166668888"); 51 | user.setOfficePhone("020-70999-8888"); 52 | user.setPassword("123456"); 53 | user.setState(DataState.UNEFFECT); 54 | user.setUserName("svili"); 55 | user.setCreateTime(new Date()); 56 | 57 | int i = service.insertSelective(user); 58 | 59 | result.put("row", i); 60 | 61 | return result; 62 | } 63 | 64 | @RequestMapping(value = "/test/crud/insertBatch.json") 65 | @ResponseBody 66 | public Map test_insertBatch() throws Exception { 67 | Map result = new HashMap(); 68 | 69 | List list = new ArrayList(); 70 | 71 | for(int i =1 ; i <10 ;i++){ 72 | User user = new User(); 73 | user.setDeptId(1+i); 74 | user.setEmail("123456789@.com"+i); 75 | user.setJob("java programer"+i); 76 | user.setLoginName("LittleNewbie"+i); 77 | user.setMobilePhone("18166668888"+i); 78 | user.setOfficePhone("020-70999-8888"+i); 79 | user.setPassword("123456"+i); 80 | user.setState(DataState.UNEFFECT); 81 | user.setUserName("svili"+i); 82 | user.setCreateTime(new Date()); 83 | list.add(user); 84 | } 85 | 86 | int num = service.insertBatch(list); 87 | 88 | result.put("row", num); 89 | 90 | return result; 91 | } 92 | 93 | @RequestMapping(value = "/test/crud/updateByPrimaryKey.json") 94 | @ResponseBody 95 | public Map test_update() throws Exception { 96 | Map result = new HashMap(); 97 | 98 | User user = new User(); 99 | user.setUserId(13); 100 | user.setDeptId(1); 101 | user.setEmail("123456789@.com"); 102 | user.setJob("java programer"); 103 | /* 104 | * user.setLoginName("LittleNewbie"); 105 | * user.setMobilePhone("18166668888"); 106 | * user.setOfficePhone("020-70999-8888"); user.setPassword("123456"); 107 | */ 108 | user.setState(DataState.UNEFFECT); 109 | user.setUserName("svili"); 110 | user.setCreateTime(new Date()); 111 | 112 | int i = service.updateByPrimaryKey(user); 113 | 114 | result.put("row", i); 115 | 116 | return result; 117 | } 118 | 119 | @RequestMapping(value = "/test/crud/updateByPrimaryKeySelective.json") 120 | @ResponseBody 121 | public Map test_updateSelective() throws Exception { 122 | Map result = new HashMap(); 123 | 124 | User user = new User(); 125 | user.setUserId(12); 126 | user.setDeptId(1); 127 | user.setEmail("123456789@.com"); 128 | user.setJob("java programer"); 129 | user.setLoginName("LittleNewbie"); 130 | user.setMobilePhone("18166668888"); 131 | user.setOfficePhone("020-70999-8888"); 132 | user.setPassword("123456"); 133 | user.setState(DataState.UNEFFECT); 134 | user.setUserName("svili"); 135 | user.setCreateTime(new Date()); 136 | 137 | int i = service.updateByPrimaryKeySelective(user); 138 | 139 | result.put("row", i); 140 | 141 | return result; 142 | } 143 | 144 | @RequestMapping(value = "/test/crud/updateByCondition.json") 145 | @ResponseBody 146 | public Map test_updateByCondition() throws Exception { 147 | Map result = new HashMap(); 148 | 149 | Map columnValueMapping = new HashMap(); 150 | columnValueMapping.put("user_name", "update"); 151 | columnValueMapping.put("dept_id", 99); 152 | columnValueMapping.put("state", DataState.EFFECT.ordinal()); 153 | 154 | String conditionExp = "dept_id != #{conditionParam.deptId}"; 155 | Map conditionParam = new HashMap(); 156 | conditionParam.put("deptId", 1); 157 | 158 | int i = service.updateByConditionSelective(User.class, columnValueMapping, conditionExp, conditionParam); 159 | 160 | result.put("row", i); 161 | 162 | return result; 163 | } 164 | 165 | @RequestMapping(value = "/test/crud/deleteByPrimaryKey.json") 166 | @ResponseBody 167 | public Map test_delete() throws Exception { 168 | Map result = new HashMap(); 169 | 170 | int i = service.deleteByPrimaryKey(User.class, "15"); 171 | 172 | result.put("row", i); 173 | 174 | return result; 175 | } 176 | 177 | @RequestMapping(value = "/test/crud/deleteByCondition.json") 178 | @ResponseBody 179 | public Map test_deleteByCondition() throws Exception { 180 | Map result = new HashMap(); 181 | 182 | String condition = "dept_id != #{conditionParam.deptId}"; 183 | Map param = new HashMap(); 184 | param.put("deptId", 1); 185 | int i = service.deleteByCondition(User.class,condition,param); 186 | 187 | result.put("row", i); 188 | 189 | return result; 190 | } 191 | 192 | @RequestMapping(value = "/test/crud/selectAdvanced.json") 193 | @ResponseBody 194 | public List test_selectAdvanced() throws Exception { 195 | 196 | String conditionExp = "user_name = #{conditionParam.user_name}"; 197 | 198 | Map conditionParam = new HashMap(); 199 | conditionParam.put("user_name", "svili"); 200 | 201 | String orderExp = "user_id"; 202 | GeneralQueryParam queryParam = new GeneralQueryParam(); 203 | 204 | queryParam.setConditionExp(conditionExp); 205 | queryParam.setConditionParam(conditionParam); 206 | queryParam.setOrderExp(orderExp); 207 | queryParam.setPageSize(3); 208 | queryParam.setPageNo(2); 209 | 210 | List result = service.selectAdvanced(User.class, queryParam); 211 | 212 | return result; 213 | } 214 | 215 | } 216 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/GeneralDaoProvider.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud; 2 | 3 | import java.util.Map; 4 | import java.util.Map.Entry; 5 | 6 | import org.apache.ibatis.jdbc.SQL; 7 | 8 | public class GeneralDaoProvider { 9 | 10 | @Deprecated 11 | public String insertSelectiveSql( final Map param) { 12 | return new SQL() { 13 | { 14 | INSERT_INTO((String) param.get("tableName")); 15 | 16 | @SuppressWarnings("unchecked") 17 | Map mapping = (Map) param.get("columnValueMapping"); 18 | for (Entry entry : mapping.entrySet()) { 19 | if(entry.getValue() instanceof java.util.Date){ 20 | VALUES(entry.getKey(), "#{columnValueMapping." + entry.getKey() + ",jdbcType=TIMESTAMP}"); 21 | break; 22 | } 23 | VALUES(entry.getKey(), "#{columnValueMapping." + entry.getKey() + "}"); 24 | } 25 | /*for (String key : mapping.keySet()) { 26 | if("time".equals(key)){ 27 | VALUES(key, "#{columnValueMapping." + key + ",jdbcType=TIMESTAMP}"); 28 | break; 29 | } 30 | VALUES(key, "#{columnValueMapping." + key + "}"); 31 | }*/ 32 | 33 | } 34 | }.toString(); 35 | } 36 | 37 | @Deprecated 38 | public String updateByPrimaryKeySql(final Map param) { 39 | return new SQL() { 40 | { 41 | UPDATE((String) param.get("tableName")); 42 | 43 | @SuppressWarnings("unchecked") 44 | Map mapping = (Map) param.get("columnValueMapping"); 45 | for (String key : mapping.keySet()) { 46 | SET(key + "=" + "#{columnValueMapping." + key + "}"); 47 | } 48 | 49 | WHERE((String) param.get("primaryKey") + "=" + param.get("primaryValue")); 50 | 51 | } 52 | }.toString(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/GeneralQueryParam.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | public class GeneralQueryParam { 7 | 8 | /** 9 | * 表对应的pojo类 10 | */ 11 | private Class queryClazz; 12 | 13 | /** 14 | * 查询列 15 | */ 16 | private List queryColumn; 17 | 18 | /** 19 | * 查询条件 where 表达式 20 | *

不要写where

21 | *

传入的参数格式为:#{conditionParam.paramName}

22 | */ 23 | private String conditionExp; 24 | 25 | /** 26 | * 查询条件 where 表达式中的参数集 27 | *

key:paramName

28 | */ 29 | private Map conditionParam; 30 | 31 | /** 32 | * 每页的数据条数 33 | *

分页查询使用

34 | */ 35 | private Integer pageSize; 36 | 37 | /** 38 | * 页码 39 | *

分页查询使用

40 | */ 41 | private Integer pageNo; 42 | 43 | /** 44 | * order by 表达式 45 | *

格式:columnName asc|desc

46 | *

多个排序字段用逗号分隔

47 | */ 48 | private String orderExp; 49 | 50 | public GeneralQueryParam() { 51 | } 52 | 53 | public Class getQueryClazz() { 54 | return queryClazz; 55 | } 56 | 57 | public void setQueryClazz(Class queryClazz) { 58 | this.queryClazz = queryClazz; 59 | } 60 | 61 | public List getQueryColumn() { 62 | return queryColumn; 63 | } 64 | 65 | public void setQueryColumn(List queryColumn) { 66 | this.queryColumn = queryColumn; 67 | } 68 | 69 | public String getConditionExp() { 70 | return conditionExp; 71 | } 72 | 73 | public void setConditionExp(String conditionExp) { 74 | this.conditionExp = conditionExp; 75 | } 76 | 77 | public Map getConditionParam() { 78 | return conditionParam; 79 | } 80 | 81 | public void setConditionParam(Map conditionParam) { 82 | this.conditionParam = conditionParam; 83 | } 84 | 85 | public Integer getPageSize() { 86 | return pageSize; 87 | } 88 | 89 | public void setPageSize(Integer pageSize) { 90 | this.pageSize = pageSize; 91 | } 92 | 93 | public Integer getPageNo() { 94 | return pageNo; 95 | } 96 | 97 | public void setPageNo(Integer pageNo) { 98 | this.pageNo = pageNo; 99 | } 100 | 101 | public String getOrderExp() { 102 | return orderExp; 103 | } 104 | 105 | public void setOrderExp(String orderExp) { 106 | this.orderExp = orderExp; 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/SQLColumn.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud; 2 | 3 | /** 4 | * SQL字段对象
5 | * java对象与jdbc转换
6 | * Mybatis-insert update中Oracle.data类型对应的Java对象需要声明jdbcType=TIMESTAMP 7 | *

8 | * 使用SQLColumn工厂(@see SQLColumnFactory)创建对象 9 | * 10 | * @author svili 11 | * @data 2017年3月27日 12 | * 13 | */ 14 | public class SQLColumn { 15 | 16 | private String columnName; 17 | 18 | private Object columnValue; 19 | 20 | private String jdbcType; 21 | 22 | public String getColumnName() { 23 | return columnName; 24 | } 25 | 26 | public void setColumnName(String columnName) { 27 | this.columnName = columnName; 28 | } 29 | 30 | public Object getColumnValue() { 31 | return columnValue; 32 | } 33 | 34 | public void setColumnValue(Object columnValue) { 35 | this.columnValue = columnValue; 36 | } 37 | 38 | public String getJdbcType() { 39 | return jdbcType; 40 | } 41 | 42 | public void setJdbcType(String jdbcType) { 43 | this.jdbcType = jdbcType; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/common/DateUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.common; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | 7 | /** 8 | * 时间格式化工具类 9 | * 10 | * @author svili 11 | * @date 2016年11月17日 12 | * 13 | */ 14 | public class DateUtil { 15 | 16 | /** 17 | * 普通格式化:yyyy-MM-dd 18 | * 19 | */ 20 | public static String formatSimple() { 21 | return formatSimple(System.currentTimeMillis()); 22 | } 23 | 24 | /** 25 | * 普通格式化:yyyy-MM-dd 26 | * 27 | */ 28 | public static String formatSimple(Date date) { 29 | return formatSimple(date.getTime()); 30 | } 31 | 32 | /** 33 | * 普通格式化:yyyy-MM-dd 34 | * 35 | */ 36 | public static String formatSimple(long time) { 37 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 38 | return simpleDateFormat.format(time); 39 | } 40 | 41 | /** 42 | * 带时分秒:yyyy-MM-dd HH:mm:ss 43 | * 44 | */ 45 | public static String format() { 46 | return format(System.currentTimeMillis()); 47 | } 48 | 49 | /** 50 | * 带时分秒:yyyy-MM-dd HH:mm:ss 51 | * 52 | */ 53 | public static String format(Date date) { 54 | return format(date.getTime()); 55 | } 56 | 57 | /** 58 | * 带时分秒:yyyy-MM-dd HH:mm:ss 59 | * 60 | */ 61 | public static String format(long time) { 62 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 63 | return simpleDateFormat.format(time); 64 | } 65 | 66 | /** 67 | * 自定义 68 | * 69 | * @param exp 70 | * 表达式 71 | */ 72 | public static String format(String exp) { 73 | return format(System.currentTimeMillis(), exp); 74 | } 75 | 76 | /** 77 | * 自定义 78 | * 79 | * @param exp 80 | * 表达式 81 | */ 82 | public static String format(Date date, String exp) { 83 | return format(date.getTime(), exp); 84 | } 85 | 86 | /** 87 | * 自定义 88 | * 89 | * @param exp 90 | * 表达式 91 | */ 92 | public static String format(long time, String exp) { 93 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(exp); 94 | return simpleDateFormat.format(time); 95 | } 96 | 97 | /** 98 | * 日期 99 | */ 100 | public static int getDay() { 101 | return getDay(System.currentTimeMillis()); 102 | } 103 | 104 | public static int getDay(Date date) { 105 | return getDay(date.getTime()); 106 | } 107 | 108 | public static int getDay(long time) { 109 | Calendar cal = Calendar.getInstance(); 110 | cal.setTimeInMillis(time); 111 | return cal.get(Calendar.DATE); 112 | } 113 | 114 | /** 115 | * 月份 116 | */ 117 | public static int getMonth() { 118 | return getMonth(System.currentTimeMillis()); 119 | } 120 | 121 | public static int getMonth(Date date) { 122 | return getMonth(date.getTime()); 123 | } 124 | 125 | public static int getMonth(long time) { 126 | Calendar cal = Calendar.getInstance(); 127 | cal.setTimeInMillis(time); 128 | return cal.get(Calendar.MONTH) + 1; 129 | } 130 | 131 | /** 132 | * 年份 133 | */ 134 | public static int getYear() { 135 | return getYear(System.currentTimeMillis()); 136 | } 137 | 138 | public static int getYear(Date date) { 139 | return getYear(date.getTime()); 140 | } 141 | 142 | public static int getYear(long time) { 143 | Calendar cal = Calendar.getInstance(); 144 | cal.setTimeInMillis(time); 145 | return cal.get(Calendar.YEAR); 146 | } 147 | 148 | /** 149 | * sql.Date 150 | */ 151 | public static java.sql.Date toSQLDate(Date date) { 152 | return date instanceof java.sql.Date ? (java.sql.Date) date : toSQLDate(date.getTime()); 153 | } 154 | 155 | public static java.sql.Date toSQLDate(long time) { 156 | return new java.sql.Date(time); 157 | } 158 | 159 | public static java.sql.Date getSQLDate() { 160 | return toSQLDate(System.currentTimeMillis()); 161 | } 162 | 163 | /** 164 | * sql.Timestamp 165 | */ 166 | public static java.sql.Timestamp toTimestamp(Date date) { 167 | return date instanceof java.sql.Timestamp ? (java.sql.Timestamp) date : toTimestamp(date.getTime()); 168 | } 169 | 170 | public static java.sql.Timestamp toTimestamp(long time) { 171 | return new java.sql.Timestamp(time); 172 | } 173 | 174 | public static java.sql.Timestamp getTimestamp() { 175 | return toTimestamp(System.currentTimeMillis()); 176 | } 177 | 178 | } 179 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/common/NumberUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.common; 2 | 3 | import java.math.BigDecimal; 4 | 5 | /** 6 | * Number格式化工具类 7 | * 8 | * @author svili 9 | * @date 2017年2月18日 10 | * 11 | */ 12 | public class NumberUtil { 13 | 14 | public static byte toByte(Number number) { 15 | return number.byteValue(); 16 | } 17 | 18 | public static double toDouble(Number number) { 19 | return number.doubleValue(); 20 | } 21 | 22 | public static float toFloat(Number number) { 23 | return number.floatValue(); 24 | } 25 | 26 | public static int toInt(Number number) { 27 | return number.intValue(); 28 | } 29 | 30 | public static long toLong(Number number) { 31 | return number.longValue(); 32 | } 33 | 34 | public static short toShort(Number number) { 35 | return number.shortValue(); 36 | } 37 | 38 | public static BigDecimal toBigDecimal(Number number) { 39 | if (number instanceof BigDecimal) { 40 | return (BigDecimal) number; 41 | } else { 42 | return new BigDecimal(number.doubleValue()); 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/common/PersistentUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.common; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import javax.persistence.Column; 10 | import javax.persistence.Id; 11 | import javax.persistence.Table; 12 | import javax.persistence.Transient; 13 | 14 | import org.apache.ibatis.reflection.ReflectionException; 15 | 16 | import com.svili.portal.crud.utils.FieldReflectUtil; 17 | 18 | /** 19 | * 持久化工具类 20 | * 21 | * @author svili 22 | * 23 | */ 24 | public class PersistentUtil { 25 | 26 | /** 27 | * 驼峰_下划线转换,默认开启
28 | * 对@Table注解无效,支持ClassName_TableName
29 | * 对@Column注解无效,支持fieldName_columnName
30 | */ 31 | private static final boolean CAMEL_TO_UNDERLINE = true; 32 | 33 | /** 34 | * 获取Java对象对应的表名
35 | * 默认下划线风格 36 | * 37 | * @param clazz 38 | * pojo类class对象 39 | * @return tableName 40 | */ 41 | public static String getTableName(Class clazz) { 42 | // 判断是否有Table注解 43 | if (clazz.isAnnotationPresent(Table.class)) { 44 | // 获取注解对象 45 | Table table = clazz.getAnnotation(Table.class); 46 | // 设置了name属性 47 | if (!table.name().trim().equals("")) { 48 | return table.name(); 49 | } 50 | } 51 | // 类名 52 | String className = clazz.getSimpleName(); 53 | 54 | if (!CAMEL_TO_UNDERLINE) { 55 | return className; 56 | } else { 57 | // 驼峰转下划线 58 | return StringUtil.camelToUnderline(className); 59 | } 60 | } 61 | 62 | /** 63 | * 获取列名
64 | * 注解优先,javax.persistence.Column name属性值。
65 | * 无注解,将字段名转为字符串,默认下划线风格.
66 | * 67 | * @param field 68 | * pojo字段对象 69 | * @return 70 | */ 71 | public static String getColumnName(Field field) { 72 | if (field.isAnnotationPresent(javax.persistence.Column.class)) { 73 | // 获取注解对象 74 | Column column = field.getAnnotation(Column.class); 75 | // 设置了name属性 76 | if (!column.name().trim().equals("")) { 77 | return column.name(); 78 | } 79 | } 80 | if (!CAMEL_TO_UNDERLINE) { 81 | return field.getName(); 82 | } else { 83 | return StringUtil.camelToUnderline(field.getName()); 84 | } 85 | } 86 | 87 | /** 88 | * 获取pojo主键字段
89 | * javax.persistence.Id注解的字段不存在返回null 90 | * 91 | * @param clazz 92 | * pojo类-class对象 93 | * @return Field 94 | */ 95 | public static Field getPrimaryFieldNotCareNull(Class clazz) { 96 | Field field = FieldReflectUtil.findField(clazz, Id.class); 97 | if (field != null) { 98 | return field; 99 | } else { 100 | return null; 101 | } 102 | } 103 | 104 | /** 105 | * 获取pojo主键字段
106 | * 107 | * 主键必须存在,不存在抛异常 108 | * 109 | * @param clazz 110 | * pojo类-class对象 111 | * @return Field 112 | * @throws ReflectionExceptio 113 | * @see org.apache.ibatis.reflection.ReflectionException 114 | */ 115 | public static Field getPrimaryField(Class clazz) { 116 | Field field = getPrimaryFieldNotCareNull(clazz); 117 | if (field != null) { 118 | return field; 119 | } else { 120 | throw new ReflectionException( 121 | "no search result for javax.persistence.Id annotation from " + clazz.getName()); 122 | } 123 | } 124 | 125 | /** 126 | * 获取pojo主键列名
127 | * 128 | * @param clazz 129 | * pojo类-class对象 130 | * @return underline-columnName 131 | * @throws ReflectionExceptio 132 | * @see org.apache.ibatis.reflection.ReflectionException 133 | */ 134 | public static String getPrimaryKey(Class clazz) { 135 | Field primaryField = getPrimaryField(clazz); 136 | return getColumnName(primaryField); 137 | } 138 | 139 | /** 140 | * 根据列名获取字段
141 | * 无匹配的字段抛异常 142 | * 143 | * @param clazz 144 | * pojo类class对象 145 | * @param columnName 146 | * 列名 147 | * @return 148 | */ 149 | public static Field getFieldByColumnName(Class clazz, String columnName) { 150 | Map mapping = getColumnFieldMapping(clazz); 151 | Field field = mapping.get(clazz.getName() + "." + columnName); 152 | if (field == null) { 153 | throw new ReflectionException( 154 | "no search matched field to columnName :" + columnName + " from " + clazz.getName()); 155 | } 156 | return field; 157 | } 158 | 159 | /** 160 | * 列名-字段Mapping
161 | * key = clazz.getName() + "." + columnName
162 | * value = Field
163 | * 可以将此方法的返回结果存储到容器中
164 | * 165 | * @param clazz 166 | * pojo类class对象 167 | * @return 168 | */ 169 | public static Map getColumnFieldMapping(Class clazz) { 170 | List fieldList = getPersistentFields(clazz); 171 | if (fieldList == null || fieldList.size() == 0) { 172 | return null; 173 | } 174 | Map mapping = new HashMap(); 175 | String className = clazz.getName(); 176 | for (Field field : fieldList) { 177 | mapping.put(className + "." + getColumnName(field), field); 178 | } 179 | return mapping; 180 | } 181 | 182 | /** 183 | * 获取持久化字段
184 | * 可以将此方法的返回结果存储到容器中
185 | * 186 | * @param clazz 187 | * class对象 188 | * @return 189 | */ 190 | public static List getPersistentFields(Class clazz) { 191 | List list = new ArrayList(); 192 | Class searchType = clazz; 193 | while (!Object.class.equals(searchType) && searchType != null) { 194 | Field[] fields = searchType.getDeclaredFields(); 195 | for (Field field : fields) { 196 | if (isPersistentField(field)) { 197 | list.add(field); 198 | } 199 | } 200 | searchType = searchType.getSuperclass(); 201 | } 202 | return list; 203 | } 204 | 205 | /** 206 | * 是否为持久化字段
207 | * javax.persistence.Transient注解为非持久化字段
208 | * 209 | * @param field 210 | * Field对象 211 | * @return 212 | */ 213 | public static boolean isPersistentField(Field field) { 214 | return !field.isAnnotationPresent(Transient.class); 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/common/StringUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.common; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * 字符串工具类 7 | * 8 | * @author svili 9 | * @date 2016年11月8日 10 | * 11 | */ 12 | public class StringUtil { 13 | 14 | /** 15 | * 将驼峰标识转换为下划线 16 | * 17 | * @param text 18 | * @return camel 19 | */ 20 | public static String camelToUnderline(String text) { 21 | if (text == null || "".equals(text.trim())) { 22 | return ""; 23 | } 24 | StringBuffer result = new StringBuffer(text.length() + 1); 25 | result.append(text.substring(0, 1)); 26 | for (int i = 1; i < text.length(); i++) { 27 | if (!Character.isLowerCase(text.charAt(i))) { 28 | result.append('_'); 29 | } 30 | result.append(text.substring(i, i + 1)); 31 | } 32 | return result.toString().toLowerCase(); 33 | } 34 | 35 | /** 36 | * 将下划线标识转换为驼峰 37 | * 38 | * @param text 39 | * @return underline 40 | */ 41 | public static String underlineToCamel(String text) { 42 | if (text == null || "".equals(text.trim())) { 43 | return ""; 44 | } 45 | int length = text.length(); 46 | StringBuffer result = new StringBuffer(); 47 | for (int i = 0; i < length; i++) { 48 | char c = text.charAt(i); 49 | if (c == '_') { 50 | if (++i < length) { 51 | result.append(Character.toUpperCase(text.charAt(i))); 52 | } 53 | } else { 54 | result.append(c); 55 | } 56 | } 57 | return result.toString(); 58 | } 59 | 60 | /** 61 | * 将String list字符串集合用逗号","分割 62 | * 63 | * @param list 64 | * 集合 65 | * @return string 66 | */ 67 | public static String splitListByComma(List list) { 68 | StringBuffer result = new StringBuffer(); 69 | for (String str : list) { 70 | result.append(",").append(str); 71 | } 72 | return result.substring(1, result.length()); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/common/TypeCastUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.common; 2 | 3 | public class TypeCastUtil { 4 | 5 | public static Number castToNumber(Object value) throws Exception { 6 | if (value == null) { 7 | return null; 8 | } 9 | if (TypeIdentifyUtil.isNumberType(value)) { 10 | return (Number) value; 11 | } 12 | if (value instanceof Boolean || value.getClass().equals(Boolean.TYPE)) { 13 | return Boolean.valueOf((boolean) value) ? 1 : 0; 14 | } 15 | if (value instanceof String) { 16 | // 未完成 17 | } 18 | throw new Exception(value + " : value is not Number type or Boolean type, can not castToNumber"); 19 | } 20 | 21 | public static Boolean castToBoolean(Object value) throws Exception { 22 | if (value == null) { 23 | return null; 24 | } 25 | if (TypeIdentifyUtil.isBooleanType(value)) { 26 | return (Boolean) value; 27 | } 28 | if (value instanceof Number) { 29 | return ((Number) value).intValue() == 1 ? true : false; 30 | } 31 | if (value instanceof String) { 32 | return Boolean.parseBoolean((String) value); 33 | } 34 | 35 | throw new Exception(value + " : value is not Number type or Boolean type, can not castToBoolean"); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/common/TypeIdentifyUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.common; 2 | 3 | /** 4 | * 类型识别工具类 5 | * 6 | * @author svili 7 | * @data 2017年3月30日 8 | * 9 | */ 10 | public class TypeIdentifyUtil { 11 | 12 | /** 13 | * 布尔类型
14 | * null对象返回false 15 | * 16 | * @param obj 17 | * 待识别的对象 18 | * @return 19 | */ 20 | public static boolean isBooleanType(Object obj) { 21 | if (obj == null) { 22 | return false; 23 | } 24 | 25 | if (obj instanceof Boolean || obj.getClass().equals(Boolean.TYPE)) { 26 | return true; 27 | } else { 28 | return false; 29 | } 30 | } 31 | 32 | /** 33 | * 数值类型
34 | * null对象返回false 35 | * 36 | * @param obj 37 | * 待识别的对象 38 | * @return 39 | */ 40 | public static boolean isNumberType(Object obj) { 41 | 42 | if (obj == null) { 43 | return false; 44 | } 45 | 46 | if (obj instanceof Number) { 47 | return true; 48 | } 49 | 50 | if (obj.getClass().equals(Byte.TYPE)) { 51 | return true; 52 | } 53 | 54 | if (obj.getClass().equals(Short.TYPE)) { 55 | return true; 56 | } 57 | 58 | if (obj.getClass().equals(Integer.TYPE)) { 59 | return true; 60 | } 61 | 62 | if (obj.getClass().equals(Long.TYPE)) { 63 | return true; 64 | } 65 | 66 | if (obj.getClass().equals(Float.TYPE)) { 67 | return true; 68 | } 69 | 70 | if (obj.getClass().equals(Double.TYPE)) { 71 | return true; 72 | } 73 | 74 | return false; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/utils/DateFieldReflectUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.utils; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | import org.apache.ibatis.reflection.ReflectionException; 6 | import org.springframework.util.ReflectionUtils; 7 | 8 | import com.svili.portal.crud.common.DateUtil; 9 | 10 | /** 11 | * 时间字段处理 12 | * 13 | * @author svili 14 | * @date 2017年2月18日 15 | * 16 | */ 17 | public class DateFieldReflectUtil { 18 | 19 | /** 20 | * 设置Date类型字段值 21 | * 22 | * @param target 23 | * the target object from which to get the field 24 | * @param field 25 | * the field to set 26 | * @param value 27 | * java.util.Date 28 | * @throws Exception 29 | * IllegalArgumentException, IllegalAccess 30 | */ 31 | public static void setFieldDateValue(Object target, Field field, Object value) throws Exception { 32 | 33 | if (!java.util.Date.class.isAssignableFrom(field.getType())) { 34 | throw new ReflectionException(target.getClass().getName() + "." + field.getName() 35 | + " : field type is not Date, can not convertToDate"); 36 | } 37 | 38 | if (!field.isAccessible()) { 39 | ReflectionUtils.makeAccessible(field); 40 | } 41 | 42 | if (value == null) { 43 | field.set(target, null); 44 | return; 45 | } 46 | 47 | if (!(value instanceof java.util.Date)) { 48 | throw new ReflectionException(value + " : is not Date type value , can not convertToDate to field " 49 | + target.getClass().getName() + "." + field.getName()); 50 | } 51 | 52 | if (field.getType().equals(java.sql.Date.class)) { 53 | field.set(target, DateUtil.toSQLDate((java.util.Date) value)); 54 | return; 55 | } 56 | 57 | if (field.getType().equals(java.sql.Timestamp.class)) { 58 | field.set(target, DateUtil.toTimestamp((java.util.Date) value)); 59 | return; 60 | } 61 | //其他Date类型 未完成 62 | 63 | field.set(target, value); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/utils/EnumFieldReflectUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.utils; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | import org.apache.ibatis.reflection.ReflectionException; 6 | import org.springframework.util.ReflectionUtils; 7 | 8 | /** 9 | * 枚举类型字段处理 10 | * 11 | * @author svili 12 | * @date 2016年11月10日 13 | * 14 | */ 15 | public class EnumFieldReflectUtil { 16 | 17 | /** 18 | * 获取枚举类型的字段值-ordinal 19 | * 20 | * @param target 21 | * the target object from which to get the field 22 | * @param field 23 | * the field to get 24 | * @return enum.ordinal 25 | * @throws Exception 26 | * IllegalArgumentException, IllegalAccess 27 | */ 28 | @SuppressWarnings("rawtypes") 29 | public static int getFieldEnumOrdinal(Object target, Field field) throws Exception { 30 | 31 | if (!field.getType().isEnum()) { 32 | throw new ReflectionException(target.getClass().getName() + "." + field.getName() 33 | + ":field type is not Enum, can not convertToEnum"); 34 | } 35 | 36 | if (!field.isAccessible()) { 37 | ReflectionUtils.makeAccessible(field); 38 | } 39 | 40 | return ((Enum) field.get(target)).ordinal(); 41 | } 42 | 43 | /** 44 | * 45 | * 设置枚举类型的字段值 46 | * 47 | * @param target 48 | * the target object from which to get the field 49 | * @param field 50 | * the field to set 51 | * @param ordinal 52 | * enum.ordinal 53 | * @throws Exception 54 | * IllegalArgumentException, IllegalAccess 55 | */ 56 | @SuppressWarnings("rawtypes") 57 | public static void setFieldEnumValueByOrdinal(Object target, Field field, Integer ordinal) throws Exception { 58 | if (!field.getType().isEnum()) { 59 | throw new ReflectionException(target.getClass().getName() + "." + field.getName() 60 | + " : field type is not Enum, can not convertToEnum"); 61 | } 62 | 63 | if (!field.isAccessible()) { 64 | ReflectionUtils.makeAccessible(field); 65 | } 66 | 67 | if(ordinal == null){ 68 | field.set(target, null); 69 | return; 70 | } 71 | 72 | Enum[] enumObjs = (Enum[]) (field.getType()).getEnumConstants(); 73 | for (Enum enumObj : enumObjs) { 74 | if (enumObj.ordinal() == ordinal) { 75 | field.set(target, enumObj); 76 | } 77 | } 78 | 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/utils/FieldReflectUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.utils; 2 | 3 | import java.lang.annotation.Annotation; 4 | import java.lang.reflect.Field; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import org.springframework.util.ReflectionUtils; 9 | 10 | import com.svili.portal.crud.common.NumberUtil; 11 | import com.svili.portal.crud.common.TypeCastUtil; 12 | 13 | /** 14 | * Field反射工具类
15 | * 16 | * @author svili 17 | * @date 2016年11月17日 18 | * 19 | */ 20 | public class FieldReflectUtil { 21 | 22 | /** 23 | * 设定目标对象指定的字段值
24 | * oracle Number类型返回的是BigDecimal
25 | * mysql tinyint(1)返回的是Boolean类型 26 | * 27 | * @param t 28 | * 对象 29 | * @param field 30 | * 字段 31 | * @throws Exception 32 | * IllegalArgumentException, IllegalAccessException 33 | */ 34 | public static void setFieldValue(T t, Field field, Object value) throws Exception { 35 | ReflectionUtils.makeAccessible(field); 36 | 37 | //空值处理 38 | if(value == null){ 39 | field.set(t, null); 40 | return; 41 | } 42 | 43 | //基本数据类型 未完成 44 | if(field.getType().isPrimitive()){ 45 | 46 | } 47 | 48 | // Enum类型字段处理 49 | if (field.getType().isEnum()) { 50 | //Enum类型的字段在数据库中存储其ordinal 51 | Number number = TypeCastUtil.castToNumber(value); 52 | int ordinal = NumberUtil.toInt(number); 53 | EnumFieldReflectUtil.setFieldEnumValueByOrdinal(t, field, ordinal); 54 | return; 55 | } 56 | 57 | // Boolean类型字段处理 58 | if (field.getType().equals(Boolean.class)) { 59 | boolean b = TypeCastUtil.castToBoolean(value); 60 | field.set(t, b); 61 | return; 62 | } 63 | 64 | // Number类型字段处理 65 | if (Number.class.isAssignableFrom(field.getType())) { 66 | // oracle中Number类型返回的是BigDecimal 67 | NumberFieldReflectUtil.setFieldNumberValue(t, field, (Number) value); 68 | return; 69 | } 70 | 71 | // Date类型字段处理 72 | if (java.util.Date.class.isAssignableFrom(field.getType())) { 73 | DateFieldReflectUtil.setFieldDateValue(t, field, value); 74 | return; 75 | } 76 | field.set(t, value); 77 | } 78 | 79 | /** 80 | * 获取目标对象指定的字段值 81 | *

82 | * 空值返回null 83 | *

84 | * 85 | * @param t 86 | * 对象 87 | * @param field 88 | * 字段 89 | * @return value 90 | * @throws Exception 91 | * IllegalArgumentException, IllegalAccessException 92 | */ 93 | public static Object getFieldValue(T t, Field field) throws Exception { 94 | ReflectionUtils.makeAccessible(field); 95 | if (field.get(t) == null) { 96 | return null; 97 | } 98 | // Enum类型字段处理 99 | if (field.getType().isEnum()) { 100 | return EnumFieldReflectUtil.getFieldEnumOrdinal(t, field); 101 | } 102 | return field.get(t); 103 | } 104 | 105 | /** 106 | * 获取class类中指定注解类型的field对象 107 | * 108 | * @param clazz 109 | * pojo类-class对象 110 | * @param annotationType 111 | * 注解类-class对象 112 | * @return Field or null 113 | */ 114 | public static Field findField(Class clazz, Class annotationType) { 115 | Class searchType = clazz; 116 | while (!Object.class.equals(searchType) && searchType != null) { 117 | Field[] fields = searchType.getDeclaredFields(); 118 | for (Field field : fields) { 119 | if (field.isAnnotationPresent(annotationType)) { 120 | return field; 121 | } 122 | } 123 | searchType = searchType.getSuperclass(); 124 | } 125 | return null; 126 | } 127 | 128 | /** 129 | * 获取所有字段
130 | * 暂时无用 131 | * 132 | * @param clazz 133 | * class对象 134 | * @return 135 | */ 136 | @Deprecated 137 | public static List getAllField(Class clazz) { 138 | List list = new ArrayList(); 139 | Class searchType = clazz; 140 | while (!Object.class.equals(searchType) && searchType != null) { 141 | Field[] fields = searchType.getDeclaredFields(); 142 | for (Field field : fields) { 143 | list.add(field); 144 | } 145 | searchType = searchType.getSuperclass(); 146 | } 147 | return list; 148 | } 149 | 150 | } 151 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/utils/GeneralMapperReflectUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.utils; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.ArrayList; 5 | import java.util.LinkedHashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Map.Entry; 9 | 10 | import com.svili.portal.crud.SQLColumn; 11 | import com.svili.portal.crud.common.PersistentUtil; 12 | 13 | 14 | /** 15 | * 映射工具类 16 | * 17 | * @author svili 18 | * @date 2016年11月8日 19 | * 20 | */ 21 | public class GeneralMapperReflectUtil { 22 | 23 | /** 24 | * 获取主键值 25 | * @param t pojo对象 26 | * @return 27 | */ 28 | public static Object getPrimaryValue(T t) throws Exception{ 29 | return FieldReflectUtil.getFieldValue(t, PersistentUtil.getPrimaryField(t.getClass())); 30 | } 31 | 32 | /** 33 | * 获取pojo对应table 的所有列名
34 | * 35 | * @param clazz 36 | * pojo类-class对象 37 | * @return columnNames 38 | */ 39 | public static List getAllColumnNames(Class clazz) { 40 | List columnNames = new ArrayList(); 41 | List fields = PersistentUtil.getPersistentFields(clazz); 42 | for (Field field : fields) { 43 | columnNames.add(PersistentUtil.getColumnName(field)); 44 | } 45 | return columnNames; 46 | } 47 | 48 | /** 49 | * 获取pojo对应table除主键外的所有列名
50 | * 51 | * @param clazz 52 | * pojo类-class对象 53 | * @return columnNames 54 | */ 55 | public static List getColumnNamesExceptPrimaryKey(Class clazz) { 56 | List columnNames = new ArrayList(); 57 | 58 | Field primaryField = PersistentUtil.getPrimaryFieldNotCareNull(clazz); 59 | 60 | List fields = PersistentUtil.getPersistentFields(clazz); 61 | 62 | for (Field field : fields) { 63 | if (primaryField != null && !field.equals(primaryField)) { 64 | columnNames.add(PersistentUtil.getColumnName(field)); 65 | } 66 | } 67 | return columnNames; 68 | } 69 | 70 | /** 71 | * 获取pojo除空值外的所有列名-字段值mapping 72 | * 73 | * @param 74 | * pojo类 75 | * 76 | * @param t 77 | * pojo对象 78 | * @return columnName-value 79 | * @throws Exception 80 | */ 81 | public static Map getColumnValueMappingExceptNull(T t) throws Exception { 82 | Map mapping = new LinkedHashMap(); 83 | 84 | List fields = PersistentUtil.getPersistentFields(t.getClass()); 85 | 86 | for (Field field : fields) { 87 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field); 88 | if (fieldValue != null) { 89 | mapping.put(PersistentUtil.getColumnName(field), fieldValue); 90 | } 91 | } 92 | 93 | return mapping; 94 | } 95 | 96 | public static List getSQLColumnsExceptNull(T t) throws Exception{ 97 | List sqlColumns = new ArrayList(); 98 | List fields = PersistentUtil.getPersistentFields(t.getClass()); 99 | 100 | for (Field field : fields) { 101 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field); 102 | if (fieldValue != null) { 103 | sqlColumns.add(SQLColumnFactory.createSQLColumn(t, field)); 104 | } 105 | } 106 | return sqlColumns; 107 | } 108 | 109 | public static List getAllSQLColumns(T t) throws Exception { 110 | List sqlColumns = new ArrayList(); 111 | 112 | List fields = PersistentUtil.getPersistentFields(t.getClass()); 113 | 114 | for (Field field : fields) { 115 | sqlColumns.add(SQLColumnFactory.createSQLColumn(t, field)); 116 | } 117 | return sqlColumns; 118 | } 119 | 120 | public static List getSQLColumns(T t, boolean isContainsPrimaryKey, 121 | boolean isContainsNullValue) throws Exception { 122 | List sqlColumns = new ArrayList(); 123 | 124 | List fields = PersistentUtil.getPersistentFields(t.getClass()); 125 | 126 | // 主键字段 127 | Field primaryField = PersistentUtil.getPrimaryField(t.getClass()); 128 | 129 | if (!isContainsPrimaryKey && primaryField != null) { 130 | // 不包含主键字段,移除 131 | fields.remove(primaryField); 132 | } 133 | 134 | for (Field field : fields) { 135 | // 字段值 136 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field); 137 | 138 | if (fieldValue == null) { 139 | // 空值 140 | if (isContainsNullValue) { 141 | // 包含空值 添加至结果集 142 | sqlColumns.add(SQLColumnFactory.createSQLColumn(t, field)); 143 | } 144 | 145 | } else { 146 | // 非空值 直接添加至结果集 147 | sqlColumns.add(SQLColumnFactory.createSQLColumn(t, field)); 148 | } 149 | 150 | } 151 | 152 | return sqlColumns; 153 | } 154 | 155 | 156 | /** 157 | * 获取pojo对应table的所有列名-字段值mapping
158 | * 159 | * @param 160 | * pojo类 161 | * 162 | * @param t 163 | * pojo对象 164 | * @return columnName-value 165 | * @throws Exception 166 | * IllegalArgumentException, IllegalAccessException 167 | * @see FieldReflectUtil.getFieldValue 168 | */ 169 | public static Map getAllColumnValueMapping(T t) throws Exception { 170 | Map mapping = new LinkedHashMap(); 171 | 172 | List fields = PersistentUtil.getPersistentFields(t.getClass()); 173 | 174 | for (Field field : fields) { 175 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field); 176 | mapping.put(PersistentUtil.getColumnName(field), fieldValue); 177 | } 178 | return mapping; 179 | } 180 | 181 | /** 182 | * 获取pojo对应table的 列名-字段值mapping
183 | * 184 | * @param 185 | * pojo类 186 | * 187 | * @param t 188 | * pojo对象 189 | * @param isContainsPrimaryKey 190 | * 是否包含主键 191 | * @param isContainsNullValue 192 | * 是否包含空值 193 | * @return columnName-value 194 | * @throws Exception 195 | */ 196 | public static Map getColumnValueMapping(T t, boolean isContainsPrimaryKey, 197 | boolean isContainsNullValue) throws Exception { 198 | Map mapping = new LinkedHashMap(); 199 | 200 | List fields = PersistentUtil.getPersistentFields(t.getClass()); 201 | 202 | // 主键字段 203 | Field primaryField = PersistentUtil.getPrimaryField(t.getClass()); 204 | 205 | if (!isContainsPrimaryKey && primaryField != null) { 206 | // 不包含主键字段,移除 207 | fields.remove(primaryField); 208 | } 209 | 210 | for (Field field : fields) { 211 | // 字段值 212 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field); 213 | 214 | if (fieldValue == null) { 215 | // 空值 216 | if (isContainsNullValue) { 217 | // 包含空值 添加至结果集 218 | mapping.put(PersistentUtil.getColumnName(field), fieldValue); 219 | } 220 | 221 | } else { 222 | // 非空值 直接添加至结果集 223 | mapping.put(PersistentUtil.getColumnName(field), fieldValue); 224 | } 225 | 226 | } 227 | 228 | return mapping; 229 | } 230 | 231 | /** 232 | * 列名-值 键值对转为pojo
233 | * 若mapping为空,则返回null对象
234 | * 235 | * @param mapping 236 | * columnName-value mapping 237 | * @param clazz 238 | * 待转换的pojo类-class对象 239 | * @throws Exception 240 | * InstantiationException, IllegalAccessException 241 | */ 242 | public static T parseToBean(Map mapping, Class clazz) throws Exception { 243 | if (mapping == null || mapping.size() == 0) { 244 | return null; 245 | } 246 | 247 | T t = clazz.newInstance(); 248 | for (Entry entry : mapping.entrySet()) { 249 | 250 | // oracle返回的columnName为大写 251 | String columnName = entry.getKey().toLowerCase(); 252 | 253 | Field field = PersistentUtil.getFieldByColumnName(clazz, columnName); 254 | 255 | if (field != null) { 256 | FieldReflectUtil.setFieldValue(t, field, entry.getValue()); 257 | } 258 | } 259 | return t; 260 | } 261 | 262 | } 263 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/utils/NumberFieldReflectUtil.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.utils; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | import org.apache.ibatis.reflection.ReflectionException; 6 | import org.springframework.util.ReflectionUtils; 7 | 8 | import com.svili.portal.crud.common.NumberUtil; 9 | 10 | /** 11 | * 数值字段处理 12 | * 13 | * @author svili 14 | * @date 2017年2月18日 15 | * 16 | */ 17 | public class NumberFieldReflectUtil { 18 | 19 | /** 20 | * 设置Date类型字段值 21 | * 22 | * @param target 23 | * the target object from which to get the field 24 | * @param field 25 | * the field to set 26 | * @param number 27 | * java.lang.Number 28 | * @throws Exception 29 | * IllegalArgumentException, IllegalAccess 30 | */ 31 | public static void setFieldNumberValue(Object target, Field field, Object value) throws Exception { 32 | 33 | if (!java.lang.Number.class.isAssignableFrom(field.getType())) { 34 | throw new ReflectionException(target.getClass().getName() + "." + field.getName() 35 | + " : field type is not Number, can not convertToNumber"); 36 | } 37 | 38 | if (!field.isAccessible()) { 39 | ReflectionUtils.makeAccessible(field); 40 | } 41 | 42 | if(value == null){ 43 | field.set(target, null); 44 | return; 45 | } 46 | 47 | if(!(value instanceof Number)){ 48 | throw new ReflectionException(value + " : is not Number type value , can not convertToNumber to field " 49 | + target.getClass().getName() + "." + field.getName()); 50 | } 51 | 52 | Number number = (Number)value; 53 | 54 | if (field.getType().equals(java.lang.Byte.class)) { 55 | field.set(target, NumberUtil.toByte(number)); 56 | return; 57 | } 58 | if (field.getType().equals(java.lang.Double.class)) { 59 | field.set(target, NumberUtil.toDouble(number)); 60 | return; 61 | } 62 | if (field.getType().equals(java.lang.Float.class)) { 63 | field.set(target, NumberUtil.toFloat(number)); 64 | return; 65 | } 66 | if (field.getType().equals(java.lang.Integer.class)) { 67 | field.set(target, NumberUtil.toInt(number)); 68 | return; 69 | } 70 | if (field.getType().equals(java.lang.Long.class)) { 71 | field.set(target, NumberUtil.toLong(number)); 72 | return; 73 | } 74 | if (field.getType().equals(java.lang.Short.class)) { 75 | field.set(target, NumberUtil.toShort(number)); 76 | return; 77 | } 78 | if (field.getType().equals(java.math.BigDecimal.class)) { 79 | field.set(target, NumberUtil.toBigDecimal(number)); 80 | return; 81 | } 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/crud/utils/SQLColumnFactory.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.crud.utils; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | import com.svili.portal.crud.SQLColumn; 6 | import com.svili.portal.crud.common.PersistentUtil; 7 | 8 | /** 9 | * 工厂方法
10 | * 未完成 11 | * @author svili 12 | * @data 2017年3月30日 13 | * 14 | */ 15 | public class SQLColumnFactory { 16 | 17 | public static SQLColumn createSQLColumn(T t, Field field) throws Exception { 18 | String columnName = PersistentUtil.getColumnName(field); 19 | Object columnValue = FieldReflectUtil.getFieldValue(t, field); 20 | Class fieldType = field.getType(); 21 | return createSQLColumn(columnName, columnValue, matchJdbcType(fieldType)); 22 | } 23 | 24 | public static SQLColumn createSQLColumn(String columnName, Object columnValue, String jdbcType) { 25 | SQLColumn column = new SQLColumn(); 26 | column.setColumnName(columnName); 27 | column.setColumnValue(columnValue); 28 | column.setJdbcType(jdbcType); 29 | return column; 30 | } 31 | 32 | public static String matchJdbcType(Class fieldType) { 33 | if(fieldType.isEnum()){ 34 | //枚举类型存ordinal 35 | return "Integer"; 36 | } 37 | if (String.class.equals(fieldType)) { 38 | //CLOB未完成 39 | return "VARCHAR"; 40 | } 41 | if (Integer.class.equals(fieldType) || Integer.TYPE.equals(fieldType)) { 42 | return "INTEGER"; 43 | } 44 | if (Double.class.equals(fieldType) || Double.TYPE.equals(fieldType)) { 45 | return "DOUBLE"; 46 | } 47 | if (Float.class.equals(fieldType) || Float.TYPE.equals(fieldType)) { 48 | return "FLOAT"; 49 | } 50 | if (java.util.Date.class.isAssignableFrom(fieldType)) { 51 | return "TIMESTAMP"; 52 | } 53 | //CLOB BLOB未完成 54 | return null; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/dao/GeneralDao.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.dao; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.apache.ibatis.annotations.InsertProvider; 7 | import org.springframework.stereotype.Repository; 8 | 9 | import com.svili.portal.crud.GeneralDaoProvider; 10 | 11 | /** 12 | * crud通用dao 13 | * @author svili 14 | * @date 2016年11月11日 15 | * 16 | */ 17 | @Repository("generalDao") 18 | public interface GeneralDao { 19 | 20 | Map selectByPrimaryKey(Map param); 21 | 22 | int deleteByPrimaryKey(Map param); 23 | 24 | int deleteByCondition(Map param); 25 | 26 | int insert(Map param); 27 | 28 | @InsertProvider(type=GeneralDaoProvider.class,method="insertSelectiveSql") 29 | int insertSelective(Map param); 30 | 31 | int insertBatch(Map param); 32 | 33 | int updateByPrimaryKey(Map param); 34 | 35 | int updateByConditionSelective(Map param); 36 | 37 | List> selectAdvanced(Map param); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/pojo/User.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.pojo; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Id; 5 | import javax.persistence.Table; 6 | import javax.persistence.Transient; 7 | 8 | import org.apache.ibatis.type.Alias; 9 | 10 | import com.svili.portal.type.DataState; 11 | 12 | /** 13 | * 用户信息 14 | *

15 | * @Table和Id是必须的 16 | *

17 | * 18 | * @author svili 19 | * @date 2016年8月10日 20 | * 21 | */ 22 | @Alias("User") 23 | @Table(name = "user") 24 | public class User { 25 | 26 | @Transient 27 | private static final long serialVersionUID = -7788405797990662048L; 28 | 29 | @Id 30 | private Integer userId; 31 | 32 | private Integer deptId; 33 | 34 | private String userName; 35 | 36 | private String loginName; 37 | 38 | @Column 39 | private String password; 40 | 41 | private String mobilePhone; 42 | 43 | private String officePhone; 44 | 45 | private String email; 46 | 47 | private String job; 48 | 49 | private Integer orderId; 50 | 51 | private DataState state; 52 | 53 | private java.util.Date createTime; 54 | 55 | private java.util.Date updateTime; 56 | 57 | public User() { 58 | } 59 | 60 | public Integer getUserId() { 61 | return userId; 62 | } 63 | 64 | public void setUserId(Integer userId) { 65 | this.userId = userId; 66 | } 67 | 68 | public Integer getDeptId() { 69 | return deptId; 70 | } 71 | 72 | public void setDeptId(Integer deptId) { 73 | this.deptId = deptId; 74 | } 75 | 76 | public String getUserName() { 77 | return userName; 78 | } 79 | 80 | public void setUserName(String userName) { 81 | this.userName = userName; 82 | } 83 | 84 | public String getLoginName() { 85 | return loginName; 86 | } 87 | 88 | public void setLoginName(String loginName) { 89 | this.loginName = loginName; 90 | } 91 | 92 | public String getPassword() { 93 | return password; 94 | } 95 | 96 | public void setPassword(String password) { 97 | this.password = password; 98 | } 99 | 100 | public String getMobilePhone() { 101 | return mobilePhone; 102 | } 103 | 104 | public void setMobilePhone(String mobilePhone) { 105 | this.mobilePhone = mobilePhone; 106 | } 107 | 108 | public String getOfficePhone() { 109 | return officePhone; 110 | } 111 | 112 | public void setOfficePhone(String officePhone) { 113 | this.officePhone = officePhone; 114 | } 115 | 116 | public String getEmail() { 117 | return email; 118 | } 119 | 120 | public void setEmail(String email) { 121 | this.email = email; 122 | } 123 | 124 | public String getJob() { 125 | return job; 126 | } 127 | 128 | public void setJob(String job) { 129 | this.job = job; 130 | } 131 | 132 | public Integer getOrderId() { 133 | return orderId; 134 | } 135 | 136 | public void setOrderId(Integer orderId) { 137 | this.orderId = orderId; 138 | } 139 | 140 | public DataState getState() { 141 | return state; 142 | } 143 | 144 | public void setState(DataState state) { 145 | this.state = state; 146 | } 147 | 148 | public java.util.Date getCreateTime() { 149 | return createTime; 150 | } 151 | 152 | public void setCreateTime(java.util.Date createTime) { 153 | this.createTime = createTime; 154 | } 155 | 156 | public java.util.Date getUpdateTime() { 157 | return updateTime; 158 | } 159 | 160 | public void setUpdateTime(java.util.Date updateTime) { 161 | this.updateTime = updateTime; 162 | } 163 | 164 | @Override 165 | public int hashCode() { 166 | final int prime = 31; 167 | int result = 1; 168 | result = prime * result + ((userId == null) ? 0 : userId.hashCode()); 169 | return result; 170 | } 171 | 172 | @Override 173 | public boolean equals(Object obj) { 174 | if (this == obj) 175 | return true; 176 | if (obj == null) 177 | return false; 178 | if (getClass() != obj.getClass()) 179 | return false; 180 | User other = (User) obj; 181 | if (userId == null) { 182 | if (other.userId != null) 183 | return false; 184 | } else if (!userId.equals(other.userId)) 185 | return false; 186 | return true; 187 | } 188 | 189 | @Override 190 | public String toString() { 191 | return "User [userId=" + userId + ", deptId=" + deptId + ", userName=" + userName + ", loginName=" + loginName 192 | + ", password=" + password + ", mobilePhone=" + mobilePhone + ", officePhone=" + officePhone 193 | + ", email=" + email + ", job=" + job + ", orderId=" + orderId + ", state=" + state + ", createTime=" 194 | + createTime + ", updateTime=" + updateTime + "]"; 195 | } 196 | 197 | } 198 | -------------------------------------------------------------------------------- /src/main/java/com/svili/portal/type/DataState.java: -------------------------------------------------------------------------------- 1 | package com.svili.portal.type; 2 | 3 | /** 4 | * 数据有效性 5 | * 6 | * @author lishiwei 7 | * @date 2016年8月26日 8 | * 9 | */ 10 | public enum DataState { 11 | 12 | /** 13 | * 无效 14 | */ 15 | UNEFFECT(0, "无效"), 16 | 17 | /** 18 | * 有效 19 | */ 20 | EFFECT(1, "有效"); 21 | 22 | 23 | private int code; 24 | 25 | private String description; 26 | 27 | private DataState(int code, String description) { 28 | this.code = code; 29 | this.description = description; 30 | } 31 | 32 | public int getCode() { 33 | return code; 34 | } 35 | 36 | public String getDescription() { 37 | return description; 38 | } 39 | 40 | public int toCode(){ 41 | return this.code; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/resources/configs/db.properties: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # MySQL 3 | #============================================================================ 4 | mysql.driver=com.mysql.jdbc.Driver 5 | mysql.url=jdbc:mysql://localhost:3306/portal 6 | mysql.username=username 7 | mysql.password=password 8 | 9 | #============================================================================ 10 | # 通用配置 11 | #============================================================================ 12 | #定义初始连接数 13 | initialSize=0 14 | #定义最大连接数 15 | maxActive=20 16 | #定义最大空闲 17 | maxIdle=20 18 | #定义最小空闲 19 | minIdle=1 20 | #定义最长等待时间 21 | maxWait=60000 -------------------------------------------------------------------------------- /src/main/resources/configs/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,console,file 2 | 3 | #定义日志输出目的地为控制台 4 | log4j.appender.console=org.apache.log4j.ConsoleAppender 5 | log4j.appender.console.Target=System.out 6 | #可以灵活地指定日志输出格式,下面一行是指定具体的格式 7 | log4j.appender.console.layout = org.apache.log4j.PatternLayout 8 | log4j.appender.console.layout.ConversionPattern=[%c] - %m%n 9 | 10 | #文件大小到达指定尺寸的时候产生一个新的文件 11 | log4j.appender.file = org.apache.log4j.RollingFileAppender 12 | #指定输出目录 ${myweb.root} 13 | log4j.appender.file.File = D:/logs/portals/log.log 14 | #定义文件最大大小 15 | log4j.appender.file.MaxFileSize = 10MB 16 | # 输出日志,如果换成DEBUG表示输出DEBUG以上级别日志 17 | log4j.appender.file.Threshold = DEBUG 18 | log4j.appender.file.layout = org.apache.log4j.PatternLayout 19 | log4j.appender.file.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n 20 | 21 | #log4j.logger.java.sql.Connection=DEBUG 22 | #log4j.logger.java.sql.PreparedStatement=DEBUG 23 | #log4j.logger.java.sql.ResultSet=DEBUG 24 | #log4j.logger.java.sql.Statement=DEBUG,console 25 | 26 | log4j.logger.druid.sql=warn,console 27 | log4j.logger.druid.sql.DataSource=warn,console 28 | log4j.logger.druid.sql.Connection=warn,console 29 | log4j.logger.druid.sql.Statement=warn,console 30 | log4j.logger.druid.sql.ResultSet=warn,console 31 | -------------------------------------------------------------------------------- /src/main/resources/configs/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 41 | 43 | -------------------------------------------------------------------------------- /src/main/resources/configs/spring-mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /src/main/resources/configs/spring-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | 25 | 26 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/main/resources/configs/spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 31 | 32 | 33 | 34 | classpath:configs/db.properties 35 | 36 | classpath:configs/redis.properties 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/main/resources/mapping/GeneralMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 15 | 16 | 17 | insert into ${tableName} 18 | 20 | ${item.columnName} 21 | 22 | values 23 | 25 | 26 | #{item.columnValue} 27 | 28 | 29 | #{item.columnValue,jdbcType=${item.jdbcType} } 30 | 31 | 32 | 33 | 34 | 35 | insert into ${tableName} 36 | 38 | ${columnName} 39 | 40 | values 41 | 43 | 45 | 46 | #{SQLColumn.columnValue} 47 | 48 | 49 | #{SQLColumn.columnValue,jdbcType=${SQLColumn.jdbcType} } 50 | 51 | 52 | 53 | 54 | 55 | 56 | 60 | 61 | 62 | 63 | 67 | 68 | 69 | 70 | update ${tableName} 71 | 72 | 74 | 75 | ${item.columnName} = 76 | #{item.columnValue } 77 | 78 | 79 | ${item.columnName} = 80 | #{item.columnValue,jdbcType=${item.jdbcType} } 81 | 82 | 83 | 84 | where ${primaryKey} = #{primaryValue} 85 | 86 | 87 | 88 | update ${tableName} 89 | 90 | 92 | ${columnName} = #{columnValue} 93 | 94 | 95 | 96 | 97 | ${conditionExp} 98 | 99 | 100 | 101 | 102 | 123 | 124 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/errorpage/400.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | 非法请求 8 | 9 | 10 |

非法请求,网页可能已经删除、更名或暂时不可用

11 | 12 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/errorpage/404.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | 页面不存在 8 | 9 | 10 |

您访问的页面不存在

11 | 12 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/errorpage/405.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | 没有访问权限 8 | 9 | 10 |

您没有权限访问此页面

11 | 12 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/errorpage/500.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | 系统错误 8 | 9 | 10 | <%=request.getAttribute("erro_message")%> 11 | 12 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | Archetype Created Web Application 7 | 8 | 9 | /index.jsp 10 | 11 | 12 | 13 | 14 | 15 | 400 16 | /WEB-INF/errorpage/400.jsp 17 | 18 | 19 | 20 | 404 21 | /WEB-INF/errorpage/404.jsp 22 | 23 | 24 | 25 | 405 26 | /WEB-INF/errorpage/405.jsp 27 | 28 | 29 | 30 | 500 31 | /WEB-INF/errorpage/500.jsp 32 | 33 | 34 | 404 35 | /axis2-web/Error/error404.jsp 36 | 37 | 38 | 500 39 | /axis2-web/Error/error500.jsp 40 | 41 | 42 | 43 | 44 | encodingFilter 45 | org.springframework.web.filter.CharacterEncodingFilter 46 | 47 | encoding 48 | UTF-8 49 | 50 | 51 | forceEncoding 52 | true 53 | 54 | 55 | 56 | 57 | 58 | encodingFilter 59 | /* 60 | 61 | 62 | 63 | 64 | 65 | 66 | dispatcher 67 | org.springframework.web.servlet.DispatcherServlet 68 | 69 | contextConfigLocation 70 | classpath:configs/spring-servlet.xml 71 | 72 | 73 | 1 74 | 75 | 76 | 77 | 78 | dispatcher 79 | 80 | *.do 81 | *.page 82 | *.json 83 | 84 | 85 | 86 | 87 | 88 | 89 | log4jConfigLocation 90 | classpath:configs/log4j.properties 91 | 92 | 93 | 94 | log4jRefreshInterval 95 | 6000 96 | 97 | 98 | org.springframework.web.util.Log4jConfigListener 99 | 100 | 101 | 102 | 103 | 104 | org.springframework.web.context.ContextLoaderListener 105 | 106 | 107 | 108 | 109 | org.springframework.web.util.IntrospectorCleanupListener 110 | 111 | 112 | 113 | 114 | 115 | contextConfigLocation 116 | classpath:configs/spring-mybatis.xml 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /test.txt: -------------------------------------------------------------------------------- 1 | 休息休息 --------------------------------------------------------------------------------