自定义MVC增删改查

目录

创建项目

 源码


创建项目

如图,创建对应的包和类还有接口,创建源码包(resources)放置配置文件(config.xml)和连接数据库的用户名和密码(jdbc.properties)

                                       

导入相应的jar包,这里的mymvc.jar包是我自己写的框架,怎么把它变成jar包可以参考我的另一篇文章自定义mvc原理和框架实现

 

 源码

Student

package com.zking.mvctest02.model;

public class Student {

	private Integer sid;
	
	private String sname;
	
	private Integer score;
	
	private String clazz;

	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public Integer getScore() {
		return score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", score=" + score + ", clazz=" + clazz + "]";
	}
	
	
}

IStudentDao

package com.zking.mvctest02.dao;

import java.util.List;

import com.zking.mvctest02.model.Student;
import com.zking.util.PageBean;

public interface IStudentDao {

	List<Student> getStudents(Student stu,PageBean pageBean);
	
	void addStudent(Student stu);
	
}

StudentDao

package com.zking.mvctest02.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.zking.mvctest02.model.Student;
import com.zking.util.BaseDao;
import com.zking.util.BaseDao.ICovent;
import com.zking.util.DBUtil;
import com.zking.util.PageBean;

public class StudentDao implements IStudentDao {

	@Override
	public List<Student> getStudents(Student stu, PageBean pageBean) {
		String sql = "SELECT sid,sname,score,class FROM student";
		
		List<Object> param = new ArrayList<Object>();
		if(stu != null && stu.getSname() != null && !"".equals(stu.getSname())) {
			sql += " where sname like ? ";
			param.add(stu.getSname()+"%");
			
		}
		
		return BaseDao.query(sql, param, pageBean, new ICovent<Student>() {

			@Override
			public List<Student> convent(ResultSet rs) throws SQLException {
				List<Student> list = new ArrayList<Student>();
				while(rs.next()) {
					Student student = new Student();
					student.setSid(rs.getInt("sid"));
					student.setSname(rs.getString("sname"));
					student.setScore(rs.getInt("score"));
					student.setClazz(rs.getString("class"));
					list.add(student);
				}
				return list;
			}
			
		});

	}



	@Override
	public void addStudent(Student stu) {
		String sql = "insert into student(sname,score,class) values(?,?,?)";
		
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBUtil.getConection();
			ps=con.prepareStatement(sql);
			ps.setObject(1, stu.getSname());
			ps.setObject(2, stu.getScore());
			ps.setObject(3, stu.getClazz());
			ps.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtil.closeDB(null, ps, con);
		}
		
		
	}
	
	
	
	
    @Test
	public void testGetStudent() {
		Student student = new Student();
		List<Student> list = getStudents(student, new PageBean());
		list.forEach(t -> System.out.println(t));
	}
}

IStudentService

package com.zking.mvctest02.service;

import java.util.List;

import com.zking.mvctest02.model.Student;
import com.zking.util.PageBean;

public interface IStudentService {

	List<Student> getStudents(Student stu,PageBean pageBean);
	
	void addStudent(Student stu);
	
}

StudentService

package com.zking.mvctest02.service;

import java.util.List;

import com.zking.mvctest02.dao.IStudentDao;
import com.zking.mvctest02.dao.StudentDao;
import com.zking.mvctest02.model.Student;
import com.zking.util.PageBean;

public class StudentService implements IStudentService {

	private IStudentDao dao = new StudentDao();
	
	@Override
	public List<Student> getStudents(Student stu, PageBean pageBean) {
		return dao.getStudents(stu, pageBean);
	}

	@Override
	public void addStudent(Student stu) {
		dao.addStudent(stu);
	}

}

config.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
	<!ELEMENT config (action*)>
	<!ELEMENT action (forward*)>
	<!ELEMENT forward EMPTY>
	<!ATTLIST action
	  path CDATA #REQUIRED
	  type CDATA #REQUIRED
	>
	<!ATTLIST forward
	  name CDATA #REQUIRED
	  path CDATA #REQUIRED
	  redirect (true|false) "false"
	>
]>
<config>
	<action path="/studentAction" type="com.zking.mvctest02.action.StudentAction">
		<forward name="students" path="/students.jsp" redirect="false"/>
		<forward name="success" path="/studentAction.action?methodName=getStudents" redirect="false"/>
	</action>
</config>

jdbc.properties

driver.name = com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false
db.user=用户名
db.password=密码

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>mvctest02</display-name>
  
  <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>com.zking.util.EncodingFiter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

students.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="z" uri="/zking" %>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>学生信息</h1>

<form action="<%=request.getContextPath()%>/studentAction.action?methodName=getStudents" method="post">
		<input type="text" name="sname" value="<%=request.getParameter("sname") == null ? "" : request.getParameter("sname")%>"/>
		<input type="submit" value="查询">
		<a href="<%=request.getContextPath()%>/addStudent.jsp">增加</a>
	</form>

<table border="1" style="width: 96%">
		<tr>
			<td>学号</td>
			<td>姓名</td>
			<td>成绩</td>
			<td>班级</td>
		</tr>
		<c:forEach items="${students}" var="student">
			<tr>
				<td>${student.sid }</td>
				<td>${student.sname }</td>
				<td>${student.score }</td>
				<td>${student.clazz }</td>
			</tr>
		</c:forEach>
	</table>

     <z:paging pageBean="${pageBean}"/>
    

</body>
</html>

addStudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>增加页面</h1>
	<form
		action="<%=request.getContextPath()%>/studentAction.action?methodName=addStudent"
		method="post">
		姓名: <input type="text" id="" name="sname" />
		<p>
			成绩: <input type="text" name="score" />
		<p>
			班级: <input type="text" name="clazz" />
		<p>
		<p>
			<input type="submit" value="提交" />
	</form>

</body>
</html>

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值