Java MVC 使用工厂模式、懒人模式

1、数据库连接池 开机启动类
import java.beans.PropertyVetoException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataConnectionPool_userInfo extends HttpServlet{

public DataConnectionPool_userInfo(){
super();
}
//获取数据库连接池
public void init() throws ServletException {
System.out.println((new Date())+"--- 获取数据库连接池c3p0 用户信息");
//获取数据库连接池资源
ComboPooledDataSource dataSource = new ComboPooledDataSource();

//设置数据库连接池的驱动
try {
dataSource.setDriverClass(DbHelper_userInfo.driver);//com.mysql.jdbc.Driver
dataSource.setJdbcUrl(DbHelper_userInfo.url);//jdbc:mysql://localhost:3306/mytry
dataSource.setUser(DbHelper_userInfo.userName);//root
dataSource.setPassword(DbHelper_userInfo.password);//""

/*dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");//com.mysql.jdbc.Driver
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");//jdbc:mysql://localhost:3306/mytry
dataSource.setUser("userInfo");//root
dataSource.setPassword("oracle");//""*/
} catch (PropertyVetoException e) {
e.printStackTrace();
}finally{
DataSource_userInfo.setDataSource(dataSource);
//System.out.println("--- 打开数据库连接池c3p0 考勤 "+dataSource);
}
super.init();
}
//关闭数据库连接池
public void destroy() {
System.out.println((new Date())+"--- 关闭数据库连接池c3p0 考勤");
super.destroy();
}

}

2、保存、获取数据库连接池的类
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataSource_userInfo {

public static ComboPooledDataSource dataSource;
//设定数据连接池
public static void setDataSource(ComboPooledDataSource dataSource){
DataSource_userInfo.dataSource = dataSource;
//System.out.println("--- 打开数据库连接池c3p0 考勤 ");
//dataSource = dataSource;
}
//获取数据连接池中的连接
public static ComboPooledDataSource getDataSource(){
return dataSource;
}

}

3、为获取数据连接、操作连接服务的类

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.yx.publics.PublicUses_param;
public class DbHelper_userInfo {
//获取连接用户数据库信息的属性
public static String driver="";
public static String url="";
public static String userName="";
public static String password="";
static{
Properties pro=new Properties();
String path=PublicUses_param.DbPROPER_USERINFO_PATH;
InputStream in=DbHelper_userInfo.class.getResourceAsStream(path);
try {
pro.load(in);
//myDataSource = DataSource_userInfo.getDataSource();
//String aString = "";
//myDataSource = BasicDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
System.out.print(e);
}
driver=pro.getProperty("driverClassName");
url=pro.getProperty("url");
userName=pro.getProperty("userName");
password=pro.getProperty("password");
//String aa = "";
}


/**
* 获取数据源
*
* @return
*/
/*public static ComboPooledDataSource getDataSource() {
return myDataSource;
} */


/**
* 获取连接
*
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
//DataSource.getDataSource().getConnection();
return DataSource_userInfo.getDataSource().getConnection();
//return myDataSource.getConnection();
}


/**
* 关闭资源
* @param rs
* @param st
* @param conn
* @throws SQLException
*/
public static void free(ResultSet rs, Statement st, Connection conn) throws SQLException {
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
throw new SQLException();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
throw new SQLException();
} finally {
if (conn != null){
try {
conn.close();
} catch (Exception e) {
throw new SQLException();
}
}
}
}
}
}

3、DAO层

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.yx.db.DbHelper_userInfo;
import com.yx.exception.DaoException;
import com.yx.exception.DaoParameterException;
public class DaoOperateTemplate_userInfo {

/**
* 查找单个记录对象
*
* @param sql
* @param args
* @param rowMapper
* @return
* @throws DaoException
*/
public Object find(String sql, Object[] args, RowMapper rowMapper) throws DaoException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DbHelper_userInfo.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++){
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
Object obj = null;
if (rs.next()) {
obj = rowMapper.mapRow(rs);
}
return obj;
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
try {
DbHelper_userInfo.free(rs, ps, conn);
} catch (SQLException e) {
throw new DaoParameterException(e.getMessage(), e);
}
}
}

