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

1、数据库连接池开机启动类

importjava.beans.PropertyVetoException;

importjava.util.Date;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importcom.mchange.v2.c3p0.ComboPooledDataSource;

publicclassDataConnectionPool_userInfoextendsHttpServlet{

publicDataConnectionPool_userInfo(){

super();

}

//获取数据库连接池

publicvoidinit()throwsServletException{

System.out.println((newDate())+"---获取数据库连接池c3p0用户信息");

//获取数据库连接池资源

ComboPooledDataSourcedataSource=newComboPooledDataSource();

//设置数据库连接池的驱动

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(PropertyVetoExceptione){

e.printStackTrace();

}finally{

DataSource_userInfo.setDataSource(dataSource);

//System.out.println("---打开数据库连接池c3p0考勤"+dataSource);

}

super.init();

}

//关闭数据库连接池

publicvoiddestroy(){

System.out.println((newDate())+"---关闭数据库连接池c3p0考勤");

super.destroy();

}

}

2、保存、获取数据库连接池的类

importcom.mchange.v2.c3p0.ComboPooledDataSource;

publicclassDataSource_userInfo{

publicstaticComboPooledDataSourcedataSource;

//设定数据连接池

publicstaticvoidsetDataSource(ComboPooledDataSourcedataSource){

DataSource_userInfo.dataSource=dataSource;

//System.out.println("---打开数据库连接池c3p0考勤");

//dataSource=dataSource;

}

//获取数据连接池中的连接

publicstaticComboPooledDataSourcegetDataSource(){

returndataSource;

}

}

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

importjava.io.IOException;

importjava.io.InputStream;

importjava.sql.Connection;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

importjava.util.Properties;

importcom.yx.publics.PublicUses_param;

publicclassDbHelper_userInfo{

//获取连接用户数据库信息的属性

publicstaticStringdriver="";

publicstaticStringurl="";

publicstaticStringusername="";

publicstaticStringpassword="";

static{

Propertiespro=newProperties();

Stringpath=PublicUses_param.DbPROPER_USERINFO_PATH;

InputStreamin=DbHelper_userInfo.class.getResourceAsStream(path);

try{

pro.load(in);

//myDataSource=DataSource_userInfo.getDataSource();

//StringaString="";

//myDataSource=BasicDataSourceFactory.createDataSource(pro);

}catch(IOExceptione){

e.printStackTrace();

System.out.print(e);

}

driver=pro.getProperty("driverClassName");

url=pro.getProperty("url");

userName=pro.getProperty("userName");

password=pro.getProperty("password");

//Stringaa="";

}

/**

*获取数据源

*

*@return

*/

/*publicstaticComboPooledDataSourcegetDataSource(){

returnmyDataSource;

}*/

/**

*获取连接

*

*@return

*@throwsSQLException

*/

publicstaticConnectiongetConnection()throwsSQLException{

//DataSource.getDataSource().getConnection();

returnDataSource_userInfo.getDataSource().getConnection();

//returnmyDataSource.getConnection();

}

/**

*关闭资源

*@paramrs

*@paramst

*@paramconn

*@throwsSQLException

*/

publicstaticvoidfree(ResultSetrs,Statementst,Connectionconn)throwsSQLException{

try{

if(rs!=null){

rs.close();

}

}catch(SQLExceptione){

thrownewSQLException();

}finally{

try{

if(st!=null){

st.close();

}

}catch(SQLExceptione){

thrownewSQLException();

}finally{

if(conn!=null){

try{

conn.close();

}catch(Exceptione){

thrownewSQLException();

}

}

}

}

}

}

3、DAO层

importjava.sql.Connection;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

importjava.util.ArrayList;

importjava.util.List;

importcom.yx.db.DbHelper_userInfo;

importcom.yx.exception.DaoException;

importcom.yx.exception.DaoParameterException;

