spring配置文件中需要配置
<bean id="springContextHolder" class="com.xxx.kf.sso.tool.SpringContextHolder"
lazy-init="false" />
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="${jdbc.url}"></property>
</bean>
package com.xxx.kf.sso.tool;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContextHolder implements ApplicationContextAware,
DisposableBean {
private static ApplicationContext applicationContext = null;
/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext;
}
/**
* 实现DisposableBean接口, 在Context关闭时清理静态变量.
*/
@Override
public void destroy() throws Exception {
applicationContext = null;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
}
使用它的java类
package com.xxx.kf.sso.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.xiaomi.kf.sso.model.SsoUser;
import com.xiaomi.kf.sso.model.SsoUserBind;
import com.xiaomi.kf.sso.model.SsoUserLogin;
import com.xiaomi.kf.sso.tool.SpringContextHolder;
public class SsoDAO {
private static SsoDAO instance = new SsoDAO();
private DataSource ds;
private SsoDAO() {
ds = SpringContextHolder.getBean("ds");
}
public static SsoDAO getInstance() {
return instance;
}
public int insertSsoUser(SsoUser ssoUser){
String sql = "INSERT INTO sso_user(ssoId,name) VALUES (?, ? )";
PreparedStatement ps = null;
Connection conn = null;
int result = 0;
try {
conn = ds.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, ssoUser.getSsoId());
ps.setString(2, ssoUser.getName());
result = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public int insertSsoUserBind(SsoUserBind ssoUserBind) throws SQLException{
String sql = "INSERT INTO sso_user_bind(ssoId,bindType,bindInfo) VALUES (?, ?, ? )";
PreparedStatement ps = null;
Connection conn = null;
int result = 0;
try {
conn = ds.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, ssoUserBind.getSsoId());
ps.setInt(2, ssoUserBind.getBindType());
ps.setString(3, ssoUserBind.getBindInfo());
result = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public int insertSsoUserLogin(SsoUserLogin ssoUserLogin) throws SQLException{
String sql = "INSERT INTO sso_user_login(ssoId,mobile,staff_number,pwd) VALUES (?, ?, ?, ? )";
PreparedStatement ps = null;
Connection conn = null;
int result = 0;
try {
conn = ds.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, ssoUserLogin.getSsoId());
ps.setString(2, ssoUserLogin.getMobile());
ps.setString(3, ssoUserLogin.getStaffNumber());
ps.setString(4, ssoUserLogin.getPwd());
result = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}