🥼JavaWeb~(陆)JDBC,JUnit,SMBMS项目
JDBC(复习)
JDBC 连接 🚀 进修 MySQL~(肆)
JUnit
在一个方法上标记 @Test
注解后,这个方法可以脱离 Main 直接运行.
依赖 (一般创建项目时自带)
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>例子:
import org.junit.Test;
public class JDBCTest {
public void test() {
System.out.println("123");
}
}
SMBMS
项目代码: https://github.com/Weidows/Java/tree/master/JavaWeb/SMBMS
可以拉取代码直接看,下面的笔记着重讲
从零开发的流程
我的这个项目应该是跑不起来,在 web,xml 中 filter 那里有个 bug,不过无大碍,知道怎么个原理就好.
搭建
Maven-webapp 项目,补全 src/resources,添加依赖,连接 Tomcat 和 MySQL,修改 web.xml 版本
依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>compile</scope>
</dependency>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSP依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- JSTL表达式依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Standard标签库 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jsp-api</artifactId>
<version>6.0.13</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>更改版本号:
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
</web-app>
后端项目结构
|> src/main |
连接配置文件
(resources/db.properties)
driverClassName=com.mysql.cj.jdbc.Driver |
ORM 映射
(编写 pojo 下的实体类)
初始化 SQL: https://github.com/Weidows/Java/blob/master/JavaWeb/SMBMS/smbms.sql
pojo: https://github.com/Weidows/Java/blob/master/JavaWeb/SMBMS/src/main/java/com/weidows/pojo
静态基本类
https://github.com/Weidows/Java/blob/master/JavaWeb/SMBMS/src/main/java/dao/BaseDao.java
package com.weidows.dao; |
题外话
一个编程书写习惯: 方法中首先把需要用到的变量提出
就像这里的三个变量,在一开始提出来,就像是类里面的 field,使用起来比较规范,容易排查
public User getLoginUser(Connection connection, String userCode) {
PreparedStatement pstm = null;
ResultSet resultSet = null;
User user = null;
try {
if (null != connection) {
String sql = "select * from smbms_user where userCode=?";
Object[] params = {userCode};
resultSet = BaseDao.execute(connection, pstm, resultSet, sql, params);
if (resultSet.next()) {
user = setData(resultSet);
}
}
BaseDao.closeResource(null, pstm, resultSet);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return user;
}
实现登录
在这里复现一下"登录"业务,其他功能以此类推.
这里着重
过程
(自上而下),具体怎么实现的业务建议结合源码来学习.
com/weidows/pojo/User.java
https://github.com/Weidows/Java/blob/master/JavaWeb/SMBMS/src/main/java/com/weidows/pojo/User.java
com/weidows/dao/user/UserDao.java
com/weidows/dao/user/UserDaoImpl.java
com/weidows/service/user/UserService.java
com/weidows/service/user/UserServiceImpl.java
前端页面
https://github.com/Weidows/Java/blob/master/JavaWeb/SMBMS/src/main/webapp/login.jsp
对应前端的常量类
对应 User 的 servlet 类
登录 servlet 类