数据库:
搭建项目
搭建一个maven web项目
配置Tomcat
启动项目
导入项目中需要的jar包
jsp,Servlet,mysql驱动jstl,stand…
一定要是maven项目
pom.xml
构建项目包结构
编写实体类
pojo
ROM映射:表-类映射
- 实体类名,尽量和数据库中的表名一一对应
- 实体类中的属性对应数据库表中的字段,相关的命名最好也一一对应
- 实体类内方法主要有,setter、getter方法,用于设置、获取数据
- 实体类属性一般为private类型,方法为public类型 实体类应该有,无参、有参构造方法
编写基础公共类
数据库配置文件(db.properties)
数据库配置文件(mysql5.xx和8.xx的编写有差异)
driver=com.mysql.cj.jdbc.Driver
#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
url=jdbc:mysql://localhost:3306/smbms?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=root
com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别
com.mysql.jdbc.Driver
是 mysql-connector-java 5中的;com.mysql.cj.jdbc.Driver
是 mysql-connector-java 6中的。
指定时区serverTimezone
在设定时区的时候,如果设定serverTimezone=UTC,会比中国时间早8个小时,如果在中国,可以选择Asia/Hongkong
编写数据库的公共类
编写字符编码过滤器
/**
* 字符编码过滤器
*/
public class CharacterEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
导入静态资源
登录功能实现
编写前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>系统登录 - 超市订单管理系统</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/css/style.css" />
<script type="text/javascript">
/* if(top.location!=self.location){
top.location=self.location;
} */
</script>
</head>
<body class="login_bg">
<section class="loginBox">
<header class="loginHeader">
<h1>超市订单管理系统</h1>
</header>
<section class="loginCont">
<form class="loginForm" action="${pageContext.request.contextPath}/login.do" name="actionForm" id="actionForm" method="post" >
<div class="info">${error}</div>
<div class="inputbox">
<label>用户名:</label>
<input type="text" class="input-text" id="userCode" name="userCode" placeholder="请输入用户名" required/>
</div>
<div class="inputbox">
<label>密码:</label>
<input type="password" id="userPassword" name="userPassword" placeholder="请输入密码" required/>
</div>
<div class="subBtn">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</div>
</form>
</section>
</section>
</body>
</html>
设置欢迎首页
<!--设置欢迎页面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
项目启动完成弹出首页为登录页login.jsp
编写dao层登录用户登录的接口
public interface UserDao {
/**
*得到要登录的用户
* 通过userCode获取User
*/
public User getLoginUser(Connection connection, String userCode) throws SQLException;
}
编写dao层接口的实现类
/**
* dao层抛出异常,让service层去捕获处理
*/
public class UserDaoImpl extends HttpServlet implements UserDao{
/**持久层只做查询数据库的内容
* 得到要登录的用户
* */
@Override
public User getLoginUser(Connection connection, String userCode) throws SQLException {
//准备三个对象
PreparedStatement pstm = null;
ResultSet rs = null;
User user = null;
//判断是否连接成功
if (null != connection) {
String sql = "select * from smbms_user where userCode=?";
Object[] params = {
userCode};
rs = BaseDao.execute(connection, pstm, 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, pstm, rs);
}
return user;
}
}
业务层service接口
public interface UserService {
//用户登录
public User login(String userCode, String password);
}
业务层service接口实现类
/**
* service层捕获异常,进行事务处理
* 事务处理:调用不同dao的多个方法,必须使用同一个connection(connection作为参数传递)
* 事务完成之后,需要在service层进行connection的关闭,在dao层关闭(PreparedStatement和ResultSet对象)
*/
public class UserServiceImpl implements UserService {
//业务层都会调用dao层,所以我们要引入Dao层
private UserDao userDao;
public UserServiceImpl() {
userDao = new UserDaoImpl();
}
//用户登录
@Override
public User login(String userCode, String password) {
Connection connection = null;
User user = null;
try {
connection = BaseDao.getConnection();
//通过业务层调用对应的数据库操作
user = userDao.getLoginUser(connection, userCode);
} catch (Exception e) {
//e.printStackTrace();
} finally {
BaseDao.closeResource(connection, null, null);
}
return user;
}