publicclassDaoOperateTemplate_userInfo{

/**

*查找单个记录对象

*

*@paramsql

*@paramargs

*@paramrowMapper

*@return

*@throwsDaoException

*/

publicObjectfind(Stringsql,Object[]args,RowMapperrowMapper)throwsDaoException{

Connectionconn=null;

PreparedStatementps=null;

ResultSetrs=null;

try{

conn=DbHelper_userInfo.getConnection();

ps=conn.prepareStatement(sql);

for(inti=0;i<args.length;i++){

ps.setObject(i+1,args[i]);

}

rs=ps.executeQuery();

Objectobj=null;

if(rs.next()){

obj=rowMapper.mapRow(rs);

}

returnobj;

}catch(SQLExceptione){

thrownewDaoException(e.getMessage(),e);

}finally{

try{

DbHelper_userInfo.free(rs,ps,conn);

}catch(SQLExceptione){

thrownewDaoParameterException(e.getMessage(),e);

}

}

}

/**

*查找多条记录对象

*

*@paramsql

*@paramargs

*@paramrowMapper

*@return

*@throwsDaoException

*/

publicList<Object>Query(Stringsql,Object[]args,RowMapperrowMapper)throwsDaoException{

Connectionconn=null;

PreparedStatementps=null;

ResultSetrs=null;

List<Object>results=newArrayList<Object>();

try{

conn=DbHelper_userInfo.getConnection();

ps=conn.prepareStatement(sql);

for(inti=0;i<args.length;i++){

ps.setObject(i+1,args[i]);

}

rs=ps.executeQuery();

Objectobj=null;

while(rs.next()){

obj=rowMapper.mapRow(rs);

results.add(obj);

}

returnresults;

}catch(SQLExceptione){

thrownewDaoException(e.getMessage(),e);

}finally{

try{

DbHelper_userInfo.free(rs,ps,conn);

}catch(SQLExceptione){

thrownewDaoParameterException(e.getMessage(),e);

}

}

}

/**

*更新操作

*

*@paramsql

*@paramargs

*@paramisGeneralKey

*@throwsDaoException

*/

publicintupdate(Stringsql,Object[]args,booleanisGeneralKey)throwsDaoException{

Connectionconn=null;

PreparedStatementps=null;

ResultSetrs=null;

try{

conn=DbHelper_userInfo.getConnection();

ps=(isGeneralKey?conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS):conn.prepareStatement(sql));

for(inti=0;i<args.length;i++){

ps.setObject(i+1,args[i]);

}

returnps.executeUpdate();

}catch(SQLExceptione){

thrownewDaoException(e.getMessage(),e);

}finally{

try{

DbHelper_userInfo.free(rs,ps,conn);

}catch(SQLExceptione){

thrownewDaoParameterException(e.getMessage(),e);

}

}

}

}

4、公共的获取处理类

importjava.sql.ResultSet;

importjava.sql.SQLException;

publicinterfaceRowMapper{

/**

*映射接口

*@paramrs

*@return

*@throwsSQLException

*/

publicObjectmapRow(ResultSetrs)throwsSQLException;

}

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

importjava.io.IOException;

importjava.io.InputStream;

importjava.util.Properties;

importcom.yx.publics.PublicUses_param;

/**

*工厂类方法

*

*/

publicclassBoFactory_userInfo{

//privatestaticBoFactory_userInfoinstance=newBoFactory_userInfo();//懒汉法声明对象

privatestaticBoFactory_userInfoinstance=newBoFactory_userInfo();//懒汉法声明对象

privatestaticPropertiespro;//配置文件对象

privateBoFactory_userInfo(){

InputStreaminputStream=null;

try{

//初始化配置文件

pro=newProperties();

//采用类加载器方法读取配置文件信息到字节流对象,采用类加载灵活,不用写死

inputStream=BoFactory_userInfo.class.getClassLoader().getResourceAsStream(PublicUses_param.BOPROPER_BO_USER_INS);

//加载字节流对象

pro.load(inputStream);

}catch(IOExceptione){

thrownewExceptionInInitializerError(e);

}finally{

try{

if(inputStream!=null){

inputStream.close();

}

}catch(IOExceptione){

e.printStackTrace();

}

}

}

/**

*单例模式获取唯一实例

*

*@return

*/

publicstaticBoFactory_userInfogetInstance(){

returninstance;

}

/**

*根据配置文件的名字获取类的名字,采用反射机制获取其对象

*

*@paramKey

*@return

*/

publicObjectgetBO(StringKey)throwsException{

StringclassName=(String)pro.get(Key);

return(Class.forName(className).newInstance());

}

}

6、用于处理的接口类bo

importjava.util.List;

importcom.yx.exception.DaoException;

importcom.yx.vo.UserInfo;

