项目经验: 程序框架、代码书写、bug调试
程序框架:
程序模型:MVC模型
M(model)-Dao->extends BaseDao(数据库操作)
CRUD代码
C(controller)-Servlet->url-class WebServlet(“/***”)(前端页面与数据库操作)
- 获得请求的数据
- 调用Dao层方法实现业务
- 输出返回数据(DML 0 -1 1/DQL Obj List<obj>)
V(view)-html
- ajax提交数据到web服务器
- 使用jquery实现页面的动态布局
- 表单验证操作
代码书写:业务流、数据流(前端)
- 添加功能
填写数据->单击提交按钮(click)->ajax
(url=”addMsg”\data{name:.val(),msg:.val()}\success{result==”1” layer 提示})
InsertMsgServlet->request.get…(“name”)(“msg”)
New Message msg(name,msg) ->MsgDao.insert(msg) ->out.print(ret)
Int MsgDao.insert(msg) insert(){excuteUpdate(“insert..”,[msg])}
Msg entity
*****开发的顺序是数据流逆序。
- 查看功能
$(ajax(url=”/showMsg”\success {result=List<Msg> for}))
ShowMsgServlet->List<Msg>MsgDao.showUser()->JSONArray(list) ->out()
MsgDao.ShowMsg()->{excuteQuery(“select ”)}
准备工作:导入jar包
导入静态原型
只要有表单就一定要在浏览器中校验
难点:1、前后端开发方式
自从参加了蓝桥培训以来,我做的第一个小项目留言板,使用的制作工具是idea,数据库是mysql
- 首先安装mysql数据库
安装直接按下一步就行,但是一定要记住自己设置的密码
- 建message表
如下图:
id不但要设置为主键,还要设置它自动递增。
username和msg要设置它们的字符集为utf8
createtime要设置其默认CURRENT_TIMESTAMP
就这样,一个数据库的表就建好了。
- 再所有的操作之前需要在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>org.lanqiao</groupId> <artifactId>message</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>message Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.3</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies> <build> <finalName>message</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
- 首先要在maven中建好目录
然后在entity下建一个message.java
package entity; import java.util.Date; public class message { private int id; private String username; private String msg; private Date createtime; public message(){} public message(String username, String msg) { this.username=username; this.msg=msg; } public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getUsername(){ return username; } public void setUsername(String username){ this.username=username; } public String getMsg(){ return msg; } public void setMsg(String msg){ this.msg=msg; } public Date getCreatetime(){ return createtime; } public void setCreatetime(Date createtime){ this.createtime=createtime; } }
五、Dao层,有BaseDao(这是老师写的),messageDao(我写的)
BaseDao中需要修改其数据库名称和之前设置的密码
老师写的BaseDao
package dao; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.sql.*; import java.util.ArrayList; import java.util.List; public abstract class BaseDao<T> { private static ComboPooledDataSource dataSource;//连接池 private Class<T> clazz; static { try { //加载配置文件,导入一个核心类。 dataSource = new ComboPooledDataSource(); } catch (Exception e) { e.printStackTrace(); } } //反射获得clazz public BaseDao() { clazz = (Class<T>) ((ParameterizedType) this.getClass() .getGenericSuperclass()).getActualTypeArguments()[0]; } //提供获得数据源 public static ComboPooledDataSource getDataSource() { return dataSource; } //提供获得连接 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } //关闭资源 public void closeAll(Connection conn, Statement stmt, ResultSet rs) { try { if </