首先数据库建表:
项目如何搭建?
我使用的是maven,导入相应的jar包,和依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.guicedee.services</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>62</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
搭建项目准备工作
- 搭建一个maven web项目
- 配置Smart tomcat
- 测试项目是否能跑起来
- 导入项目中需要的jar包,jsp,Servlet,MySQL驱动jstl,standard,junit等
- 构建项目包结构
6.编写实体类、ROM映射:表–类映射
7.编写基础公共类 - 数据库配置文件
2.编写数据库公共类driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/smbms?useSSL=true&useUnicode=true&characterEncoding=utf-8 username=root password=password
package com.happy.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* 操作数据库的基类--静态类
*/
public class BaseDao {
static{
//静态代码块,在类加载的时候执行
init();
}
private static String driver;
private static String url;
private static String username;
private static String password;
//初始化连接参数,从配置文件里获得
public static void init(){
Properties properties=new Properties();
String configFile = "db.properties";
InputStream is=BaseDao.class.getClassLoader().getResourceAsStream(configFile);
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");
}
//获取数据库连接
//static
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/**
* 查询操作
* @param connection
* @param sql
* @param params
*/
//static
//编写公共查询方法
public static ResultSet execute(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet,String sql,Object[] params) throws Exception{
//预编译的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();//新添加sql
return resultSet;
}
/**
* 更新操作
* @param connection
* @param sql
* @param params
* @throws Exception
*/
//static
//编写增删改公共方法
public static int execute(Connection connection,PreparedStatement preparedStatement,String sql,Object[] params) throws Exception{
int updateRows = 0;
preparedStatement = connection.prepareStatement(sql);
for(int i = 0; i < params.length; i++){
preparedStatement.setObject(i+1, params[i]);
}
updateRows = preparedStatement.executeUpdate();
return updateRows;
}
/**
* 释放资源
* @param connection
*/
//static
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement, ResultSet resultSet){
boolean flag = true;
if(resultSet != null){
try {
resultSet.close();
resultSet = null;//GC回收
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag = false;
}
}
if(preparedStatement != null){
try {
preparedStatement.close();
preparedStatement = null;//GC回收
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag = false;
}
}
if(connection != null){
try {
connection.close();
connection = null;//GC回收
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag = false;
}
}
return flag;
}
}
3.编写字符编码过滤器
servletRequest.setCharacterEncoding("utf-8");
servletResponse