1.创建web项目
2.搭建项目
(1)引入jsp页面和静态资源
(2)引入依赖
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<!--json转换工具-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<!--tomcat依赖-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-api</artifactId>
<version>8.5.41</version>
<scope>provided</scope>
</dependency>
<!--jstl标签库依赖-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--junit测试单元依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--文件上传下载依赖-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
(3)创建数据库连接配置文件jdbc.properties
username=root
password=123456
url=jdbc:mysql://localhost:3306/practice
driverClassName=com.mysql.jdbc.Driver
# 最大连接数
maxActive=500
# 初始化连接数
initialSize=100
(4)导入数据库
(5)配置欢迎页面
修改web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<welcome-file-list>
<!--配置login.jsp为欢迎页面-->
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.创建数据库连接工具类
package com.xzt.prictice.utils;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.alibaba.druid.pool.DruidDataSourceFactory;
/**
* @Author xzt
* @Date 2022/4/29 23:27
* @Version 1.0
* @Introduce 数据源的工具类
*/
public class DataSourceUtils {
private static DataSource ds;
/**
* 在静态代码块中创建数据源对象
*/
static {
//创建属性对象
Properties info = new Properties();
try (//得到输入流
InputStream in = DataSourceUtils.class.getResourceAsStream("/jdbc.properties");) {
//加载到属性对象中
info.load(in);
ds = DruidDataSourceFactory.createDataSource(info);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 得到数据源
*/
public static DataSource getDataSource() {
return ds;
}
/**
* 从连接池中得到连接对象
*/
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 释放资源
*/
public static void close(Connection conn, Statement stmt, ResultSet rs) {
//关闭结果集
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭语句对象
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭连接对象
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭连接
*/
public static void close(Connection conn, Statement stmt) {
close(conn, stmt, null);
}
}
4.创建实体类
(1)用户实体类
package com.xzt.prictice.entity;
import java.util.Date;
/**
* @Author xzt
* @Date 2022/4/29 23:43
* @Version 1.0
* @Introduce 用户实体类
*/
public class User {
private Integer id;
private String username;
private String password;
private String name;
private String email;
private String pic;
private Integer gender;
private Date birthday;
private String address;
//省略getterAndsetter,toString等
}
(2)省份实体类
package com.xzt.prictice.entity;
/**
* @Author xzt
* @Date 2022/4/29 23:43
* @Version 1.0
* @Introduce 省份实体类
*/
public class Province {
private Integer id;
private String code;
private String name;
//省略getterAndsetter,toString等
}
(3)城市实体类
package com.xzt.prictice.entity;
/**
* @Author xzt
* @Date 2022/4/29 23:43
* @Version 1.0
* @Introduce 城市实体类
*/
public class City {
private Integer id;
private String code;
private String name;
private String provinceCode;
//省略getterAndsetter,toString等
}
(4)县区实体类
package com.xzt.prictice.entity;
/**
* @Author xzt
* @Date 2022/4/29 23:43
* @Version 1.0
* @Introduce 县区实体类
*/
public class Area {
private Integer id;
private String code;
private String name;
private String cityCode;
//省略getterAndsetter,toString等
}
页面资源和数据库sql脚本下载链接:
链接:https://pan.baidu.com/s/1zwqMDsMtsSBK2Nk8wfG-GA
提取码:6666
搭建项目到此结束,功能实现请看下一章。。。