/**
* 查找多条记录对象
*
* @param sql
* @param args
* @param rowMapper
* @return
* @throws DaoException
*/
public List<Object> Query(String sql, Object[] args, RowMapper rowMapper) throws DaoException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Object> results = new ArrayList<Object>();
try {
conn = DbHelper_userInfo.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++){
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
Object obj = null;
while (rs.next()) {
obj = rowMapper.mapRow(rs);
results.add(obj);
}
return results;
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
try {
DbHelper_userInfo.free(rs, ps, conn);
} catch (SQLException e) {
throw new DaoParameterException(e.getMessage(), e);
}
}
}

/**
* 更新操作
*
* @param sql
* @param args
* @param isGeneralKey
* @throws DaoException
*/
public int update(String sql, Object[] args, boolean isGeneralKey)throws DaoException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DbHelper_userInfo.getConnection();
ps = (isGeneralKey?conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS):conn.prepareStatement(sql));
for (int i = 0; i < args.length; i++){
ps.setObject(i + 1, args[i]);
}
return ps.executeUpdate();
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
try {
DbHelper_userInfo.free(rs, ps, conn);
} catch (SQLException e) {
throw new DaoParameterException(e.getMessage(), e);
}
}
}
}

4、公共的获取处理类
import java.sql.ResultSet;
import java.sql.SQLException;
public interface RowMapper {

/**
* 映射接口
* @param rs
* @return
* @throws SQLException
*/
public Object mapRow(ResultSet rs) throws SQLException;

}

5、使用工厂模式获取指定的接口的实现类

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.yx.publics.PublicUses_param;

