1.MVC三层架构
M:Model>>>模型层:封装数据Java Bean;
V:View>>>视图层:jsp专注显示;
C:Controller>>>控制层:Servlet接收页面请求,找模型层处理,然后响应数据出去;
**好处:**分层,逻辑清楚,便于维护,扩展方便;【适用大型项目】
**缺点:**小型项目,代码稍多;
三层架构与MVC的关系
2.实战案例:学生管理系统
整个项目工程布局图
环境搭建,准备工作
第一步:1.数据库建表,添加数据;
mysql> create table stu(
-> id int primary key not null auto_increment,
-> name varchar(20),
-> gender varchar(2),
-> phone varchar(12),
-> birthday date,
-> hobby varchar(50),
-> info varchar(200)
-> );
Query OK, 0 rows affected
需要导入必要的jar包
2.数据库连接c3p0配置文件【c3p0-config.xml】
一般只需修改默认配置即可
<c3p0-config>
<!-- default-config 默认的配置, -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/students</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<!-- This app is massive! -->
<named-config name="oracle">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property>
<!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
3.封装JDBCUtil.java用来获取来连接,一般放在util包下
package Student.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtil {
static ComboPooledDataSource dataSource = null;
static{
dataSource = new ComboPooledDataSource();
}
public static DataSource getDataSource(){
return dataSource;
}
/**
* 获取连接对象
* @return
* @throws SQLException
*/
public static Connection getConn() throws SQLException{
return dataSource.getConnection();
}
/**
* 释放资源
* @param conn
* @param st
* @param rs
*/
public static void release(Connection conn , Statement st , ResultSet rs){
closeRs(rs);
closeSt(st);
closeConn(conn);
}
public static void release(Connection conn , Statement st){
closeSt(st);
closeConn(conn);
}
private static void closeRs(ResultSet rs){
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}
}
private static void closeSt(Statement st){
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null;
}
}
private static void closeConn(Connection conn){
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
4.同样在util包下创建一个TextUtil,用来判断字符是否为空
/**
* @author Leonard
* 判断一个字符串是否为空
*/
public class TextUtils {
public static boolean isEmpty(CharSequence s){
return s==null || s.length()==0;
}
}
接下开始写项目了。。。。。。。。。。。。。。。。。
写项目
项目完成预览图
进入第一个列表
分页显示页面
第一步:创建jsp页面及对应的Servlet
1.创建index.jsp显示页面
<body>
//超链接的地址是Servlet控制层
<h3><a href="StudentListServlet">显示所有学生列表</a></h3><br>
<h3><a href="StudentListPageServlet?currentPage=1">分页显示所有学生列表</a></h3>
</body>
StudentListServlet.java
public class StudentListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
StudentService service = new StudentServiceImpl();
List<Student> list = service.findAll();
//把结果存域
request.getSession().setAttribute("list", list);
//重定向方式,把结果存到session域需要
response.sendRedirect("list.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
StudentListPageServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.获取需要显示的页码数
int currentPage = Integer.parseInt(request.getParameter("currentPage"));
//2.根据指定页数获取该页数据回来;
StudentService service = new StudentServiceImpl();
PageBean pageBean = service.findStudentByPage(currentPage);
request.setAttribute("pageBean", pageBean);
//3.跳转页面
request.getRequestDispatcher("list_page.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
2.查询显示页面list.jsp
html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function doDelete(sid){
var flag = confirm("是否删除?");
if(flag){
location.href="DeleteServlet?sid="+sid;
}
}
</script>
<title>学生列表页面</title>
</head>
<body>
<form action="SearchStudentServlet" method="post">
<table border="1" cellspacing="0">
<tr>
<td colspan="8">
按姓名查询:<input type="text" name="sname">
按性别查询:<select name="gender" >
<option value="">--请选择--
<option value="男">男
<option value="女">女
</select>
<input type="submit" value="查询">
<a href="add.jsp">添加</a>
</td>
</tr>
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>电话</td>
<!-- <td>生日</td> -->
<td>爱好</td>
<td>简介</td>
<td>操作</td>
</tr>
<c:forEach items="${list}" var="stu">
<tr>
<td>${stu.sid}</td>
<td>${stu.sname}</td>
<td>${stu.gender}</td>
<td>${stu.phone}</td>
<%-- <td>${stu.birthday}</td> --%>
<td>${stu.hobby}</td>
<td>${stu.info}</td>
<td><a href="EditServlet?sid=${stu.sid}">更新</a> <a href="#" onclick="doDelete(${stu.sid})">删除</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
SearchStudentServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.中文
request.setCharacterEncoding("UTF-8");
//2.获取前台数据
String sname = request.getParameter("sname");
String gender = request.getParameter("gender");
//3.调用service
StudentService service = new StudentServiceImpl();
List<Student> list = service.SearchStudentList(sname, gender);
System.out.println("list集合大小========"+list.