1、SMBMS(超市管理项目)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r5ny1ksQ-1632046547618)(SMBMS(超市管理项目)].assets/20200516122458676.png)
数据库:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jvt6c7Nf-1632046547622)(SMBMS(超市管理项目)].assets/20200516122532275.png)
项目如何搭建?
考虑是不是用maven? jar包,依赖
2、搭建项目准备工作
1. 搭建一个maven web 项目
2. 配置Tomcat
3. 测试项目是否能够跑起来
4. 导入项目中需要的jar包;
jsp,Servlet,mysql驱动jstl,stand…
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</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. 构建项目包结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cVis6hxK-1632046547626)(SMBMS(超市管理项目)].assets/202005161230352.png)
6. 编写实体类
ROM映射:表-类映射
7. 编写基础公共类
7.1 数据库配置文件(mysql5.xx和8.xx的编写有差异)
driver=com.mysql.jdbc.Driver
#在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8
url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
7.2 编写数据库的公共类
/**
* 操作数据库的公共类
*/
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 {
//预编译的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++) {
//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;
}
}
7.3 编写字符编码过滤器
public class CharacterEncodingFilter implements Filter{
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request,response);
}
public void destroy() {
}
}
<filter>
<filter-name>Filter</filter-name>
<filter-class>com.tree.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 4 导入静态资源
3、登录功能实现
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Cfke0NpH-1632046547634)(SMBMS(超市管理项目)].assets/20200516125301633.png)
1. 编写前端页面
2. 设置首页
2.1 设置欢迎首页
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
3. 编写dao层登录用户登录的接口
public interface UserDao {
//得到要登录的用户
public User getLoginUser(Connection connection,String userCode,String userPassword) throws SQLException;
}
<