/**
* 工厂类方法
*
*/
public class BoFactory_userInfo {

//private static BoFactory_userInfo instance = new BoFactory_userInfo();//懒汉法声明对象
private static BoFactory_userInfo instance = new BoFactory_userInfo();//懒汉法声明对象
private static Properties pro;// 配置文件对象

private BoFactory_userInfo() {
InputStream inputStream = null;
try {
// 初始化配置文件
pro = new Properties();
// 采用类加载器方法读取配置文件信息到字节流对象,采用类加载灵活,不用写死
inputStream = BoFactory_userInfo.class.getClassLoader().getResourceAsStream(PublicUses_param.BOPROPER_BO_USER_INS);
// 加载字节流对象
pro.load(inputStream);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
} finally{
try {
if(inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 单例模式获取唯一实例
*
* @return
*/
public static BoFactory_userInfo getInstance(){
return instance;
}

/**
* 根据配置文件的名字获取类的名字,采用反射机制获取其对象
*
* @param Key
* @return
*/
public Object getBO(String Key) throws Exception {
String className = (String) pro.get(Key);
return (Class.forName(className).newInstance());
}

}

6、用于处理的接口类 bo

import java.util.List;

import com.yx.exception.DaoException;
import com.yx.vo.UserInfo;

public interface UserInfoBo {

/**
* 添加方法
* @param student
* @throws DaoException
*/
public int insertUserInfo(UserInfo userInfo) throws DaoException;

/**
* 删除方法
* @param student
* @throws DaoException
*/
public int deleteUserInfo(UserInfo userInfo) throws DaoException;

/**
* 修改方法
* @param student
* @throws DaoException
*/
public int modifyUserInfo(UserInfo userInfo) throws DaoException;
/**
* 修改方法 修改密码
* @param student
* @throws DaoException
*/
public int modifyUserInfoPass(UserInfo userInfo) throws DaoException;

/**
* 获取列表
* @return
* @throws DaoException
*/
public List<UserInfo> selectUserInfo(String order,String sort,int pageIndex,int pageSize) throws DaoException;
public List<UserInfo> selectUserInfo(String[] param,String[] paramValue,String order,String sort,int pageIndex,int pageSize) throws DaoException;

//获取满足条件的信息条数
public int selectUserInfoCount(String[] param,String[] paramValue,String sort) throws DaoException;

}

7、实现处理的接口类 bo
package com.yx.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.yx.dao.DaoOperateTemplate_userInfo;
import com.yx.dao.RowMapper;
import com.yx.exception.DaoException;
import com.yx.publics.PublicUses_method;
import com.yx.vo.UserInfo;

public class UserInfoBoImpl implements UserInfoBo {

private DaoOperateTemplate_userInfo daoTemplate = new DaoOperateTemplate_userInfo();

/**
* 添加方法
* @param student
* @throws DaoException
*/
public int insertUserInfo(UserInfo userInfo) throws DaoException {
// userInfo表有20个字段
//String sql = "insert into userInfo(userId,userName,password,limits,status,createTime,creater,modifyTime,"+
String sql = "insert into userInfo(userId,userName,password,limits,status,type,creater,"+
"modifyer,reservedInt1,reservedInt2,reservedInt3,reservedInt4,reservedInt5,reservedChar1,reservedChar2,"+
"reservedChar3,reservedChar4,reservedChar5,reservedChar6) "+
"values(seq_userInfoId.Nextval,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Object[] args = new Object[]{userInfo.getUserName(),userInfo.getPassword(),
userInfo.getLimits(),userInfo.getStatus(),userInfo.getType(),userInfo.getCreater(),
userInfo.getModifyer(),userInfo.getReservedInt1(),userInfo.getReservedInt2(),
userInfo.getReservedInt3(),userInfo.getReservedInt4(),userInfo.getReservedInt5(),
userInfo.getReservedChar1(),userInfo.getReservedChar2(),userInfo.getReservedChar3(),
userInfo.getReservedChar4(),userInfo.getReservedChar5(),userInfo.getReservedChar6()};
return daoTemplate.update(sql,args,false);
}

/** *****************************************************
* 删除方法
* @param student
* @throws DaoException
*/
public int deleteUserInfo(UserInfo userInfo) throws DaoException {
return 0;
}

/** *****************************************************
* 修改方法
* @param student
* @throws DaoException
*/
public int modifyUserInfo(UserInfo userInfo) throws DaoException {
// userInfo表有20个字段 //to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') reservedChar1
String sql = "update userInfo set userName=?,limits=?,status=?,type=?,"+
"modifyTime=sysdate,modifyer=?,reservedInt1=?,reservedInt2=?,reservedInt3=?,reservedInt4=?,reservedInt5=?,"+
"reservedChar1=?,reservedChar2=?,reservedChar3=?,reservedChar4=?,reservedChar5=?,reservedChar6=? where userId=?";
Object[] args = new Object[]{userInfo.getUserName(),
userInfo.getLimits(),userInfo.getStatus(),userInfo.getType(),
userInfo.getModifyer(),userInfo.getReservedInt1(),userInfo.getReservedInt2(),
userInfo.getReservedInt3(),userInfo.getReservedInt4(),userInfo.getReservedInt5(),
userInfo.getReservedChar1(),userInfo.getReservedChar2(),userInfo.getReservedChar3(),
userInfo.getReservedChar4(),userInfo.getReservedChar5(),userInfo.getReservedChar6(),userInfo.getUserId()};
/*System.out.println("--1、"+userInfo.getReservedChar1()+"--2、"+userInfo.getReservedChar2()+
"--3、"+userInfo.getReservedChar3()+"--4、"+userInfo.getReservedChar4()
+"--5、"+userInfo.getReservedChar5()+"--6、"+userInfo.getReservedChar6());*/
return daoTemplate.update(sql,args,false);
}

/**
* 修改方法 修改密码
* @param student
* @throws DaoException
*/
public int modifyUserInfoPass(UserInfo userInfo) throws DaoException {
String sql = "update userInfo set password=? where userId=?";
Object[] args = new Object[]{userInfo.getPassword(),userInfo.getUserId()};
return daoTemplate.update(sql,args,false);
}

/**
* 获取列表
* @return
* @throws DaoException
*/
@SuppressWarnings("unchecked")
public List<UserInfo> selectUserInfo(String order,String sort,int pageIndex,int pageSize) throws DaoException {
String[] param = new String[0];
String[] paramValue = new String[0];
/*List courseList = selectUserInfo(param,paramValue);
return courseList;*/
return selectUserInfo(param,paramValue,order,sort,pageIndex,pageSize);
}
/**
* 获取列表
* @return
* @throws DaoException
* select * from (select rownum rown,d.* from yongxin_userInfo.userInfo d where 1=1 and 1=1 order by userId)
* where 1=1 and rownum<=10 and rown>((1-1)*10) order by userId ;
*
* param为sql中的字段名 paramValue为字段内容
* order为排序内容 sort为纯sql语句
* pageIndex为第几页 pageSize为每页多少条数据
*/
@SuppressWarnings("unchecked")
public List<UserInfo> selectUserInfo(String[] param,String[] paramValue,String order,String sort,int pageIndex,int pageSize) throws DaoException {
//String sql = "select * from userInfo where id=? ";
/*String sql = "select * from userInfo where 1=1 ";
int paramLength = param.length;
int paramVLength = paramValue.length;
Object[] args = null;
//判断传输的参数长度是否相等 参数长度是否为0
if(paramLength!=paramVLength||paramLength==0||paramVLength==0){
args = new Object[0];
}else {
for(int i=0;param!=null&&paramLength>0&&i<paramLength;i++){
sql = sql+" and "+param[i]+"=? ";
}
args = new Object[paramLength];
for(int i=0;paramValue!=null&&paramVLength>0&&i<paramVLength;i++){
args[i] = paramValue[i];
}
}*/
String sql1 = "select * from (select rownum rown,d.* from yongxin_userInfo.userInfo d where 1=1 ";
String sql2 = " where 1=1 and rownum<="+pageSize+" and rown>(("+pageIndex+"-1)*"+pageSize+") ";
int paramLength = param.length;
int paramVLength = paramValue.length;
Object[] args = null;
//判断传输的参数长度是否相等 参数长度是否为0
if(paramLength!=paramVLength||paramLength==0||paramVLength==0){
args = new Object[0];
}else {
for(int i=0;param!=null&&paramLength>0&&i<paramLength;i++){
sql1 = sql1+" and "+param[i]+"=? ";
sql2 = sql2+" and "+param[i]+"=? ";
}
args = new Object[paramLength*2];
for(int i=0;paramValue!=null&&paramVLength>0&&i<(paramVLength*2);i++){
if(i<paramVLength){
args[i] = paramValue[i];
}else{
args[i] = paramValue[i-paramVLength];
}
}
}
if(sort!=null&&!"".equals(sort)&&sort.length()>1){
sql1=sql1+" and "+sort+" "+order+" ) ";
sql2=sql2+" and "+sort+" "+order;
}else{
sql1=sql1+order+" ) ";
sql2=sql2+order;
}
String sql = sql1+sql2;
System.out.println("获取用户信息的sql语句:"+sql);
List courseList = daoTemplate.Query(sql,args,new userInfoRowMapper());
return courseList;
}

/*
*获取满足条件的信息条数
*/
@SuppressWarnings("unchecked")
public int selectUserInfoCount(String[] param,String[] paramValue,String sort) throws DaoException {
//String sql = "select * from userInfo where id=? ";
//String sql = "select count(*) num from userInfo where 1=1 ";
String sql = "select * from userInfo where 1=1 ";
int paramLength = 0;
if(param!=null&&param.length>0){
paramLength = param.length;
}
int paramVLength = 0;
if(paramValue!=null&&paramValue.length>0){
paramVLength = paramValue.length;
}
Object[] args = null;
//判断传输的参数长度是否相等 参数长度是否为0
if(paramLength!=paramVLength||paramLength==0||paramVLength==0){
args = new Object[0];
}else {
for(int i=0;param!=null&&paramLength>0&&i<paramLength;i++){
sql = sql+" and "+param[i]+"=? ";
}
args = new Object[paramLength];
for(int i=0;paramValue!=null&&paramVLength>0&&i<paramVLength;i++){
args[i] = paramValue[i];
}
}
if(sort!=null&&!"".equals(sort)&&sort.length()>1){
sql = sql +" and " +sort;
}
System.out.println("获取用户信息的sql语句:"+sql);
List courseList = daoTemplate.Query(sql,args,new userInfoRowMapper());
return courseList.size();
}


/**
* 内部匿名类
*
* @author Administrator
*
*/
class userInfoRowMapper implements RowMapper{
public Object mapRow(ResultSet rs) throws SQLException {
UserInfo userInfo = new UserInfo();
userInfo.setUserId(rs.getInt("userId"));
userInfo.setUserName(rs.getString("userName"));
userInfo.setPassword(rs.getString("password"));
userInfo.setStatus(rs.getInt("status"));
userInfo.setLimits(rs.getInt("limits"));
userInfo.setType(rs.getInt("type"));
userInfo.setCreateTime(rs.getString("createTime"));
userInfo.setCreater(rs.getInt("creater"));
userInfo.setModifyTime(rs.getString("modifyTime"));
userInfo.setModifyer(rs.getInt("modifyer"));
userInfo.setReservedInt1(rs.getInt("reservedInt1"));
userInfo.setReservedInt2(rs.getInt("reservedInt2"));
userInfo.setReservedInt3(rs.getInt("reservedInt3"));
userInfo.setReservedInt4(rs.getInt("reservedInt4"));
userInfo.setReservedInt5(rs.getInt("reservedInt5"));
userInfo.setReservedChar1(PublicUses_method.checkParam(rs.getString("reservedChar1"),""));
userInfo.setReservedChar2(PublicUses_method.checkParam(rs.getString("reservedChar2"),""));
userInfo.setReservedChar3(PublicUses_method.checkParam(rs.getString("reservedChar3"),""));
userInfo.setReservedChar4(PublicUses_method.checkParam(rs.getString("reservedChar4"),""));
userInfo.setReservedChar5(PublicUses_method.checkParam(rs.getString("reservedChar5"),""));
userInfo.setReservedChar6(PublicUses_method.checkParam(rs.getString("reservedChar6"),""));
return userInfo;
}
}




}

8、action类
package com.yx.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONObject;
import com.yx.bo.BoFactory_userInfo;
import com.yx.bo.UserInfoBo;
import com.yx.publics.PublicUses_method;
import com.yx.publics.PublicUses_param;
import com.yx.vo.UserInfo;

@SuppressWarnings("serial")
public class GetUserInfo extends HttpServlet{

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//super.doPost(request,response);
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

//设置要用到的必要信息
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String[] params = null;
String[] paramValues = null;
JSONObject obj = new JSONObject();
JSONArray arr=new JSONArray();
JSONObject objV = new JSONObject();
List<UserInfo> userInfoList = null;
UserInfo userInfo = null;
int num = 0;

try {
@SuppressWarnings("unused")
//设置要用到的必要信息
UserInfoBo userInfoBo = (UserInfoBo) BoFactory_userInfo.getInstance().getBO("userInfoBo");

//获取查询的项目 默认为-1
//-1转到参数错误页面或者404页面 0为匹配用户名和密码是否正确 1获取指定用户信息 2为获取所有用户信息
int param = PublicUses_method.checkParam(request.getParameter("param"),-1);
//判断获取用户信息的项目
//0为获取用户名、用户id、等级、状态、类型,1为获取除保留字段外的信息,2为获取保留字信息但无信息colb字段
//3为获取所有保留字段信息,4获取所有用户字段信息但无信息colb字段,5获取所有用户字段信息
int paramV = PublicUses_method.checkParam(request.getParameter("paramV"),0);

//登录的用户名
String loginUserName = PublicUses_method.checkParam(session.getAttribute("userName"),"");
int loginUserId = PublicUses_method.checkParam(session.getAttribute("userId"),-1);
//无参数 session中无userId、userName 登录后如果状态为-1
//System.out.println("---"+param+"==="+loginUserId+"----"+loginUserName);
if(PublicUses_param.getUesrInfo){
if(param==-1||(param!=0&&loginUserId==-1)||(param!=0&&"".equals(loginUserName))){
obj.put("success",false);
obj.put("info","获取用户信息错误:1、参数错误:param:"+param+";2、会话中用户名或用户id为空");
System.out.println("获取用户信息错误:1、参数错误:param:"+param+";2、会话中用户名或用户id为空");
return;
}
}

//获取关于userInfo表的其他信息
int userId = PublicUses_method.checkParam(request.getParameter("userId"),-1000);//当为0时为获取所有用户信息
String userName = PublicUses_method.checkParam(request.getParameter("userName"),"");
String password = PublicUses_method.checkParam(request.getParameter("password"),"");
double limits = PublicUses_method.checkParam(request.getParameter("limits"),-1000);
int status = PublicUses_method.checkParam(request.getParameter("status"),-1000);
int type = PublicUses_method.checkParam(request.getParameter("type"),-1000);
int creater = PublicUses_method.checkParam(request.getParameter("creater"),-1000);//创建人id
String createTime = PublicUses_method.checkParam(request.getParameter("createTime"),"");
int modifyer = PublicUses_method.checkParam(request.getParameter("modifyer"),-1000);//创建人id
String modifyTime = PublicUses_method.checkParam(request.getParameter("modifyTime"),"");
int reservedInt1 = PublicUses_method.checkParam(request.getParameter("reservedInt1"),-1000);
int reservedInt2 = PublicUses_method.checkParam(request.getParameter("reservedInt2"),-1000);
int reservedInt3 = PublicUses_method.checkParam(request.getParameter("reservedInt3"),-1000);
int reservedInt4 = PublicUses_method.checkParam(request.getParameter("reservedInt4"),-1000);
int reservedInt5 = PublicUses_method.checkParam(request.getParameter("reservedInt5"),-1000);
String reservedChar1 = PublicUses_method.checkParam(request.getParameter("reservedChar1"),"");
String reservedChar2 = PublicUses_method.checkParam(request.getParameter("reservedChar2"),"");
String reservedChar3 = PublicUses_method.checkParam(request.getParameter("reservedChar3"),"");
String reservedChar4 = PublicUses_method.checkParam(request.getParameter("reservedChar4"),"");
String reservedChar5 = PublicUses_method.checkParam(request.getParameter("reservedChar5"),"");
String reservedChar6 = PublicUses_method.checkParam(request.getParameter("reservedChar6"),"");

//获取查询条件
String order = PublicUses_method.checkParam(request.getParameter("order")," userId ");//排序条件
int orderV = PublicUses_method.checkParam(request.getParameter("orderV"),0);//0正序 1倒叙
String sort = PublicUses_method.checkParam(request.getParameter("sort"),"");//sql语句
int pageIndex = PublicUses_method.checkParam(request.getParameter("pageIndex"),1);//第几页
int pageSize = PublicUses_method.checkParam(request.getParameter("pageSize"),10);//每页多少条
if(!"".equals(order)){
order = " order by "+order+" ";
if(orderV==1){
order = order +" desc ";
}
}

userName = userName.toLowerCase();
if(param==0){//判断登录 用户名小写
if ("".equals(userName)||"".equals(password)){
//request.getRequestDispatcher("/").forward(request, response);
obj.put("success",false);
obj.put("info","获取用户信息错误,用户名或密码为空:userName:"+userName+"---password:"+password);
System.out.println("获取用户信息错误,用户名或密码为空:userName:"+userName+"---password:"+password);
return;
}
params = new String[1];
paramValues = new String[1];
params[0] = "userName";
paramValues[0] = userName;
userInfoList = userInfoBo.selectUserInfo(params,paramValues,"","",1,2);
//System.out.println("---"+userName+"---"+userInfoList+"==="+userInfoList.size()+"---");
if(userInfoList!=null&&userInfoList.size()>0){
for(int i=0;userInfoList!=null&&i<userInfoList.size();i++){
userInfo = userInfoList.get(i);
//用户名和密码匹配 状态为0
if (userInfo!=null&&(new PublicUses_method()).encode(password).equals(userInfo.getPassword())) {//如果密码正确
objV.put("userId",userInfo.getUserId());
objV.put("userName",userInfo.getUserName());
objV.put("limits",userInfo.getLimits());
objV.put("status",userInfo.getStatus());
objV.put("type",userInfo.getType());

/*objV.put("reservedInt1",userInfo.getReservedInt1());
objV.put("reservedInt2",userInfo.getReservedInt2());
objV.put("reservedInt3",userInfo.getReservedInt3());
objV.put("reservedInt4",userInfo.getReservedInt4());
objV.put("reservedInt5",userInfo.getReservedInt5());
objV.put("reservedChar1",userInfo.getReservedChar1());
objV.put("reservedChar2",userInfo.getReservedChar2());
objV.put("reservedChar3",userInfo.getReservedChar3());
objV.put("reservedChar4",userInfo.getReservedChar4());
objV.put("reservedChar5",userInfo.getReservedChar5());*/

arr.add(objV);
obj.put("success",true);
obj.put("info",arr);
obj.put("count",1);
obj.put("pageIndex",pageIndex);
obj.put("pageSize",pageSize);
}else{
obj.put("success",false);
obj.put("info","密码错误");
System.out.println("密码错误");
}
}
}else{
obj.put("success",false);
obj.put("info","获取用户信息错误,根据用户名获取到的信息为空");
System.out.println("获取用户信息错误,根据用户名获取到的信息为空");
//obj.put("info",arr);
}
}else if(param==1){//1为获取满足条件的用户信息
StringBuffer paramS = new StringBuffer("1");
StringBuffer paramSV = new StringBuffer("1");
if(userId!=-1000){
paramS.append(",userId");
paramSV.append(","+userId);
}
if(!"".equals(userName)){
paramS.append(",userName");
paramSV.append(","+userName);
}
if(limits!=-1000){
paramS.append(",limits");
paramSV.append(","+limits);
}
if(status!=-1000){
paramS.append(",status");
paramSV.append(","+status);
}
if(type!=-1000){
paramS.append(",type");
paramSV.append(","+type);
}
if(creater!=-1000){
paramS.append(",creater");
paramSV.append(","+creater);
}
if(!"".equals(createTime)){
paramS.append(",createTime");
paramSV.append(","+createTime);
}
if(modifyer!=-1000){
paramS.append(",modifyer");
paramSV.append(","+modifyer);
}
if(!"".equals(modifyTime)){
paramS.append(",modifyTime");
paramSV.append(","+modifyTime);
}
if(reservedInt1!=-1000){
paramS.append(",reservedInt1");
paramSV.append(","+reservedInt1);
}
if(reservedInt2!=-1000){
paramS.append(",reservedInt2");
paramSV.append(","+reservedInt2);
}
if(reservedInt3!=-1000){
paramS.append(",reservedInt3");
paramSV.append(","+reservedInt3);
}
if(reservedInt4!=-1000){
paramS.append(",reservedInt4");
paramSV.append(","+reservedInt4);
}
if(reservedInt5!=-1000){
paramS.append(",reservedInt5");
paramSV.append(","+reservedInt5);
}
if(!"".equals(reservedChar1)){
paramS.append(",reservedChar1");
paramSV.append(","+reservedChar1);
}
if(!"".equals(reservedChar2)){
paramS.append(",reservedChar2");
paramSV.append(","+reservedChar2);
}
if(!"".equals(reservedChar3)){
paramS.append(",reservedChar3");
paramSV.append(","+reservedChar3);
}
if(!"".equals(reservedChar4)){
paramS.append(",reservedChar4");
paramSV.append(","+reservedChar4);
}
if(!"".equals(reservedChar5)){
paramS.append(",reservedChar5");
paramSV.append(","+reservedChar5);
}
if(!"".equals(reservedChar6)){
paramS.append(",reservedChar6");
paramSV.append(","+reservedChar6);
}
String[] paramT = paramS.toString().split(",");
String[] paramVT = paramSV.toString().split(",");
params = new String[paramT.length];
paramValues = new String[paramVT.length];
for(int i=0;i<paramT.length&&i<paramVT.length;i++){
params[i] = paramT[i];
paramValues[i] = paramVT[i];
}
//selectUserInfo(String[] param,String[] paramValue,String order,String sort,int pageIndex,int pageSize)
//userInfoList = userInfoBo.selectUserInfo(order,sort,pageIndex,pageSize);//8888888
userInfoList = userInfoBo.selectUserInfo(params,paramValues,order,sort,pageIndex,pageSize);
if(userInfoList!=null&&userInfoList.size()>0){
for(byte i=0;i<userInfoList.size();i++){
if(paramV==4||paramV==1||paramV==0){
objV.put("userId",userInfoList.get(i).getUserId());
objV.put("userName",userInfoList.get(i).getUserName());
objV.put("limits",userInfoList.get(i).getLimits());
objV.put("status",userInfoList.get(i).getStatus());
objV.put("type",userInfoList.get(i).getType());
}
if(paramV==4||paramV==1){
objV.put("createTime",userInfoList.get(i).getCreateTime());
objV.put("creater",userInfoList.get(i).getCreater());
objV.put("modifyTime",userInfoList.get(i).getModifyTime());
objV.put("modifyer",userInfoList.get(i).getModifyer());
}
if(paramV==4||paramV==3||paramV==2){
objV.put("reservedInt1",userInfoList.get(i).getReservedInt1());
objV.put("reservedInt2",userInfoList.get(i).getReservedInt2());
objV.put("reservedInt3",userInfoList.get(i).getReservedInt3());
objV.put("reservedInt4",userInfoList.get(i).getReservedInt4());
objV.put("reservedInt5",userInfoList.get(i).getReservedInt5());
objV.put("reservedChar1",userInfoList.get(i).getReservedChar1());
objV.put("reservedChar2",userInfoList.get(i).getReservedChar2());
objV.put("reservedChar3",userInfoList.get(i).getReservedChar3());
objV.put("reservedChar4",userInfoList.get(i).getReservedChar4());
objV.put("reservedChar5",userInfoList.get(i).getReservedChar5());
}
if(paramV==5||paramV==3){
objV.put("reservedChar6",userInfoList.get(i).getReservedChar6());
}
if(paramV==6){
objV.put("password",userInfoList.get(i).getPassword());
}
arr.add(objV);
}
obj.put("success",true);
//System.out.println(arr);
obj.put("info",arr);
num = userInfoBo.selectUserInfoCount(params,paramValues,sort);
obj.put("count",num);
obj.put("pageIndex",pageIndex);
obj.put("pageSize",pageSize);
}else{
obj.put("success",false);
obj.put("info","根据信息获取的用户信息为空");
System.out.println("根据信息获取的用户信息为空");
}
}
}catch(Exception e) {
e.printStackTrace();
}finally{
out.println(obj.toString());
if(out!=null){out.flush();out.close();}
}
}


}
在web.xml配置访问action即可
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值