SMBMS
数据库:
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目录下
代码show
pojo/Bill.java
package com.gongyi.pojo;
import java.math.BigDecimal;
import java.util.Date;
public class Bill {
private Integer 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;
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;
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;
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;
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 {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
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++) {
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();
resultSet = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
preparedStatement = null;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (connection != null) {
try {
connection.close();
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
<?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"
metadata-complete="true">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.gongyi.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
pom.xml
<?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.gongyi</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>5.1.47</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>
代码结构图:
静态资源下载地址
彩蛋
1.sql yog中的架构设计器
2.idea maven web配置tomcat fix时无artifactId可添加
解决:maven用idea自带的默认的就行,3.8.1不行
3.idea取消重复代码提醒:
|