补充和完善 DRUID配置和注解

master
星期八 6 years ago
parent 99ec5c2535
commit bd53f9ba9b
  1. 23
      pom.xml
  2. 3
      src/main/java/com/lq/api/vo/SysFileVo.java
  3. 18
      src/main/java/com/lq/api/web/SysFileController.java
  4. 4
      src/main/java/com/lq/cms/aspectJ/SysUserServiceAspectj.java
  5. 10
      src/main/java/com/lq/cms/vo/BasePageVo.java
  6. 5
      src/main/java/com/lq/code/interceptor/PageInteceptor.java
  7. 28
      src/main/java/com/lq/code/util/BeanUtil.java
  8. 148
      src/main/java/com/lq/code/util/CastUtil.java
  9. 31
      src/main/java/com/lq/code/util/Collectiontil.java
  10. 2
      src/main/java/com/lq/code/util/FileUtil.java
  11. 113
      src/main/java/com/lq/code/util/PropsUtil.java
  12. 13
      src/main/java/com/lq/code/util/jdbc/JdbcUtils.java
  13. 4
      src/main/java/com/lq/code/util/sql/AbstractDbBuiler.java
  14. 6
      src/main/java/com/lq/code/util/sql/MysqlBuilder.java
  15. 6
      src/main/java/com/lq/code/util/sql/OracleBuiler.java
  16. 19
      src/main/java/com/lq/code/util/sql/PageInterface.java
  17. 5
      src/main/java/com/lq/code/util/sql/SqlUtil.java
  18. 9
      src/main/resources/dev/jdbc.properties
  19. 3
      src/main/resources/spring-mvc-config.xml
  20. 35
      src/main/resources/spring-mybatis-config.xml

@ -160,6 +160,18 @@
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 处理事务和AOP所需的包 (低于1.7用(@PointCut)切点注解会报错) -->
<dependency>
<groupId>org.aspectj</groupId>
@ -172,17 +184,7 @@
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
@ -214,6 +216,7 @@
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<!--
<dependency>

@ -2,5 +2,8 @@ package com.lq.api.vo;
public class SysFileVo {
private String url;
}

