1.搭建maven项目
之后一路创建。
将pom删除干净
web.xml
创建这两个文件
第一部分结束。。。==================================
2.配置tomcat
点击
创建
3.测试tomcat
成功启动
4.导入项目中会用的jar包
javax.servlet servlet-api2.5 javax.servlet.jsp jsp-api2.1 com.microsoft.sqlserver sqljdbc44.0 javax.servlet.jsp.jstl jstl-api1.2 taglibs standard1.1.2
5. 创建项目结构
6. 编写实体类(pojo文件)
ORM映射:表和java类映射
这里我没有自己手打。直接使用的github。因为是简单的把数据库元素写成java,可以直接搬。不要浪费时间。
对应数据库表中的四个数据表的元素
P2hemia/SMBMS: 狂神说JavaWeb视频项目SMBMS (github.com)
7.编写基础公共类
因为sqlserver有问题,我就下载了mysql进行使用。以下都是mysql的jdbc
1. 数据库配置文件
2.编写数据库的公共类
package com.Li.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("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);//在try/catch中无法返回,所以需要提升作用域
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
//编写查询公共类
public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException {
//预编译的sql在后面直接执行就可以了
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++) {
preparedStatement.setObject(i+1, params[i]);
}
int updateRows = preparedStatement.executeUpdate();
return updateRows;
}
//释放资源
public static boolean closeResource(Connection connection,ResultSet resultSet, PreparedStatement preparedStatement){
boolean flag = true;
if (resultSet!=null){
try {
resultSet.close();
//GC回收
resultSet = null;
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
if (connection!=null){
try {
connection.close();
//GC回收
connection = null;
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
if (preparedStatement!=null){
try {
preparedStatement.close();
//GC回收
preparedStatement = null;
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
return flag;
}
}
3.编写字符过滤器(简单)
package com.Li.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");
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy() {
}
}
web.xml配置
CharacterEncodingFilter
com.Li.filter.CharacterEncodingFilter
CharacterEncodingFilter
/*
8.导入静态资源
资源在上面的github中。复制到web目录下面



