smbms项目准备
1. 创建一个Maven项目
2. 添加web结构
3. 配置Tomcat
4. 测试项目是否能够跑起来
5. 导入jar包
一开始应该导入servlet,mysql,jsp包
<dependencies>
<!--servlet包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--jsp包-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!--mysql包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
6.创建项目包结构
dao:主要写有关于数据库的java代码
Filter:编写过滤器代码
pojo:编写实体类代码,OMG映射:表和类的映射
service:业务代码
servlet:servlet代码,主要用于取参数
7.编写实体类(表和类的映射)
public class Bill {
private Integer id;
private String billCode;
private String productName;
private String productDesc;
private String productUnit;
private String productCount;
private String totallPrice;
private Integer isPayment;
private Integer createBy;
private Date createDate;
private String modifyBy;
private String modifyDate;
private Integer providerId;
public Bill(Integer id, String billCode, String productName, String productDesc, String productUnit, String productCount, String totallPrice, Integer isPayment, Integer createBy, Date createDate, String modifyBy, String modifyDate, Integer providerId) {
this.id = id;
this.billCode = billCode;
this.productName = productName;
this.productDesc = productDesc;
this.productUnit = productUnit;
this.productCount = productCount;
this.totallPrice = totallPrice;
this.isPayment = isPayment;
this.createBy = createBy;
this.createDate = createDate;
this.modifyBy = modifyBy;
this.modifyDate = modifyDate;
this.providerId = providerId;
}
public Bill() {
}
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 String getProductCount() {
return productCount;
}
public void setProductCount(String productCount) {
this.productCount = productCount;
}
public String getTotallPrice() {
return totallPrice;
}
public void setTotallPrice(String totallPrice) {
this.totallPrice = totallPrice;
}
public Integer getIsPayment() {
return isPayment;
}
public void setIsPayment(Integer isPayment) {
this.isPayment = isPayment;
}
public Integer getCreateBy() {
return createBy;
}
public void setCreateBy(Integer createBy) {
this.createBy = createBy;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getModifyBy() {
return modifyBy;
}
public void setModifyBy(String modifyBy) {
this.modifyBy = modifyBy;
}
public String getModifyDate() {
return modifyDate;
}
public void setModifyDate(String modifyDate) {
this.modifyDate = modifyDate;
}
public Integer getProviderId() {
return providerId;
}
public void setProviderId(Integer providerId) {
this.providerId = providerId;
}
}
8.编写基础公共类
8.1编写数据库公共类
public class baseDao {
private static String driver;
private static String url;
private static String username;
private static String password;
static{
Properties ps = new Properties();
//通过类加载器读取对应的资源
InputStream rs = baseDao.class.getClassLoader().getResourceAsStream("db.properties");
try {
ps.load(rs);
} catch (IOException e) {
e.printStackTrace();
}
driver=ps.getProperty("driver");
url=ps.getProperty("url");
username=ps.getProperty("username");
password=ps.getProperty("password");
}
//编写连接数据库
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (Exception throwables) {
throwables.printStackTrace();
}
return connection;
}
//编写查询公共类
public static ResultSet Query(Connection connection,String sql,Object[] params){
ResultSet resultSet=null;
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i <params.length ; i++) {
preparedStatement.setObject(i+1,params[i]);
}
resultSet = preparedStatement.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return resultSet;
}
//编写增删改公共类
public static int update(Connection connection,String sql,Object[] params) throws Exception {
PreparedStatement 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 closeResult(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) {
boolean flag = true;
if (resultSet != null) {
try {
resultSet.close();
//GC回收
resultSet = null;
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
//GC回收
preparedStatement = null;
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
if (connection != null) {
try {
connection.close();
//GC回收
connection = null;
} catch (SQLException throwables) {
throwables.printStackTrace();
flag = false;
}
}
return flag;
}
}
8.2编写字符过滤器
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");
servletResponse.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {
}
}
9导入网页静态资源
https://gitee.com/git_baboben/smbms?_from=gitee_search