利用maven模板写一个超市订单管理系统(待更)
Posted 凸然猿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用maven模板写一个超市订单管理系统(待更)相关的知识,希望对你有一定的参考价值。
步骤:
1.创建并配置maven,(这里我用的是maven3.6.1版本,没用idea自带maven)
1.1配置web.xml版本(因为使用idea模板时默认配置的是web 1版本),在这里我自定义的是web 4版本
<?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"> </web-app>
1.2 导包(用多少导多少后期逐渐的不全)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.king</groupId> <artifactId>smbms</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> </project>
2.配置tomcat(版本 tomcat9)
注意:设置 VM options:-Dfile.encoding=UTF-8(统一,防止控制台中文乱码)
3.创建项目包结构
4.编写实体类 pojo
ORM映射:表-->类映射
5.编写基础公共类
5.1 数据库配置文件
注意:我的是mysql 8 的配置文件按,如果是5.x的会略有改动,如何改?,可以去看我之前写的关于mysql8.x和5.x相互转换那篇
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8&useSSL=false username=root password=123456
5.2 编写:操作数据库公共类
package com.king.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"); url=properties.getProperty("url"); username=properties.getProperty("username"); password=properties.getProperty("password"); } //获取数据库的来链接 public static Connection getConnection(){ Connection connection=null; try { Class.forName(driver); //DriverManager,驱动管理类 connection = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return connection; } //编写查询公共类 public static ResultSet execute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement ) throws SQLException { //预编译的sql后边无需传参 preparedStatement = connection.prepareStatement(sql); for (int i=0;i<params.length;i++){ //setObject,占位符从一开始,但数组从0开始 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++){ //setObject,占位符从一开始,但数组从0开始 preparedStatement.setObject(i+1,params[i]); } int updateRows= preparedStatement.executeUpdate(); return updateRows; } //关闭连接,释放资源 public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){ boolean flag = true; if(resultSet!=null) { try { resultSet.close(); //GC回收 resultSet = null; } catch (SQLException e) { e.printStackTrace(); flag = false; } } if(preparedStatement!=null) { try { preparedStatement.close(); //GC回收 preparedStatement = null; } catch (SQLException e) { e.printStackTrace(); flag = false; } } if(connection!=null) { try { connection.close(); //GC回收 connection = null; } catch (SQLException e) { e.printStackTrace(); flag = false; } } return flag; } }
5.3 编写字符编码过滤器(过滤所有页面)
类
import javax.servlet.*; import java.io.IOException; public class CharacterEncodingFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("utf-8"); servletResponse.setCharacterEncoding("utf-8"); filterChain.doFilter(servletRequest,servletResponse); } public void destroy() { } }
web
<!--字符编码过滤器--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.king.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
finally:
导入静态资源;比如css,js,图片等....
(想要静态资源的,评论留邮箱)
以上是关于利用maven模板写一个超市订单管理系统(待更)的主要内容,如果未能解决你的问题,请参考以下文章