//根据用户名或者角色查询用户总数
public int getUserCount(Connection connection,String username,int userRole) throws SQLException;
UserDaoImpl
//根据用户名或者角色查询用户总数【最难理解的SQL】
public int getUserCount(Connection connection, String username, int userRole) throws SQLException {
PreparedStatement pstm = null;
ResultSet rs = null;
int count = 0;
if (connection!=null){
StringBuffer sql = new StringBuffer();
sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id");
ArrayList
UserService
//查询记录数
public int getUserCount(String username, int userRole);
UserServiceImpl
//查询记录数
public int getUserCount(String username, int userRole) {
Connection connection = null;
int count = 0;
try {
connection = BaseDao.getConnection();
count = userDao.getUserCount(connection, username, userRole);
} catch (SQLException e) {
e.printStackTrace();
} finally {
BaseDao.closeResource(connection,null,null);
}
return count;
}
2、获取用户列表
userdao
//通过条件查询-userList
public List getUserList(Connection connection, String username, int userRole, int currentPageNo, int pageSize) throws Exception;
userdaoImpl
public List getUserList(Connection connection, String username, int userRole, int currentPageNo, int pageSize) throws Exception {
PreparedStatement pstm = null;
ResultSet rs = null;
List userList = new ArrayList();
if (connection!=null){
StringBuffer sql = new StringBuffer();
sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");
List list = new ArrayList();//存放我们的参数
if (!StringUtils.isNullOrEmpty(username)){ //拼接sql语句
sql.append(" and u.userName like ?");
list.add("%"+username+"%"); //index:0
}
if (userRole>0){
sql.append(" and u.userRole = ?");
list.add(userRole); //index:1
}
//在数据库中,分页使用 limit startIndex,pageSize; 总数
//当前页 (当前页-1)*页面大小
//0,5 1 0 01234
//5,5 2 5 56789
//10,5 3 10
sql.append(" order by creationDate DESC limit ?,?");
currentPageNo = (currentPageNo-1)*pageSize;
list.add(currentPageNo);
list.add(pageSize);
//怎么把list转换为数组
Object[] params = list.toArray();
System.out.println("sql-->"+sql.toString()); //输出最后完整的SQL语句
rs = BaseDao.execute(connection, pstm, null, sql.toString(), params);
while (rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setUserCode(rs.getString("userCode"));
user.setUserName(rs.getString("userName"));
user.setGender(rs.getInt("gender"));
user.setBirthday(rs.getDate("birthday"));
user.setPhone(rs.getString("phone"));
user.setUserRole(rs.getInt("userRole"));
user.setUserRoleName(rs.getString("userRoleName"));
userList.add(user);
}
BaseDao.closeResource(null,pstm,rs);
}
return userList;
}
userService
//通过条件查询-userList
public List getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize);
userServiceImpl
public List getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
Connection connection = null;
List userList = null;
try {
connection = BaseDao.getConnection();
userList = userDao.getUserList(connection, queryUserName, queryUserRole,currentPageNo,pageSize);
} catch (Exception e) {
e.printStackTrace();
} finally {
BaseDao.closeResource(connection,null,null);
}
return userList;
}
3、获取角色操作
为了我们职责统一,可以把角色的操作单独放在一个包中,和POJO类对应
RoleDao
public interface RoleDao {
//获取角色列表
public List getRoleList(Connection connection) throws SQLException;
}
RoleDaoImpl
public List getRoleList(Connection connection) throws SQLException {
PreparedStatement pstm = null;
ResultSet resultSet = null;
ArrayList roleList = new ArrayList();
if (connection!=null){
String sql = "select * from smbms_role";
Object[] params = {};
resultSet = BaseDao.execute(connection, pstm, resultSet, sql, params);
while (resultSet.next()){
Role _role = new Role();
_role.setId(resultSet.getInt("id"));
_role.setRoleCode(resultSet.getString("roleCode"));
_role.setRoleName(resultSet.getString("roleName"));
roleList.add(_role);
}
BaseDao.closeResource(null,pstm,resultSet);
}
return roleList;
}
RoleService
//获取角色列表
public List getRoleList();
RoleServiceImpl
public class RoleServiceImpl implements RoleService{
//引入Dao
private RoleDao roleDao = null;
public RoleServiceImpl(){
roleDao = new RoleDaoImpl();
}
public List getRoleList() {
Connection connection = null;
List roleList = null;
try {
connection = BaseDao.getConnection();
roleList = roleDao.getRoleList(connection);
} catch (SQLException e) {
e.printStackTrace();
} finally {
BaseDao.closeResource(connection,null,null);
}
return roleList;
}
}
4、用户显示的Servlet
获取用户前端的数据(查询)
判断请求是否需要执行,看参数的值判断
为了实现分页,需要计算出当前页面和总页面,页面大小...
用户列表展示
返回前端
//重点,难点
public void query(HttpServletRequest req, HttpServletResponse resp){
//查询用户列表
//从前端获取数据:
String queryUserName = req.getParameter("queryname");
String temp = req.getParameter("queryUserRole");
String pageIndex = req.getParameter("pageIndex");
int queryUserRole = 0;
//获取用户列表
UserServiceImpl userService = new UserServiceImpl();
List userList = null;
//第一次走这个请求,一定是第一页,页面大小固定的;
int pageSize = 5;//可以把这个写到配置文件中,方便后期修改;
int currentPageNo = 1;
if (queryUserName == null){
queryUserName = ""; //不手动赋值会产生空指针异常
}
if (temp != null && !temp.equals("")){
queryUserRole = Integer.parseInt(temp); //给查询赋值!0,1,2,3
}
if (pageIndex != null){
currentPageNo = Integer.parseInt(pageIndex);
}
//获取用户的总数(分页:上一页,下一页的情况)
int totalCount = userService.getUserCount(queryUserName,queryUserRole);
//总页数支持
PageSupport pageSupport = new PageSupport();
pageSupport.setCurrentPageNo(currentPageNo);
pageSupport.setPageSize(pageSize);
pageSupport.setTotalCount(totalCount);
int totalPageCount = pageSupport.getTotalPageCount(); //总共有几页
//控制首页和尾页
//如果页面要小于1了,就显示第一页的东西
if (totalPageCount<1){
currentPageNo = 1;
}else if (currentPageNo>totalPageCount){ //当前页面大于了最后一项;
currentPageNo = totalPageCount;
}
//获取用户列表展示
userList = userService.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);
req.setAttribute("userList",userList);
RoleServiceImpl roleService = new RoleServiceImpl();
List roleList = roleService.getRoleList();
req.setAttribute("roleList",roleList);
req.setAttribute("totalCount",totalCount);
req.setAttribute("currentPageNo",currentPageNo);
req.setAttribute("totalPageCount",totalPageCount);
req.setAttribute("queryUserName",queryUserName);
req.setAttribute("queryUserRole",queryUserRole);
//返回前端
try {
req.getRequestDispatcher("userlist.jsp").forward(req,resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml&q
Mockito单元测试实例:
public class SettingServiceTest {
private List<PersonDTO> personList = new ArrayList<PersonDTO>();
@InjectMocks
private SettingPojoService settin
public class DeleteExtraSpace {
/**
* 题目:给定字符串,删除开始和结尾处的空格,并将中间的多个连续的空格合并成一个。
* 方法1.用已有的String类的trim和replaceAll方法
* 方法2.全部用正则表达式,这个我不熟
* 方法3.“重新发明轮子”,从头遍历一次
*/
public static v
今天早上打开MyEclipse时,自动关闭!弹出An error has occurred.See the log file错误提示!
很郁闷昨天启动和关闭还好着!!!打开几次依然报此错误,确定不是眼花了!
打开日志文件!找到当日错误文件内容:
--------------------------------------------------------------------------