@ -1,8 +1,10 @@
package com.lq.api.web;
import com.lq.api.service.SysFileService;
import com.lq.api.vo.SysFileVo;
import com.lq.cms.emun.StatusTypeEnum;
import com.lq.code.entity.AjaxResult;
import com.lq.code.util.BeanUtil;
import com.lq.code.util.FileUtil;
import com.lq.entity.SysFile;
import org.slf4j.LoggerFactory;
@ -14,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
@ -37,7 +40,7 @@ public class SysFileController {
@RequestMapping("/upload")
@ResponseBody
public Object upload(@RequestParam("files")MultipartFile[] files) throws IOException {
public Object upload(@RequestParam("files")MultipartFile[] files, HttpServletRequest request) throws IOException {
AjaxResult ajaxResult = new AjaxResult();
ajaxResult.setMsg("上传成功");
Date nowTime = new Date();
@ -65,6 +68,19 @@ public class SysFileController {
}
ajaxResult.setData(sysFileList);
return ajaxResult;
}
@RequestMapping("/findAll")
@ResponseBody
public Object findAll(){
AjaxResult ajaxResult = new AjaxResult();
List<SysFile> list = sysFileService.findAll();
ajaxResult.setData(list);
ajaxResult.setMsg("");
ajaxResult.setSuccess(true);
return ajaxResult;
}

@ -11,7 +11,7 @@ import org.springframework.stereotype.Component;
@Component
public class SysUserServiceAspectj {
@Pointcut("execution(* com.lq.cms.service.SysUserService.count(..))")
@Pointcut("execution(* com.lq.cms.service.SysUserService.findByLoginNameAndPassword(..))")
public void perFormEncord(){}
@ -34,7 +34,7 @@ public class SysUserServiceAspectj {
//后置返回通知
@AfterReturning(value = "perFormEncord()",returning = "retVal")
public void afterReturning(JoinPoint joinPoint,Object retVal){
// System.out.println(retVal);
}

@ -1,19 +1,27 @@
package com.lq.cms.vo;
import com.lq.code.util.sql.PageInterface;
/**分页父类
* Created by qi_liang on 2018/1/27.
*/
public class BasePageVo {
public class BasePageVo implements PageInterface {
//页码
private Integer page;
//行数
private Integer rows;
@Override
public Integer getPage() {
return page;
}
@Override
public Integer getPageSize() {
return rows;
}
public BasePageVo setPage(Integer page) {
this.page = page;
return this;

@ -5,6 +5,7 @@ import com.lq.code.util.StringUtil;
import com.lq.code.util.sql.AbstractDbBuiler;
import com.lq.code.util.sql.MysqlBuilder;
import com.lq.code.util.sql.OracleBuiler;
import com.lq.code.util.sql.PageInterface;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
@ -50,7 +51,7 @@ public class PageInteceptor implements Interceptor {
if (selectId.matches(pageSqlId)){
BoundSql boundSql=(BoundSql)metaStatementHandler.getValue("delegate.boundSql");
String sql=boundSql.getSql();
BasePageVo vo=(BasePageVo)boundSql.getParameterObject();
PageInterface pageInterface=(PageInterface)boundSql.getParameterObject();
AbstractDbBuiler db = null;
if (dialect.equals("mysql")){
db = new MysqlBuilder();
@ -59,7 +60,7 @@ public class PageInteceptor implements Interceptor {
db = new OracleBuiler();
}
String countSql= db.countSql(sql);
String pageSql=db.concatPageSql(sql,vo);
String pageSql=db.concatPageSql(sql,pageInterface);
metaStatementHandler.setValue("delegate.boundSql.sql",pageSql);
}

@ -187,24 +187,26 @@ public class BeanUtil {
String entityPath=entityClazz.getName();
String [] entityNameArray=entityPath.split("\\.");
String entityName=entityNameArray[entityNameArray.length-1];
return entityName;
}
public static void main(String[] args) {
SysUserVo vo = new SysUserVo();
vo.setPage(1);
vo.setCreateTime(new Date());
vo.setId(1L);
SysUser sysUser = new SysUser();
sysUser.setLoginName("小米数据");
copyNotNull(sysUser,vo);
System.out.println(sysUser.getId());
System.out.println(sysUser.getLoginName());
/**
* 判断对象是否该与类型一致
* @param obj
* @param clazzType
* @return
*/
public static Boolean isType(Object obj,Class clazzType){
if (obj!=null) {
Class clazz = obj.getClass();
return clazz.getTypeName().equals(clazzType.getTypeName()) ? true : false;
}
else{
return false;
}
}
/**
* 判断对象是否为空 ,为空返回true
* @param object

@ -0,0 +1,148 @@
package com.lq.code.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 转型操作工具
*/
public class CastUtil {
/**
* 对象类型转字符串类型
* @param obj 对象
* @return
*/
public static String castString(Object obj){
return castString(obj,"");
}
/**
* 对象类型转字符串类型
* @param obj 对象
* @param defaultValue 默认值
* @return
*/
public static String castString(Object obj,String defaultValue){
return obj !=null ? String.valueOf(obj):defaultValue;
}
/**
* 对象类型转 double
* @param obj
* @return
*/
public static double castDouble(Object obj){
return castDouble(obj, (double) 0);
}
/**
* 对象类型转 double
* @param obj
* @param defaultValue
* @return
*/
public static double castDouble(Object obj,Double defaultValue){
double value = defaultValue;
if (obj!=null){
String strValue = castString(obj);
if (StringUtil.isNotNull(strValue)){
value = Double.parseDouble(strValue);
}
}
return value;
}
/**
* 对象类型转 int
* @param obj
* @return
*/
public static int castInt(Object obj){
return castInt(obj,0);
}
/**
* 对象类型转 int
* @param obj
* @param defaultValue
* @return
*/
public static int castInt(Object obj,Integer defaultValue){
int value = defaultValue;
if (obj!=null){
String strValue = castString(obj);
if (StringUtil.isNotNull(strValue)){
value = Integer.parseInt(strValue);
}
}
return value;
}
/**
* 对象类型转布尔
* @param obj
* @return
*/
public static boolean castBoolean(Object obj){
return castBoolean(obj,false);
}
/**
* 对象类型转布尔
* @param obj
* @param defaultValue
* @return
*/
public static boolean castBoolean(Object obj,Boolean defaultValue){
boolean valule = defaultValue;
if (obj!=null){
String strValue = castString(obj);
if (StringUtil.isNotNull(strValue)){
valule = Boolean.parseBoolean(strValue);
}
}
return valule;
}
/**
* 对象类型装整型(长整型)
* @param obj
* @return
*/
public static long castLong(Object obj){
return castLong(obj,0L);
}
/**
* 对象类型装整型(长整型)
* @param obj
* @param defaultVaule
* @return
*/
public static long castLong(Object obj,Long defaultVaule){
long value = defaultVaule;
if (obj!=null){
String strValue = castString(obj);
if (StringUtil.isNotNull(strValue)){
value = Long.parseLong(strValue);
}
}
return value;
}
}

@ -0,0 +1,31 @@
package com.lq.code.util;
import org.apache.commons.collections.CollectionUtils;
import java.util.Collection;
/**
* 集合工具类
*/
public class Collectiontil {
/**
* 判断 collection 是否为null
* @param collection
* @return
*/
public static boolean isEmpty(Collection<?> collection){
return CollectionUtils.isEmpty(collection);
}
/**
* 判断 集合是否不为 null
* @param collection
* @return
*/
public static boolean isNotEmpty(Collection<?> collection){
return CollectionUtils.isNotEmpty(collection);
}
}

@ -76,6 +76,7 @@ public class FileUtil {
switch (fileFormat.toUpperCase()){
case "BMP":
case "JPEG":
case "JPG":
case "ICO":
case "PNG":
case "JNG":
@ -92,6 +93,7 @@ public class FileUtil {
fileType = FileTypeEnum.FILE_TYPE_OTHER.getValue();break;
}
}
return fileType;
}

@ -0,0 +1,113 @@
package com.lq.code.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 属性文件工具类
*/
public class PropsUtil {
private static final Logger logger = LoggerFactory.getLogger(PropsUtil.class);
/**
* 加载属性文件
* @param fileName
* @return
*/
public static Properties loadProps(String fileName){
Properties properties = null;
InputStream is = null;
try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
if (is == null){
throw new FileNotFoundException(fileName+" file is not found");
}
properties = new Properties();
properties.load(is);
} catch (IOException e) {
logger.error(" load properties file failure",e);
} finally {
if (is != null){
try {
is.close();
} catch (IOException e) {
logger.error("close input stram failure",e);
}
}
}
return properties;
}
public static String getString(Properties properties,String key){
return getString(properties,key,"");
}
/**
* 获取字符串类型属性
* @param properties 配置文件对象
* @param key 属性key
* @param defaultValue 属性为空设置默认值
* @return
*/
public static String getString(Properties properties,String key,String defaultValue){
String value = defaultValue;
if (properties.containsKey(key)){
value = properties.getProperty(key);
}
return value;
}
/**
* 获取整型属性(默认值:0)
* @param properties
* @param key
* @return
*/
public static int getInt(Properties properties,String key){
return getInt(properties,key,0);
}
/**
* 获取整型属性
* @param properties
* @param key
* @param defaultValue
* @return
*/
public static int getInt(Properties properties,String key,int defaultValue){
int value = defaultValue;
if (properties.containsKey(key)){
value = CastUtil.castInt(properties.getProperty(key));
}
return value;
}
/**
* 获取布尔类型属性默认值false
* @param properties
* @param key
* @return
*/
public static boolean getBoolean(Properties properties,String key){
return getBoolean(properties,key,false);
}
public static boolean getBoolean(Properties properties,String key,Boolean defaultValue){
boolean value = defaultValue;
if (properties.containsKey(key)){
value = CastUtil.castBoolean(properties.getProperty(key));
}
return value;
}
}

@ -1,5 +1,6 @@
package com.lq.code.util.jdbc;
import com.lq.code.util.PropsUtil;
import com.lq.code.util.StringUtil;
import com.lq.code.util.jdbc.mode.Column;
import com.lq.code.util.jdbc.mode.DbInfo;
@ -23,18 +24,16 @@ public class JdbcUtils {
private static String password = null;
static {
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties prop = new Properties();
try {
prop.load(in);
Properties prop = PropsUtil.loadProps("jdbc.properties");
driver = prop.getProperty("jdbc.driver");
driver = PropsUtil.getString(prop,"jdbc.driver");
url = prop.getProperty("jdbc.url");
url = PropsUtil.getString(prop,"jdbc.url");
username = prop.getProperty("jdbc.user");
username = PropsUtil.getString(prop,"jdbc.user");
password = prop.getProperty("jdbc.password");
password = PropsUtil.getString(prop,"jdbc.password");
Class.forName(driver);
} catch (Exception e) {

@ -21,10 +21,10 @@ public abstract class AbstractDbBuiler {
/**
*  构建分页sql语句
* @param sql
* @param vo
* @param pageInterface
* @return
*/
abstract public String concatPageSql(String sql, BasePageVo vo);
abstract public String concatPageSql(String sql, PageInterface pageInterface);
/**
* 构建创建表的sql语句

@ -98,11 +98,11 @@ public class MysqlBuilder extends AbstractDbBuiler {
}
@Override
public String concatPageSql(String sql, BasePageVo vo) {
public String concatPageSql(String sql, PageInterface pageInterface) {
StringBuffer sb=new StringBuffer(sql);
Integer index=(vo.getPage()-1)*vo.getRows();
sb.append(" limit ").append(index).append(","+vo.getRows());
Integer index=(pageInterface.getPage()-1)*pageInterface.getPageSize();
sb.append(" limit ").append(index).append(","+pageInterface.getPageSize());
return sb.toString();
}

@ -16,11 +16,11 @@ public class OracleBuiler extends AbstractDbBuiler {
}
@Override
public String concatPageSql(String sql, BasePageVo vo) {
public String concatPageSql(String sql, PageInterface pageInterface) {
StringBuffer sb=new StringBuffer("select * from (select t1.*,ROWNUM rm from(");
sb.append(sql);
Integer index=(vo.getPage()-1)*vo.getRows();
sb.append(")t1 where rownum<="+(index+vo.getRows())+") where rm>"+index);
Integer index=(pageInterface.getPage()-1)*pageInterface.getPageSize();
sb.append(")t1 where rownum<="+(index+pageInterface.getPageSize())+") where rm>"+index);
return sb.toString();
}

@ -0,0 +1,19 @@
package com.lq.code.util.sql;
/**
* 分页抽象类
*/
public interface PageInterface {
/**
* 页码
* @return
*/
Integer getPage();
/**
* 每页数量
* @return
*/
Integer getPageSize();
}

@ -35,6 +35,11 @@ public class SqlUtil {
return strBuffer.toString().toLowerCase();
}
/**
* 驼峰装大小写
* @param str
* @return
*/
public static String humpToCase(String str){
StringBuffer strBuffer=new StringBuffer();
char[] charArray=str.toCharArray();

@ -2,15 +2,10 @@ jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8
jdbc.user=root
jdbc.password=mysql
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
initialSize=0
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
initialSize=5
maxActive=5
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
maxIdle=5
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
minIdle=1
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
maxWait=6000
maxWait=60000
email.port=Create property

@ -52,7 +52,8 @@
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=utf-8"/>
</bean></mvc:message-converters>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

@ -27,26 +27,41 @@
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<!--属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall-->
<property name="filters" value="stat" />
<property name="maxActive" value="50" />
<property name="initialSize" value="5" />
<property name="maxWait" value="10000" />
<property name="minIdle" value="5" />
<!--最大连接池数量-->
<property name="maxActive" value="${maxActive}" />
<!-- 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时-->
<property name="initialSize" value="${initialSize}" />
<!--获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。-->
<property name="maxWait" value="${maxWait}" />
<!--最小连接池数量-->
<property name="minIdle" value="${minIdle}" />
<!--有两个含义:1.Destroy线程会检测连接的间隔时间 2.testWhileIdle的判断依据,-->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<!--是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。-->
<property name="poolPreparedStatements" value="false"/>
<!--要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100-->
<!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="100"/>-->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!--用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。-->
<property name="validationQuery" value="SELECT 'X'" />
<!--建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。-->
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="true" />
<!--申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。-->
<property name="testOnBorrow" value="false" />
<!--归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能-->
<property name="testOnReturn" value="false" />
<property name="maxOpenPreparedStatements"
value="20" />
<property name="removeAbandonedTimeout" value="1800" /> <!-- 1800秒,也就是30分钟 -->
<property name="logAbandoned" value="true" /> <!-- 关闭abanded连接时输出错误日志 -->
<!-- 物理连接初始化的时候执行的sql-->
<!-- <property name="connectionInitSqls" value=""/>-->
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

Loading…
Cancel
Save