SMBMS(狂神说)
数据库:
项目搭建
-
搭建一个maven web项目
-
配置Tomcat
-
导入项目中需要的jar包
<dependencies> <!--Servlet依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <!--Jsp依赖--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> </dependency> <!--连接数据库--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <!--JSTL表达式依赖--> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <!--Standard标签库依赖--> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies>
-
创建项目包结构
-
编写实体类
ORM映射 : 表-类映射
-
编写基础公共类
-
数据库配置文件
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8&useSSL=true username=root password=123456
-
编写数据库的公共类
import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; //操作数据库的公共类 public class BaseDao { private static String driver; private static String url; private static String username; private static String password; //静态代码块,类加载的时候就初始化 static { Properties properties = new Properties(); //通过类加载器读取对应的资源 InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties"); try { properties.load(is); } catch (IOException e) { e.printStackTrace(); } driver=properties.getProperty("driver"); url=properties.getProperty("url"); username=properties.getProperty("username"); password=properties.getProperty("password"); } //获取数据库连接 public static Connection getConnection(){ Connection connection=null; try { Class.forName(driver); connection = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return connection; } //编写查询公共类 public static ResultSet execute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement) throws SQLException { preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { //setObject占位符?从1开始,而数组从0开始 preparedStatement.setObject(i+1,params[i]); } resultSet = preparedStatement.executeQuery(); return resultSet; } //编写增删改公共类 public static int execute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement) throws SQLException { preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { //setObject占位符?从1开始,而数组从0开始 preparedStatement.setObject(i+1,params[i]); } int updateRows = preparedStatement.executeUpdate(); return updateRows; } //释放资源 public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){ boolean flag=true; if (resultSet!=null){ try { resultSet.close(); //GC回收 resultSet=null; } catch (SQLException e) { e.printStackTrace(); flag=false; } } if (preparedStatement!=null){ try { preparedStatement.close(); //GC回收 preparedStatement=null; } catch (SQLException e) { e.printStackTrace(); flag=false; } } if (connection!=null){ try { connection.close(); //GC回收 connection=null; } catch (SQLException e) { e.printStackTrace(); flag=false; } } return flag; } }
-
编写字符编码过滤器
package com.kuang.filter; import javax.servlet.*; import java.io.IOException; public class CharacterEncodingFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("utf-8"); servletResponse.setCharacterEncoding("utf-8"); servletResponse.setContentType("text/html"); filterChain.doFilter(servletRequest, servletResponse); } public void destroy() { } }
注册
<!--字符编码过滤器--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.kuang.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
-
-
导入静态资源
登录功能实现
-
编写前端页面
-
设置首页(欢迎页面)
<!--设置欢迎页面--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list>
-
编写dao层登录用户登录的结构
//得到要登录的用户 public User getLoginUser(Connection connection, String userCode) throws SQLException;
-
编写dao的实现类
public class UserDaoImpl implements UserDao{ //得到要登录的用户 @Override public User getLoginUser(Connection connection, String userCode) throws SQLException { PreparedStatement ps=null; ResultSet rs=null; User user=null; if (connection!=null){ String sql="select * from smbms_user where userCode=?"; Object[] params={ userCode}; rs = BaseDao.execute(connection, ps, rs, sql, params); if (rs.next()){ user=new User(); user.setId(rs.getInt("id")); user.setUserCode(rs.getString("userCode")); user.setUserName(rs.getString("userName")); user.setUserPassword(rs.getString("userPassword")); user.setGender(rs.getInt("gender")); user.setBirthday(rs.getDate("birthday")); user.setPhone(rs.getString("phone")); user.setAddress(rs.getString("address")); user.setUserRole(rs.getInt("userRole")); user.setCreatedBy(rs.getInt("createdBy")); user.setCreationDate(rs.getTimestamp("creationDate")); user.setModifyBy(rs.getInt("modifyBy")); user.setModifyDate(rs.getTimestamp("modifyDate")); } BaseDao.closeResource(null,ps,rs); } return user; } }
-
业务层接口
//用户登录 public User login(String userCode,String password);
-
业务层实现类
//实现Servlet复用
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
if (method!=null && method.equals("savepwd")){
//method.equals("savepwd")是判断是否进入了修改密码的页面
this.updatePwd(req,resp);
}else if(method!=null && method.equals("pwdmodify")){
this.pwdModify(req,resp);
}else if(method!=null && method.equals("query")){
this.query(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
//用户管理
public void query(HttpServletRequest req, HttpServletResponse resp){
//查询用户列表 从前端获取数据
String queryName = req.getParameter("queryname");
String temp = req.getParameter("queryUserRole");
String pageIndex = req.getParameter("pageIndex");
int queryUserRole =0;
//获取用户列表
UserServiceImpl userService = new UserServiceImpl();
//第一次走这个请求,一定是第一页,页面大小也是固定的
int pageSize=5;//页面大小 可以写在配置文件
int currentPageNo=1;//当前页
if (queryName==null){
queryName="";
}
if (temp!=null && !temp.equals("")){
queryUserRole=Integer.parseInt(temp);
}
if (pageIndex!=null){
currentPageNo=Integer.parseInt(pageIndex);
}
//获取用户的总数
int totalCount = userService.getUserCount(queryName, queryUserRole);
//分页
PageSupport pageSupport=new PageSupport();
pageSupport.setCurrentPageNo(currentPageNo);
pageSupport.setPageSize(pageSize);
pageSupport.setTotalCount(totalCount);
int totalPageCount = pageSupport.getTotalPageCount();
//控制首页和尾页
if (currentPageNo<1){
//如果页面小于1,则强制转为1
currentPageNo=1;
}else if (currentPageNo>totalPageCount){
//如果页面大于最后一页,则强制转为最后一页
currentPageNo=totalPageCount;
}
//获取用户列表展示
List<User> userList = userService.getUserList(queryName, queryUserRole, currentPageNo, pageSize);
req.setAttribute("userList",userList);
RoleServiceImpl roleService = new RoleServiceImpl();
List<Role> roleList = roleService.getRoleList();
req.setAttribute("roleList",roleList);
req.setAttribute("totalCount",totalCount);
req.setAttribute(