项目搭建
1)搭建一个maven web项目
配web.xml的时候去tomcat下面找最新的
2)配置tomcat
3)测试项目是否能跑起来
4)导入项目中可能会遇到的jar包
jsp,servlet,mysql驱动,jstl,stand
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.2.1</version> <scope>provided</scope> </dependency>
5)创建项目包结构
6)编写实体类
ORM映射:表-类映射
7)编写基础公共类
7.1)数据库配置文件
7.2)编写数据库的公共类
1 package com.mine.dao; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.sql.*; 6 import java.util.Properties; 7 8 // 操作数据库的公共类 9 public class BaseDao { 10 private static String driver; 11 private static String url; 12 private static String username; 13 private static String password; 14 15 16 // 静态代码块,类加载的时候就初始化了 17 static { 18 Properties properties = new Properties(); 19 // 通过类加载器读取对应的资源 20 InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties"); 21 try { 22 properties.load(is); // 加载流对象 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 27 driver = properties.getProperty("driver"); 28 url = properties.getProperty("url"); 29 username = properties.getProperty("username"); 30 password = properties.getProperty("password"); 31 32 } 33 34 // 获取数据库的连接 35 public static Connection getConnection() { 36 Connection connection = null; // 提升作用域 37 try { 38 Class.forName(driver); 39 connection = DriverManager.getConnection(url, username, password); 40 } catch (Exception e) { 41 e.printStackTrace(); 42 } 43 return connection; 44 } 45 46 // 编写查询公共类,要统一关闭所以把它们提到参数里面 47 public static ResultSet execute(Connection connection,String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException { 48 // 预编译的sql在后面直接执行就可以了 49 preparedStatement = connection.prepareStatement(sql); // 预编译sql 50 for (int i = 0; i < params.length; i++) { 51 // setObject 52 preparedStatement.setObject(i + 1, params[i]); 53 } 54 resultSet = preparedStatement.executeQuery(); // 用了预编译,所以执行sql的时候不用传参 55 return resultSet; 56 } 57 58 59 // 编写增删改公共方法 60 61 62 // String sql 传进来要执行的sql语句 63 // Object[] params 传进来的参数 64 public static int execute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement) throws SQLException { 65 preparedStatement = connection.prepareStatement(sql); // 预编译sql 66 for (int i = 0; i < params.length; i++) { 67 // setObject 68 preparedStatement.setObject(i + 1, params[i]); 69 } 70 int updateRows = preparedStatement.executeUpdate(); 71 return updateRows; 72 } 73 74 75 // 关闭连接释放资源 76 public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) { 77 boolean flag = true; 78 if (resultSet != null) { 79 try { 80 resultSet.close(); 81 // 如果还存在,就让GC垃圾回收器回收 82 resultSet=null; 83 } catch (SQLException e) { // 如果关闭失败,让flag = false 84 e.printStackTrace(); 85 flag = false; 86 } 87 } 88 89 if (preparedStatement != null) { 90 try { 91 preparedStatement.close(); 92 // 如果还存在,就让GC垃圾回收器回收 93 preparedStatement=null; 94 } catch (SQLException e) { // 如果关闭失败,让flag = false 95 e.printStackTrace(); 96 flag = false; 97 } 98 } 99 100 if (connection != null) { 101 try { 102 connection.close(); 103 // 如果还存在,就让GC垃圾回收器回收 104 connection=null; 105 } catch (SQLException e) { // 如果关闭失败,让flag = false 106 e.printStackTrace(); 107 flag = false; 108 } 109 } 110 return flag; // 如果都释放成功返回true,只要有一个没释放就返回false 111 112 } 113 114 115 116 }
报错:
出现问题原因及解决:自己在写的时候把这个函数写进了static里面
7.3)编写字符编码过滤器
1 package com.mine.filter; 2 3 import javax.servlet.*; 4 import java.io.IOException; 5 6 public class CharracterEncodingFilter implements Filter { 7 public void init(FilterConfig filterConfig) throws ServletException { 8 9 } 10 11 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 12 servletRequest.setCharacterEncoding("utf-8"); 13 servletResponse.setCharacterEncoding("utf-8"); 14 filterChain.doFilter(servletRequest, servletResponse); 15 } 16 17 public void destroy() { 18 19 } 20 }
注册:
1 <!-- 字符编码过滤器--> 2 <filter> 3 <filter-name>CharracterEncodingFilter</filter-name> 4 <filter-class>com.mine.filter.CharracterEncodingFilter</filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name>CharracterEncodingFilter</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
8)导入静态资源