# DEBUG\u8BBE\u7F6E\u8F93\u51FA\u65E5\u5FD7\u7EA7\u522B\uFF0C\u7531\u4E8E\u4E3ADEBUG\uFF0C\u6240\u4EE5ERROR\u3001WARN\u548CINFO \u7EA7\u522B\u65E5\u5FD7\u4FE1\u606F\u4E5F\u4F1A\u663E\u793A\u51FA\u6765
log4j.rootLogger=DEBUG,Console,RollingFile
#\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern= [%-5p]-[%d{yyyy-MM-dd HH:mm:ss}] -%l -%m%n
#\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u64CD\u4F5C\u7CFB\u7EDFD\u76D8\u6839\u76EE\u5F55\u4E0B\u7684log.log\u6587\u4EF6\u4E2D
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=D://log.log
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p %-40.40c %X{traceId}-%m%n
DBLink.java:
package com.jd.tool.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
import com.jd.tool.PropertiesTool;
/**
* 鏁版嵁搴撶鐞嗗伐鍏风被
*
* @author GaoHuanjie
*/
public class DBLink {
private Logger logger = Logger.getLogger(DBLink.class);
/**
* 鑾峰彇鏁版嵁搴撹繛鎺�
*
* @author GaoHuanjie
*/
private Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩
String userName = PropertiesTool.getValue("db.username");
String password = PropertiesTool.getValue("db.password");
String url = PropertiesTool.getValue("db.url");
return DriverManager.getConnection(url, userName, password);//鑾峰彇杩炴帴
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}
return null;
}
/**
* 鍒ゆ柇SQL璇彞鏄惁鑳芥煡鍑烘暟鎹�
*
* @author GaoHuanjie
*/
public boolean exist(String sql) {
Connection connection = null;
Statement statement =null;
ResultSet resultSet=null;
try {
connection = getConnection();//鑾峰彇杩炴帴
statement = connection.createStatement();
resultSet= statement.executeQuery(sql);//鎵цsql,灏嗘煡璇㈢殑鏁版嵁瀛樺埌ResultSet绫诲瀷鐨勫彉閲忎腑
return resultSet.next();
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(resultSet,statement,connection);
}
return false;
}
/**
* 鍒ゆ柇SQL璇彞鏄惁鑳芥煡鍑烘暟鎹�
*
* @author GaoHuanjie
*/
public boolean exist(String sql, Object ...params) {
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet=null;
try {
connection = getConnection();//鑾峰彇杩炴帴
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1, params[i]);
}
resultSet= preparedStatement.executeQuery();//鎵цsql,灏嗘煡璇㈢殑鏁版嵁瀛樺埌ResultSet绫诲瀷鐨勫彉閲忎腑
return resultSet.next();
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(resultSet,preparedStatement,connection);
}
return false;
}
/**
* 鏌ヨ鏁版嵁
*
* @author GaoHuanjie
*/
public void select(String sql,IRowMapper rowMapper) {//鎺ュ彛鏃犳硶鍒涘缓瀵硅薄锛屾墍浠owMapper鍙傛暟涓�瀹氭寚鍚慖RowMapper鎺ュ彛瀹炵幇绫诲璞�
Connection connection = null;
Statement statement =null;
ResultSet resultSet=null;
try {
connection = getConnection();//鑾峰彇杩炴帴
statement = connection.createStatement();
resultSet= statement.executeQuery(sql);//鎵цsql,灏嗘煡璇㈢殑鏁版嵁瀛樺埌ResultSet绫诲瀷鐨勫彉閲忎腑
rowMapper.rowMapper(resultSet);//鍥犱负rowMapper鍙傛暟鎸囧悜IRowMapper鎺ュ彛瀹炵幇绫诲璞★紝鎵�浠ユ澶勫皢璋冪敤鎺ュ彛瀹炵幇绫讳腑鎵�瀹炵幇鐨剅owMapper鏂规硶 澶氭��
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {//閲婃斁璧勬簮
close(resultSet,statement,connection);
}
}
/**
* 鏌ヨ鏁版嵁
*
* @author GaoHuanjie
*/
public void select(String sql,IRowMapper rowMapper,Object ...params) {
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet=null;
try {
connection = getConnection();//鑾峰彇杩炴帴
preparedStatement= connection.prepareStatement(sql);//鍚湁锛熷彿鍗犱綅绗︾殑sql
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1, params[i]);//涓猴紵鍙疯祴鍊�
}
resultSet= preparedStatement.executeQuery();//鎵цsql
rowMapper.rowMapper(resultSet);//鍥犱负rowMapper鍙傛暟鎸囧悜IRowMapper鎺ュ彛瀹炵幇绫诲璞★紝鎵�浠ユ澶勫皢璋冪敤鎺ュ彛瀹炵幇绫讳腑鎵�瀹炵幇鐨剅owMapper鏂规硶 澶氭��
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {//閲婃斁璧勬簮
close(resultSet,preparedStatement,connection);
}
}
/**
* 淇敼锛坕nsert銆乽pdate鍜宒elete锛夋暟鎹�
*
* @author GaoHuanjie
*/
public boolean update(String sql) {
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();//鑾峰彇鏁版嵁搴撹繛鎺ュ璞★紝涓�涓璞¤〃绀轰竴娆℃暟鎹簱杩炴帴
statement = connection.createStatement();//鑾峰彇Statement瀵硅薄
int result = statement.executeUpdate(sql);//鎵цsql璇彞锛岃繑鍥炲彈褰卞搷鐨勮鏁帮紝浠呴檺浜庢暟鎹甶nsert銆乽pdate鍜宒elete
return result>0;//澶勭悊缁撴灉
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {//鍗充究鏈夊紓甯镐篃浼氭墽琛屼唬鐮�
close(statement,connection);
}
return false;
}
/**
* 淇敼锛坕nsert銆乽pdate鍜宒elete锛夋暟鎹�
*
* @author GaoHuanjie
*/
public boolean update(String sql,Object ...params) {
Connection connection= null;
PreparedStatement preparedStatement= null;
try {
connection= getConnection();
preparedStatement= connection.prepareStatement(sql);//鍚湁锛熷彿鍗犱綅绗︾殑sql
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1, params[i]);//涓猴紵鍙疯祴鍊�
}
return preparedStatement.executeUpdate()>0;
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}finally {
close(preparedStatement,connection);
}
return false;
}
/**
* 閲婃斁璧勬簮
*
* @author GaoHuanjie
*/
private void close(Statement statement,Connection connection) {
try {
if(statement!=null) {//鏈夊彲鑳界敱浜庡紓甯稿鑷磗tatement娌℃湁璧嬪�硷紝姣斿url鍑洪敊
statement.close();
}
} catch (SQLException e) {
logger.debug(e.getMessage(), e);
}
try {
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
logger.debug(e.getMessage(), e);
}
}
/**
* 閲婃斁璧勬簮
*
* @author GaoHuanjie
*/
private void close(ResultSet resultSet,Statement statement,Connection connection) {//閲嶈浇
try {
if(resultSet!=null) {
resultSet.close();
}
} catch (SQLException e) {
logger.debug(e.getMessage(), e);
}
close(statement,connection);
}
}
IRowMapper.java:
package com.jd.tool.db;
import java.sql.ResultSet;
public interface IRowMapper {
void rowMapper(ResultSet rs);
}
PropertiesTool.java:
package com.jd.tool;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesTool {
private static Properties properties = new Properties();
static {
InputStream inputStream = PropertiesTool.class.getClassLoader().getResourceAsStream("db.properties");//灏哾b.properties鍙樹负javaIO娴佸璞�
try {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key) {
return properties.getProperty(key);
}
}
MD5Tool.java:
package com.jd.tool;
import java.math.BigInteger;
import java.security.MessageDigest;
public class MD5Tool {
public static String encrypt(String password) {
byte[] bytes = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(password.getBytes());//鍔犲瘑
bytes = messageDigest.digest();//鑾峰緱鍔犲瘑缁撴灉
} catch (Exception e) {
e.printStackTrace();
}
String result = new BigInteger(1, bytes).toString(16);// 灏嗗姞瀵嗗悗鐨勬暟鎹浆鎹负16杩涘埗鏁板瓧
// 鐢熸垚鏁板瓧鏈弧32浣嶏紝鍒欏墠闈㈣ˉ0
for (int i = 0; i < 32 - result.length(); i++) {
result = "0" + result;
}
return result;
}
}