构建项目Maven/jar
初次构建项目时,思考是不是通过maven创建,使用maven的化需要导入那些依赖;
如果不是使用maven创建项目的话,使用哪些些jar包。
检测验证项目
选择使用maven创建项目完成,可以使用模板,导入完成后 补充自己目录结构。
在java下建自己的目录地址,此次使用的是com.smbms01
,resources下建立资源路径;
在java的代码路径下,建立相应的pack包路径:dao、filter、pojo/entity、service、servlet
。
导入jar包/依赖
使用maven创建项目的情况,我们要在pom.xml中添加依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
junit: 测试依赖;
javax.servlet:服务依赖;
javax.servlet.jsp:画面jsp依赖;
javax.servlet.jsp.jstl:jstl依赖;
mysql-connector-java:mysql的驱动连接。
编写相应实体类
编写实体类与SQL中表的字段要一一对应:
User的表结构
实体类:
package com.smbms01.pojo;
import java.util.Date;
/**
* 用户表
*/
public class User {
/* id */
private Integer id;
/* 用户编码 */
private String userCode;
/* 用户名称 */
private String userName;
/* 用户密码 */
private String userPassword;
/* 用户性别 */
private String gender;
/* 出生日期 */
private String birthday;
/* 电话 */
private String phone;
/* 地址 */
private String address;
/* 用户角色 */
private String userRole;
/* 创建者 */
private String createBy;
/* 创建时间 */
private String createdDate;
/* 更新者 */
private String modifyBy;
/* 更新时间 */
private String modifyDate;
/* 年龄 */
private Integer age;
/* 用戶角色名称 */
private String userRoleName;
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getAge() {
Date date = new Date();
Integer age = Integer.valueOf(birthday) - Integer.valueOf(date.getDate());
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
this.userRole = userRole;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
public String getModifyBy() {
return modifyBy;
}
public void setModifyBy(String modifyBy) {
this.modifyBy = modifyBy;
}
public String getModifyDate() {
return modifyDate;
}
public void setModifyDate(String modifyDate) {
this.modifyDate = modifyDate;
}
}
在eclipse中,使用
alt+shift+s
,可以使用工具的快捷方式,生成相应的gettesetter方法。
在idea中 ,使用alt+inster
,在打开的框中,可以生产相应的gettersetter方法。
编写SQL公共基础类
package com.smbms01.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");
driver = properties.getProperty("url");
driver = properties.getProperty("username");
driver = 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,PreparedStatement preparedStatement,ResultSet resultSet) 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 updateRows = preparedStatement.executeUpdate();
return updateRows;
}
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
boolean flag = true;
if (resultSet !=null){
try {
resultSet.close();
preparedStatement = null;
} catch (SQLException e){
e.printStackTrace();
flag = false;
}
}
if (preparedStatement !=null){
try {
resultSet.close();
preparedStatement = null;
} catch (SQLException e){
e.printStackTrace();
flag = false;
}
}
if (connection !=null){
try {
resultSet.close();
preparedStatement = null;
} catch (SQLException e){
e.printStackTrace();
flag = false;
}
}
return true;
}
}
编写字符编码过滤器
package com.smbms01.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 servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
在这里插入代码片 <!--字符编码过滤器-->
<filter>
<filter-name>CharacterEnCodingFilter</filter-name>
<filter-class>com.smbms01.filter.CharacterEnCodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEnCodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
导入静态资源
需要的内容,js,图片,css样式等;