publicinterfaceUserInfoBo{

/**

*添加方法

*@paramstudent

*@throwsDaoException

*/

publicintinsertUserInfo(UserInfouserInfo)throwsDaoException;

/**

*删除方法

*@paramstudent

*@throwsDaoException

*/

publicintdeleteUserInfo(UserInfouserInfo)throwsDaoException;

/**

*修改方法

*@paramstudent

*@throwsDaoException

*/

publicintmodifyUserInfo(UserInfouserInfo)throwsDaoException;

/**

*修改方法修改密码

*@paramstudent

*@throwsDaoException

*/

publicintmodifyUserInfoPass(UserInfouserInfo)throwsDaoException;

/**

*获取列表

*@return

*@throwsDaoException

*/

publicList<UserInfo>selectUserInfo(Stringorder,Stringsort,intpageIndex,intpageSize)throwsDaoException;

publicList<UserInfo>selectUserInfo(String[]param,String[]paramValue,Stringorder,Stringsort,intpageIndex,intpageSize)throwsDaoException;

//获取满足条件的信息条数

publicintselectUserInfoCount(String[]param,String[]paramValue,Stringsort)throwsDaoException;

}

7、实现处理的接口类bo

packagecom.yx.bo;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.util.List;

importcom.yx.dao.DaoOperateTemplate_userInfo;

importcom.yx.dao.RowMapper;

importcom.yx.exception.DaoException;

importcom.yx.publics.PublicUses_method;

importcom.yx.vo.UserInfo;

