项目搭建
1.搭建maven web项目
2.配置tomcat
3.测试项目能否成功启动
4.导入项目中可能会遇到的jar包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
5.创建项目包结构
6.编写实体类 ORM映射:表-类映射
User,Role,Bill,Provider
7.编写基础公共类
7.1 数据库配置文件database.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=true&user=root&password=123456
user=root
password=123456
7.2 编写数据库公共类
主要的工作为:
- 获取数据库连接
- 编写增删改查公共方法
- 关闭对应的资源
package xyz.yiui.dao;
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("database.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() throws ClassNotFoundException, SQLException {
Connection connection=null;
Class.forName(driver);
connection= DriverManager.getConnection(url,username,password);
return connection;
}
///编写查询公共方法 为了方便统一关闭,传入参数resultSet等
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++){
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++){
preparedStatement.setObject(i+1,params[i]);
}
int ans=preparedStatement.executeUpdate();
return ans;
}
///释放资源
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) {
boolean flag=true;
if(resultSet!=null){
try{
resultSet.close();
resultSet=null;///GC回收
}catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if(preparedStatement!=null){
try{
preparedStatement.close();
preparedStatement=null;///GC回收
}catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if(connection!=null){
try{
connection.close();
connection=null;///GC回收
}catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
return flag;
}
}
7.3 编写字符编码过滤器
package xyz.yiui.filter;
import javax.servlet.*;
import java.io.IOException;
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() {
}
}
在web.xml里注册
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>xyz.yiui.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
8.导入静态资源
登录功能实现
1.编写前端页面login.jsp
2.设置首页
在web.xml里配置
<!--设置首页-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
3.编写dao层 用户登录的接口
3.1 编写dao接口UserDao
package xyz.yiui.dao.user;
import xyz.yiui.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;
public interface UserDao {
//得到要登录的用户
public User getLoginUser(Connection connection,String userCode) throws SQLException;
}
3.2 编写对应的实现类UserDaoImpl
package xyz.yiui.dao.user;
import xyz.yiui.dao.BaseDao;
import xyz.yiui.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDaoImpl implements UserDao{
@Override
public User getLoginUser(Connection connection, String userCode) throws SQLException {
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
User user=null;
if(connection!=null){
String sql="select * from smbms_user where userCode=?";
Object[] params={
userCode};
resultSet= BaseDao.execute(connection,resultSet,preparedStatement,sql,params);
if(resultSet.next()){
user=new User();
user.setId(resultSet.getInt("id"));
user.setUserCode(resultSet.getString("userCode"));
user.setUserName(resultSet.getString("userName"));
user.setUserPassword(resultSet.getString("userPassword"));
user.setGender(resultSet.getInt("gender"));
user.setBirthday(resultSet.getDate("birthday"));
user.setPhone(resultSet.getString("phone"));
user.setAddress(resultSet.getString("address"));
user.setUserRole(resultSet.getInt("userRole"));
user.setCreatedBy(resultSet.getInt("createdBy"));
user.setCreationDate(resultSet.getTimestamp("creationDate"));
user.setModifyBy(resultSet.getInt("modifyBy"));
user.setModifyDate(resultSet.getTimestamp("modifyDate"));
}
BaseDao.closeResource(connection,preparedStatement,resultSet);
}
return user;
}
}
4.编写业务层service 用户登录
4.1 业务层接口
package xyz.yiui.service.user;
import xyz.yiui.pojo.User;
public interface UserService {
///用户登录
public User login(String userCode,String password);
}
4.2 业务层实现类
如果密码不正确,设user为空。
package xyz.yiui.service.user;
import org.junit.jupiter.api.Test;
import xyz.yiui.dao.BaseDao;
import xyz.yiui.dao.user.UserDao;
import xyz.yiui.dao.user.UserDaoImpl;
import xyz.yiui.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;
public class UserServiceImpl implements UserService{
///引入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();
///调用dao层具体操作
user= userDao.getLoginUser(connection,userCode);
if(!password.equals(user.getUserPassword())) user=null;
}catch (SQLException | ClassNotFoundException e){
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return user;///返回查询结果
}
@Test
public void test(){
UserServiceImpl userService=new UserServiceImpl();
User admin=userService.login("admin","111");
System.out.println(admin.getUserPassword());
}
}
4.3 编写测试类
@Test
public void test(){
UserServiceImpl userService=new UserServiceImpl();
User admin=userService.login("admin","111");
System.out.println(admin.getUserPassword());
}
5.编写控制层servlet
5.1 编写代码
package xyz.yiui.servlet.user;
import xyz.yiui.pojo.User;
import xyz.yiui.service.user.UserService;
import xyz.yiui.service.user.UserServiceImpl;
import xyz.yiui.util.Constans;
import javax