試着寫一寫一系列博客,主要是記錄從搭建框架,到集成一些相關的demo,以便之後在項目中用到的時候能夠快速拷貝代碼。java
計劃是一步一步的集成一些技術,好比,攔截器的使用,easypoi 的使用demo,Redis 的使用demo ,利用Redis作緩存,限流處理。rabbitmq 的使用demosql
文件上傳demo ,集成shiro 的demo,一些工具類的收集。等等想到什麼寫什麼緩存
項目結構:app
MybatisConfigurer 框架
@Configuration public class MybatisConfigurer { private final static String ENTITY_PACKAGE ="com.zh.demo.entity"; private final static String MAPPER_PACKAGE ="com.zh.demo.dao"; //Mapper插件基礎接口的徹底限定名 private final static String MAPPER_INTERFACE_REFERENCE ="com.zh.demo.core.Mapper"; @Bean public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setTypeAliasesPackage(ENTITY_PACKAGE); // 配置分頁插件,詳情請查閱官方文檔 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("pageSizeZero", "true");// 分頁尺寸爲0時查詢全部紀錄再也不執行分頁 properties.setProperty("reasonable", "true");// 頁碼<=0 查詢第一頁,頁碼>=總頁數查詢最後一頁 properties.setProperty("supportMethodsArguments", "true");// 支持經過 Mapper 接口參數來傳遞分頁參數 pageHelper.setProperties(properties); // 添加插件 factory.setPlugins(new Interceptor[] { pageHelper }); // 添加XML目錄 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); // https://blog.csdn.net/kkdelta/article/details/5507799 factory.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml")); return factory.getObject(); } @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); mapperScannerConfigurer.setBasePackage(MAPPER_PACKAGE); // 配置通用Mapper,詳情請查閱官方文檔 Properties properties = new Properties(); properties.setProperty("mappers", MAPPER_INTERFACE_REFERENCE); properties.setProperty("notEmpty", "false");// insert、update是否判斷字符串類型!='' 即 test="str != // null"表達式內是否追加 and str != '' properties.setProperty("IDENTITY", "MYSQL"); mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; } }
DateConverterConfig:全局handler前日期統一處理(特別是查詢的時候,日期控件傳回來的日期,須要轉換)ide
@Component public class DateConverterConfig implements Converter<String, Date> { private static final List<String> formarts = new ArrayList<>(4); static { formarts.add("yyyy-MM"); formarts.add("yyyy-MM-dd"); formarts.add("yyyy-MM-dd HH:mm"); formarts.add("yyyy-MM-dd HH:mm:ss"); } @Override public Date convert(String source) { String value = source.trim(); if ("".equals(value)) { return null; } if (source.matches("^\\d{4}-\\d{1,2}$")) { return parseDate(source, formarts.get(0)); } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) { return parseDate(source, formarts.get(1)); } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) { return parseDate(source, formarts.get(2)); } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) { return parseDate(source, formarts.get(3)); } else { throw new IllegalArgumentException("Invalid boolean value '" + source + "'"); } } /** * 格式化日期 * @param dateStr String 字符型日期 * @param format String 格式 * @return Date 日期 */ public Date parseDate(String dateStr, String format) { Date date = null; try { DateFormat dateFormat = new SimpleDateFormat(format); date = dateFormat.parse(dateStr); } catch (Exception e) { } return date; } }