publicclassUserInfoBoImplimplementsUserInfoBo{

privateDaoOperateTemplate_userInfodaoTemplate=newDaoOperateTemplate_userInfo();

/**

*添加方法

*@paramstudent

*@throwsDaoException

*/

publicintinsertUserInfo(UserInfouserInfo)throwsDaoException{

//userInfo表有20个字段

//Stringsql="insertintouserInfo(userId,userName,password,limits,status,createTime,creater,modifyTime,"+

Stringsql="insertintouserInfo(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=newObject[]{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()};

returndaoTemplate.update(sql,args,false);

}

/*******************************************************

*删除方法

*@paramstudent

*@throwsDaoException

*/

publicintdeleteUserInfo(UserInfouserInfo)throwsDaoException{

return0;

}

/*******************************************************

*修改方法

*@paramstudent

*@throwsDaoException

*/

publicintmodifyUserInfo(UserInfouserInfo)throwsDaoException{

//userInfo表有20个字段//to_date('2004-05-0713:23:44','yyyy-mm-ddhh24:mi:ss')reservedChar1

Stringsql="updateuserInfosetuserName=?,limits=?,status=?,type=?,"+

"modifyTime=sysdate,modifyer=?,reservedInt1=?,reservedInt2=?,reservedInt3=?,reservedInt4=?,reservedInt5=?,"+

"reservedChar1=?,reservedChar2=?,reservedChar3=?,reservedChar4=?,reservedChar5=?,reservedChar6=?whereuserId=?";

Object[]args=newObject[]{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());*/

returndaoTemplate.update(sql,args,false);

}

/**

*修改方法修改密码

*@paramstudent

*@throwsDaoException

*/

publicintmodifyUserInfoPass(UserInfouserInfo)throwsDaoException{

Stringsql="updateuserInfosetpassword=?whereuserId=?";

Object[]args=newObject[]{userInfo.getPassword(),userInfo.getUserId()};

returndaoTemplate.update(sql,args,false);

}

/**

*获取列表

*@return

*@throwsDaoException

*/

@SuppressWarnings("unchecked")

publicList<UserInfo>selectUserInfo(Stringorder,Stringsort,intpageIndex,intpageSize)throwsDaoException{

String[]param=newString[0];

String[]paramValue=newString[0];

/*ListcourseList=selectUserInfo(param,paramValue);

returncourseList;*/

returnselectUserInfo(param,paramValue,order,sort,pageIndex,pageSize);

}

/**

*获取列表

*@return

*@throwsDaoException

*select*from(selectrownumrown,d.*fromyongxin_userInfo.userInfodwhere1=1and1=1orderbyuserId)

*where1=1andrownum<=10andrown>((1-1)*10)orderbyuserId;

*

*param为sql中的字段名paramValue为字段内容

*order为排序内容sort为纯sql语句

*pageIndex为第几页pageSize为每页多少条数据

*/

@SuppressWarnings("unchecked")

publicList<UserInfo>selectUserInfo(String[]param,String[]paramValue,Stringorder,Stringsort,intpageIndex,intpageSize)throwsDaoException{

//Stringsql="select*fromuserInfowhereid=?";

/*Stringsql="select*fromuserInfowhere1=1";

intparamLength=param.length;

intparamVLength=paramValue.length;

Object[]args=null;

//判断传输的参数长度是否相等参数长度是否为0

if(paramLength!=paramVLength||paramLength==0||paramVLength==0){

args=newObject[0];

}else{

for(inti=0;param!=null&&paramLength>0&&i<paramLength;i++){

sql=sql+"and"+param[i]+"=?";

}

args=newObject[paramLength];

for(inti=0;paramValue!=null&&paramVLength>0&&i<paramVLength;i++){

args[i]=paramValue[i];

}

}*/

Stringsql1="select*from(selectrownumrown,d.*fromyongxin_userInfo.userInfodwhere1=1";

Stringsql2="where1=1andrownum<="+pageSize+"andrown>(("+pageIndex+"-1)*"+pageSize+")";

intparamLength=param.length;

intparamVLength=paramValue.length;

Object[]args=null;

//判断传输的参数长度是否相等参数长度是否为0

if(paramLength!=paramVLength||paramLength==0||paramVLength==0){

args=newObject[0];

}else{

for(inti=0;param!=null&&paramLength>0&&i<paramLength;i++){

sql1=sql1+"and"+param[i]+"=?";

sql2=sql2+"and"+param[i]+"=?";

}

args=newObject[paramLength*2];

for(inti=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;

}

Stringsql=sql1+sql2;

System.out.println("获取用户信息的sql语句:"+sql);

ListcourseList=daoTemplate.Query(sql,args,newuserInfoRowMapper());

returncourseList;

}

/*

*获取满足条件的信息条数

*/

@SuppressWarnings("unchecked")

publicintselectUserInfoCount(String[]param,String[]paramValue,Stringsort)throwsDaoException{

//Stringsql="select*fromuserInfowhereid=?";

//Stringsql="selectcount(*)numfromuserInfowhere1=1";

Stringsql="select*fromuserInfowhere1=1";

intparamLength=0;

if(param!=null&&param.length>0){

paramLength=param.length;

}

intparamVLength=0;

if(paramValue!=null&&paramValue.length>0){

paramVLength=paramValue.length;

}

Object[]args=null;

//判断传输的参数长度是否相等参数长度是否为0

if(paramLength!=paramVLength||paramLength==0||paramVLength==0){

args=newObject[0];

}else{

for(inti=0;param!=null&&paramLength>0&&i<paramLength;i++){

sql=sql+"and"+param[i]+"=?";

}

args=newObject[paramLength];

for(inti=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);

ListcourseList=daoTemplate.Query(sql,args,newuserInfoRowMapper());

returncourseList.size();

}

/**

*内部匿名类

*

*@authorAdministrator

*

*/

classuserInfoRowMapperimplementsRowMapper{

publicObjectmapRow(ResultSetrs)throwsSQLException{

UserInfouserInfo=newUserInfo();

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"),""));

returnuserInfo;

}

}

}

8、action类

packagecom.yx.action;

importjava.io.IOException;

importjava.io.PrintWriter;

importjava.util.List;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importjavax.servlet.http.HttpSession;

importatg.taglib.json.util.JSONArray;

importatg.taglib.json.util.JSONObject;

importcom.yx.bo.BoFactory_userInfo;

importcom.yx.bo.UserInfoBo;

importcom.yx.publics.PublicUses_method;

importcom.yx.publics.PublicUses_param;

importcom.yx.vo.UserInfo;

@SuppressWarnings("serial")

publicclassGetUserInfoextendsHttpServlet{

@Override

protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

this.doPost(request,response);

}

@Override

protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

//super.doPost(request,response);

response.setContentType("text/html");

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

//设置要用到的必要信息

PrintWriterout=response.getWriter();

HttpSessionsession=request.getSession();

String[]params=null;

String[]paramValues=null;

JSONObjectobj=newJSONObject();

JSONArrayarr=newJSONArray();

JSONObjectobjV=newJSONObject();

List<UserInfo>userInfoList=null;

UserInfouserInfo=null;

intnum=0;

try{

@SuppressWarnings("unused")

//设置要用到的必要信息

UserInfoBouserInfoBo=(UserInfoBo)BoFactory_userInfo.getInstance().getBO("userInfoBo");

//获取查询的项目默认为-1

//-1转到参数错误页面或者404页面0为匹配用户名和密码是否正确1获取指定用户信息2为获取所有用户信息

intparam=PublicUses_method.checkParam(request.getParameter("param"),-1);

//判断获取用户信息的项目

//0为获取用户名、用户id、等级、状态、类型,1为获取除保留字段外的信息,2为获取保留字信息但无信息colb字段

//3为获取所有保留字段信息,4获取所有用户字段信息但无信息colb字段,5获取所有用户字段信息

intparamV=PublicUses_method.checkParam(request.getParameter("paramV"),0);

//登录的用户名

StringloginUserName=PublicUses_method.checkParam(session.getAttribute("userName"),"");

intloginUserId=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表的其他信息

intuserId=PublicUses_method.checkParam(request.getParameter("userId"),-1000);//当为0时为获取所有用户信息

StringuserName=PublicUses_method.checkParam(request.getParameter("userName"),"");

Stringpassword=PublicUses_method.checkParam(request.getParameter("password"),"");

doublelimits=PublicUses_method.checkParam(request.getParameter("limits"),-1000);

intstatus=PublicUses_method.checkParam(request.getParameter("status"),-1000);

inttype=PublicUses_method.checkParam(request.getParameter("type"),-1000);

intcreater=PublicUses_method.checkParam(request.getParameter("creater"),-1000);//创建人id

StringcreateTime=PublicUses_method.checkParam(request.getParameter("createTime"),"");

intmodifyer=PublicUses_method.checkParam(request.getParameter("modifyer"),-1000);//创建人id

StringmodifyTime=PublicUses_method.checkParam(request.getParameter("modifyTime"),"");

intreservedInt1=PublicUses_method.checkParam(request.getParameter("reservedInt1"),-1000);

intreservedInt2=PublicUses_method.checkParam(request.getParameter("reservedInt2"),-1000);

intreservedInt3=PublicUses_method.checkParam(request.getParameter("reservedInt3"),-1000);

intreservedInt4=PublicUses_method.checkParam(request.getParameter("reservedInt4"),-1000);

intreservedInt5=PublicUses_method.checkParam(request.getParameter("reservedInt5"),-1000);

StringreservedChar1=PublicUses_method.checkParam(request.getParameter("reservedChar1"),"");

StringreservedChar2=PublicUses_method.checkParam(request.getParameter("reservedChar2"),"");

StringreservedChar3=PublicUses_method.checkParam(request.getParameter("reservedChar3"),"");

StringreservedChar4=PublicUses_method.checkParam(request.getParameter("reservedChar4"),"");

StringreservedChar5=PublicUses_method.checkParam(request.getParameter("reservedChar5"),"");

StringreservedChar6=PublicUses_method.checkParam(request.getParameter("reservedChar6"),"");

//获取查询条件

Stringorder=PublicUses_method.checkParam(request.getParameter("order"),"userId");//排序条件

intorderV=PublicUses_method.checkParam(request.getParameter("orderV"),0);//0正序1倒叙

Stringsort=PublicUses_method.checkParam(request.getParameter("sort"),"");//sql语句

intpageIndex=PublicUses_method.checkParam(request.getParameter("pageIndex"),1);//第几页

intpageSize=PublicUses_method.checkParam(request.getParameter("pageSize"),10);//每页多少条

if(!"".equals(order)){

order="orderby"+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=newString[1];

paramValues=newString[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(inti=0;userInfoList!=null&&i<userInfoList.size();i++){

userInfo=userInfoList.get(i);

//用户名和密码匹配状态为0

if(userInfo!=null&&(newPublicUses_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);

}

}elseif(param==1){//1为获取满足条件的用户信息

StringBufferparamS=newStringBuffer("1");

StringBufferparamSV=newStringBuffer("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=newString[paramT.length];

paramValues=newString[paramVT.length];

for(inti=0;i<paramT.length&&i<paramVT.length;i++){

params[i]=paramT[i];

paramValues[i]=paramVT[i];

}

//selectUserInfo(String[]param,String[]paramValue,Stringorder,Stringsort,intpageIndex,intpageSize)

//userInfoList=userInfoBo.selectUserInfo(order,sort,pageIndex,pageSize);//8888888

userInfoList=userInfoBo.selectUserInfo(params,paramValues,order,sort,pageIndex,pageSize);

if(userInfoList!=null&&userInfoList.size()>0){

for(bytei=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(Exceptione){

e.printStackTrace();

}finally{

out.println(obj.toString());

if(out!=null){out.flush();out.close();}

}

}

}

在web.xml配置访问action即可

相关推荐