数据库:
sql脚本:【通过sqlyog图形化界面操作,在历史记录中对应相应的sql,表的引擎默认为InnoDB,字符集和核对默认和数据库一致】
CREATE DATABASE `smbms`CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `smbms`;
CREATE TABLE `smbms`.`smbms_user` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `userCode` VARCHAR(15), `userName` VARCHAR(15), `userPassword` VARCHAR(15), `gender` INT(10), `birthday` DATE, `phone` VARCHAR(15), `address` VARCHAR(30), `userRole` BIGINT, `createBy` BIGINT, `creationDate` DATETIME, `modifyBy` BIGINT, `modifyDate` DATETIME, PRIMARY KEY (`id`) );
CREATE TABLE `smbms`.`smbms_role` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `roleCode` VARCHAR(30), `roleName` VARCHAR(30), `createBy` BIGINT, `creationDate` DATETIME, `modifyBy` BIGINT, `modifyDate` DATETIME, PRIMARY KEY (`id`) );
CREATE TABLE `smbms`.`smbms_address` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `contact` VARCHAR(30), `addressDesc` VARCHAR(30), `postCode` VARCHAR(30), `tel` VARCHAR(30), `createBy` BIGINT, `creationDate` DATETIME, `modifyBy` BIGINT, `modifyDate` DATETIME, `userId` BIGINT, PRIMARY KEY (`id`) );
CREATE TABLE `smbms`.`smbms_bill` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `billCode` VARCHAR(30), `productName` VARCHAR(30), `productDesc` VARCHAR(100), `productUnit` VARCHAR(10), `productCount` DECIMAL(20,2), `totalPrice` DECIMAL(20,2), `isPayment` INT, `createBy` BIGINT, `creationDate` DATETIME, `modifyBy` BIGINT, `modifyDate` DATETIME, `providerId` BIGINT, PRIMARY KEY (`id`) );
CREATE TABLE `smbms`.`smbms_provider` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `proCode` VARCHAR(30), `proName` VARCHAR(50), `proDesc` VARCHAR(100), `proContact` VARCHAR(50), `proPhone` VARCHAR(30), `proAddress` VARCHAR(100), `proFax` VARCHAR(30), `createBy` BIGINT, `creationDate` DATETIME, `modifyBy` BIGINT, `modifyDate` DATETIME, PRIMARY KEY (`id`) );
设计器:
项目如何搭建?
考虑使用不使用maven?依赖,jar
1.搭建一个maven web项目
1)根据idea 的maven模板创建一个web项目
2)pom中删除非必要的东西(只留gav坐标及打包方式)
3)补全目录(java和resources)
4)web.xml改为新版:4.0的
2.配置tomcat
3.测试项目是否能够跑起来
4.导入项目中会遇到的jar包
jsp,servlet,mysql驱动,jstl,standard…
5.创建项目包结构
6.编写实体类
ORM映射:表-类 映射
可以通过idea右侧数据库的mybatis-generator创建-生成实体未驼峰命名
实体类属性最好加注释
7.编写基础公共类
1.数据库配置文件
2.编写数据库的公共类
3.编写字符编码过滤器
8.导入静态资源
放在webapp目录下
pojo/Bill.java
package com.gongyi.pojo;
import java.math.BigDecimal;
import java.util.Date;
public class Bill {
private Integer id;//id
private String billCode;//账单编码
private String productName;//商品名称
private String productDesc;//商品描述
private String productUnit;//商品单位
private BigDecimal productCount;//商品数量
private BigDecimal totalPrice;//总金额
private Integer isPayment;//是否支付
private Long createBy;//创建者
private Date creationDate;//创建时间
private Long modifyBy;//更新者
private Date modifyDate;//更新时间
private Long providerId;//供应商id
private String providerName;//供应商名称
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBillCode() {
return billCode;
}
public void setBillCode(String billCode) {
this.billCode = billCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductUnit() {
return productUnit;
}
public void setProductUnit(String productUnit) {
this.productUnit = productUnit;
}
public BigDecimal getProductCount() {
return productCount;
}
public void setProductCount(BigDecimal productCount) {
this.productCount = productCount;
}
public BigDecimal getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(BigDecimal totalPrice) {
this.totalPrice = totalPrice;
}
public Integer getIsPayment() {
return isPayment;
}
public void setIsPayment(Integer isPayment) {
this.isPayment = isPayment;
}
public String getProviderName() {
return providerName;
}
public void setProviderName(String providerName) {
this.providerName = providerName;
}
public Long getCreateBy() {
return createBy;
}
public void setCreateBy(Long createBy) {
this.createBy = createBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Long getModifyBy() {
return modifyBy;
}
public void setModifyBy(Long modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public Long getProviderId() {
return providerId;
}
public void setProviderId(Long providerId) {
this.providerId = providerId;
}
}
pojo/Provider.java
package com.gongyi.pojo;
import java.util.Date;
public class Provider {
private Integer id;//id
private String proCode;//供应商编码
private String proName;//供应商名称
private String proDesc;//供应商描述
private String proContact;//供应商联系人
private String proPhone;//供应商电话
private String proAddress;//供应商地址
private String proFax;//供应商传真
private Long createBy;//创建者
private Date creationDate;//创建时间
private Long modifyBy;//更新者
private Date modifyDate;//更新时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProAddress() {
return proAddress;
}
public void setProAddress(String proAddress) {
this.proAddress = proAddress;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
public Long getCreateBy() {
return createBy;
}
public void setCreateBy(Long createBy) {
this.createBy = createBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Long getModifyBy() {
return modifyBy;
}
public void setModifyBy(Long modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
pojo/Role.java
package com.gongyi.pojo;
import java.util.Date;
public class Role {
private Integer id;//id
private String roleCode;//角色编码
private String roleName;//角色名称
private Long createBy;//创建者
private Date creationDate;//创建时间
private Long modifyBy;//更新者
private Date modifyDate;//更新时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRoleCode() {
return roleCode;
}
public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public Long getCreateBy() {
return createBy;
}
public void setCreateBy(Long createBy) {
this.createBy = createBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Long getModifyBy() {
return modifyBy;
}
public void setModifyBy(Long modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
pojo/User.java
package com.gongyi.pojo;
import java.util.Date;
public class User {
private Integer id;//id
private String userCode;//用户编码
private String userName;//用户名称
private String userPassword;//用户密码
private Integer gender;//性别
private Date birthday;//出生日期
private String phone;//电话
private String address;//地址
private Long userRole;//用户角色
private Long createBy;//创建者
private Date creationDate;//创建时间
private Long modifyBy;//更新者
private Date modifyDate;//更新时间
private Integer age;//年龄
private String userRoleName;//用户角色名称
public Integer getAge() {
Date date = new Date();
Integer age = date.getYear() - birthday.getYear();
return age;
}
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
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 Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date 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 Long getUserRole() {
return userRole;
}
public void setUserRole(Long userRole) {
this.userRole = userRole;
}
public Long getCreateBy() {
return createBy;
}
public void setCreateBy(Long createBy) {
this.createBy = createBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Long getModifyBy() {
return modifyBy;
}
public void setModifyBy(Long modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
dao/BaseDao.java
package com.gongyi.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);
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,占位符从1开始,但是我们的数组是从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,占位符从1开始,但是我们的数组是从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;
}
}
filter/CharacterEncodingFilter.java
package com.gongyi.filter;
import javax.servlet.*;
import java.io.IOException;
public class CharacterEncodingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void destroy() {
}
}
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
web.xml
<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"
metadata-complete="true">
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>com.gongyi.filter.CharacterEncodingFilterfilter-class>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
pom.xml
<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.0modelVersion>
<groupId>com.gongyigroupId>
<artifactId>smbmsartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.3.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>javax.servlet.jsp.jstlgroupId>
<artifactId>jstl-apiartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>1.1.2version>
dependency>
dependencies>
project>
代码结构图:
静态资源下载地址
1.sql yog中的架构设计器
2.idea maven web配置tomcat fix时无artifactId可添加
解决:maven用idea自带的默认的就行,3.8.1不行
3.idea取消重复代码提醒: