文章目录
项目架构
数据库
项目搭建
搭建一个maven web项目
配置Tomcat
测试项目是否能够跑起来
导入项目中需要用的jar包
- jsp
- servlet
- mysql
- jstl
- standard
创建项目包结构
编写实体类
ORM映射:表-类映射
Bill表实体类
packagecom.yang.pojo;importjava.math.BigDecimal;importjava.util.Date;publicclassBill{privateInteger id;privateString billCode;privateString productName;privateString productDesc;privateString productUnit;privateBigDecimal productCount;privateBigDecimal totalPrice;privateInteger isPayment;privateInteger createdBy;privateDate creationDate;privateInteger modifyBy;privateDate modifyDate;privateInteger providerName;publicIntegergetId(){return id;}publicvoidsetId(Integer id){this.id = id;}publicStringgetBillCode(){return billCode;}publicvoidsetBillCode(String billCode){this.billCode = billCode;}publicStringgetProductName(){return productName;}publicvoidsetProductName(String productName){this.productName = productName;}publicStringgetProductDesc(){return productDesc;}publicvoidsetProductDesc(String productDesc){this.productDesc = productDesc;}publicStringgetProductUnit(){return productUnit;}publicvoidsetProductUnit(String productUnit){this.productUnit = productUnit;}publicBigDecimalgetProductCount(){return productCount;}publicvoidsetProductCount(BigDecimal productCount){this.productCount = productCount;}publicBigDecimalgetTotalPrice(){return totalPrice;}publicvoidsetTotalPrice(BigDecimal totalPrice){this.totalPrice = totalPrice;}publicIntegergetIsPayment(){return isPayment;}publicvoidsetIsPayment(Integer isPayment){this.isPayment = isPayment;}publicIntegergetCreatedBy(){return createdBy;}publicvoidsetCreatedBy(Integer createdBy){this.createdBy = createdBy;}publicDategetCreationDate(){return creationDate;}publicvoidsetCreationDate(Date creationDate){this.creationDate = creationDate;}publicIntegergetModifyBy(){return modifyBy;}publicvoidsetModifyBy(Integer modifyBy){this.modifyBy = modifyBy;}publicDategetModifyDate(){return modifyDate;}publicvoidsetModifyDate(Date modifyDate){this.modifyDate = modifyDate;}publicIntegergetProviderName(){return providerName;}publicvoidsetProviderName(Integer providerName){this.providerName = providerName;}}
Provider表实体类
packagecom.yang.pojo;importjava.util.Date;publicclassProvider{privateInteger id;privateString proCode;privateString proName;privateString proDesc;privateString proContact;privateString proPhone;privateString proAddress;privateString proFax;privateInteger createdBy;privateDate creationDate;privateDate modifyDate;privateInteger modifyBy;publicIntegergetId(){return id;}publicvoidsetId(Integer id){this.id = id;}publicStringgetProCode(){return proCode;}publicvoidsetProCode(String proCode){this.proCode = proCode;}publicStringgetProName(){return proName;}publicvoidsetProName(String proName){this.proName = proName;}publicStringgetProDesc(){return proDesc;}publicvoidsetProDesc(String proDesc){this.proDesc = proDesc;}publicStringgetProContact(){return proContact;}publicvoidsetProContact(String proContact){this.proContact = proContact;}publicStringgetProPhone(){return proPhone;}publicvoidsetProPhone(String proPhone){this.proPhone = proPhone;}publicStringgetProAddress(){return proAddress;}publicvoidsetProAddress(String proAddress){this.proAddress = proAddress;}publicStringgetProFax(){return proFax;}publicvoidsetProFax(String proFax){this.proFax = proFax;}publicIntegergetCreatedBy(){return createdBy;}publicvoidsetCreatedBy(Integer createdBy){this.createdBy = createdBy;}publicDategetCreationDate(){return creationDate;}publicvoidsetCreationDate(Date creationDate){this.creationDate = creationDate;}publicDategetModifyDate(){return modifyDate;}publicvoidsetModifyDate(Date modifyDate){this.modifyDate = modifyDate;}publicIntegergetModifyBy(){return modifyBy;}publicvoidsetModifyBy(Integer modifyBy){this.modifyBy = modifyBy;}}
Role表实体类
packagecom.yang.pojo;importjava.util.Date;publicclassRole{privateInteger id;privateString roleCode;privateString roleName;privateInteger createdBy;privateDate creationDate;privateInteger modifyBy;privateDate modifyDate;publicIntegergetId(){return id;}publicvoidsetId(Integer id){this.id = id;}publicStringgetRoleCode(){return roleCode;}publicvoidsetRoleCode(String roleCode){this.roleCode = roleCode;}publicStringgetRoleName(){return roleName;}publicvoidsetRoleName(String roleName){this.roleName = roleName;}publicIntegergetCreatedBy(){return createdBy;}publicvoidsetCreatedBy(Integer createdBy){this.createdBy = createdBy;}publicDategetCreationDate(){return creationDate;}publicvoidsetCreationDate(Date creationDate){this.creationDate = creationDate;}publicIntegergetModifyBy(){return modifyBy;}publicvoidsetModifyBy(Integer modifyBy){this.modifyBy = modifyBy;}publicDategetModifyDate(){return modifyDate;}publicvoidsetModifyDate(Date modifyDate){this.modifyDate = modifyDate;}}
User表实体类
packagecom.yang.pojo;importjava.util.Date;publicclassUser{privateInteger id;privateString userCode;privateString userName;privateString userPassword;privateInteger gender;privateDate birthday;privateString phone;privateString address;privateInteger userRole;privateInteger createdBy;privateDate creationDate;privateInteger modifyBy;privateDate modifyDate;privateInteger age;privateString userRoleName;publicIntegergetAge(){Date date =newDate();Integer age = date.getYear()-birthday.getYear();return age;}publicStringgetUserRoleName(){return userRoleName;}publicvoidsetUserRoleName(String userRoleName){this.userRoleName = userRoleName;}publicIntegergetId(){return id;}publicvoidsetId(Integer id){this.id = id;}publicStringgetUserCode(){return userCode;}publicvoidsetUserCode(String userCode){this.userCode = userCode;}publicStringgetUserName(){return userName;}publicvoidsetUserName(String userName){this.userName = userName;}publicStringgetUserPassword(){return userPassword;}publicvoidsetUserPassword(String userPassword){this.userPassword = userPassword;}publicIntegergetGender(){return gender;}publicvoidsetGender(Integer gender){this.gender = gender;}publicDategetBirthday(){return birthday;}publicvoidsetBirthday(Date birthday){this.birthday = birthday;}publicStringgetPhone(){return phone;}publicvoidsetPhone(String phone){this.phone = phone;}publicStringgetAddress(){return address;}publicvoidsetAddress(String address){this.address = address;}publicIntegergetUserRole(){return userRole;}publicvoidsetUserRole(Integer userRole){this.userRole = userRole;}publicIntegergetCreatedBy(){return createdBy;}publicvoidsetCreatedBy(Integer createdBy){this.createdBy = createdBy;}publicDategetCreationDate(){return creationDate;}publicvoidsetCreationDate(Date creationDate){this.creationDate = creationDate;}publicIntegergetModifyBy(){return modifyBy;}publicvoidsetModifyBy(Integer modifyBy){this.modifyBy = modifyBy;}publicDategetModifyDate(){return modifyDate;}publicvoidsetModifyDate(Date modifyDate){this.modifyDate = modifyDate;}}
编写基础公共类
数据库配置文件
```java
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3308?useUnicode=true&characterEncoding=utf- 8&useSSL=false
username=root
password=123456
### 编写数据库的公共类
packagecom.yang.dao;importjava.io.IOException;importjava.io.InputStream;importjava.sql.*;importjava.util.Properties;//操作数据库的公共类publicclassBaseDao{privatestaticString driver;privatestaticString url;privatestaticString username;privatestaticString password;//静态代码块,类加载的时候就初始化static{Properties properties =newProperties();//通过类加载器读取对应的资源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");}//获取数据库的链接publicstaticConnectiongetConnection(){Connection connection =null;try{Class.forName(driver);
connection =DriverManager.getConnection(url, username, password);}catch(Exception e){
e.printStackTrace();}return connection;}//编写查询公共类publicstaticResultSetexecute(Connection connection,Object[] params,String sql,ResultSet resultSet,PreparedStatement preparedStatement)throwsSQLException{//预编译的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;}//编写增删改工具类publicstaticintexecute(Connection connection,Object[] params,String sql,PreparedStatement preparedStatement)throwsSQLException{
preparedStatement = connection.prepareStatement(sql);for(int i =0; i < params.length; i++){//setObject,占位符从1开始,但是我们的数组是从0开始!
preparedStatement.setObject(i+1,params[i]);}int updateRows = preparedStatement.executeUpdate();return updateRows;}//释放资源publicstaticbooleancloseResource(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;}}
### 编写字符编码过滤器
packagecom.yang.filter;importjavax.servlet.*;importjava.io.IOException;publicclassCharacterEncodingFilterimplementsFilter{publicvoidinit(FilterConfig filterConfig)throwsServletException{}publicvoiddoFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain)throwsIOException,ServletException{
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);}publicvoiddestroy(){}}
<filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>com.yang.filter.CharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
```
导入静态资源
版权归原作者 Violent-Ayang 所有, 如有侵权,请联系我们删除。