資源下載:https://download.csdn.net/download/weixin_44893902/45603211
練習點設計: 模糊查詢、刪除、新增
一、語言和環境
- 實現語言:JAVA語言。
- 環境要求:MyEclipse/Eclipse + Tomcat + MySql。
- 使用技術:
Jsp+Servlet+JavaBean或SpringMVC+Spring+Mybatis。
二、實現功能
隨着數字化信息的發展,現需要制作學生信息管理系統,主要功能如下:
1.首頁默認顯示所有學生信息,如圖1所示。

2.鼠標懸停某行數據時,以線性過渡動畫顯示光棒效果,如圖2所示。

3.用戶輸入學生名稱,點擊查詢,則完成模糊查詢,顯示查詢結果,如圖3所示。

4.用戶點擊刪除,則彈出提示框,用戶點擊確定后,刪除選中數據並顯示最新數據,如圖4和圖5所示。


5.用戶點擊“新增”按鈕,則打開新增頁面,填寫完相關信息后點擊新增按鈕,增加學生信息數據到數據庫,且頁面跳轉到列表頁面展示最新數據,如圖6和圖7所示。


三、 數據庫設計
1.創建數據庫(stu_db)。
2.創建數據表(student),結構如下。
| 字段名 | 說明 | 字段類型 | 長度 | 備注 |
|---|---|---|---|---|
| id | 編號 | int | 主鍵,自增 | |
| name | 學生姓名 | varchar | 50 | 不能為空 |
| age | 年齡 | int | 不能為空 | |
| classes | 班級名稱 | varchar | 50 | 不能為空 |
| birth | 出生日期 | date | 不能為空 |
四、推薦實現步驟
1.SSM版本的實現步驟如下:
(1)創建數據庫和數據表,添加測試數據(至少添加4條測試數據)。
(2)創建Web工程並創建各個包,導入工程所需的jar文件。
(3)添加相關SSM框架支持。
(4)配置項目所需要的各種配置文件(mybatis配置文件、spring配置文件、springMVC配置文件)。
(5)創建實體類。
(6)創建MyBatis操作數據庫所需的Mapper接口及其Xml映射數據庫操作語句文件。
(7)創建業務邏輯相應的接口及其實現類,實現相應的業務,並在類中加入對DAO/Mapper的引用和注入。
(8)創建Controller控制器類,在Controller中添加對業務邏輯類的引用和注入,並配置springMVC配置文件。
(9)創建相關的操作頁面,並使用CSS對頁面進行美化。
(10)實現頁面的各項操作功能,並在相關地方進行驗證,操作要人性化。
(11)調試運行成功后導出相關的數據庫文件並提交。
五、實現代碼
1、MySQL數據庫
stu_db.sql

2、項目Java代碼
目錄結構
student

JAR包:


src
com.controller
StudentController.java
package com.controller;
import java.util.List;
import javax.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.dao.StudentMapper;
import com.entity.Student;
import com.service.StudentService;
@Controller
public class StudentController {
@Resource
StudentService service;
@RequestMapping("/selectStudent")
public ModelAndView selectStudent(String keyword) {
List<Student> studentList=service.selectAll(keyword);
if (keyword==null||keyword.trim().equals("")) {
keyword="";
}
System.out.println("123456");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("studentList", studentList);
modelAndView.setViewName("student");
return modelAndView;
}
@RequestMapping("/delStudent")
public String delStudent(int id) {
int del=service.delStudent(id);
return "redirect:/selectStudent.do";
}
@RequestMapping("/jumpInsert")
public String jumpInsert() {
return "addStudent";
}
@RequestMapping("/insertStudent")
public String insertStudent(Student student) {
int add=service.insertStudent(student);
return "redirect:/selectStudent.do";
}
}
com.dao
StudentMapper.java
package com.dao;
import com.entity.Student;
import java.util.List;
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
Student selectByPrimaryKey(Integer id);
List<Student> selectAll();
int updateByPrimaryKey(Student record);
List<Student> likeSelect(String keyword);
}
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.StudentMapper" >
<resultMap id="BaseResultMap" type="com.entity.Student" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="classes" property="classes" jdbcType="VARCHAR" />
<result column="birth" property="birth" jdbcType="VARCHAR" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from student
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.entity.Student" >
insert into student (id, name, age,
classes, birth)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{classes,jdbcType=VARCHAR}, #{birth,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com.entity.Student" >
update student
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
classes = #{classes,jdbcType=VARCHAR},
birth = #{birth,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select id, name, age, classes, birth
from student
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select id, name, age, classes, birth
from student
</select>
<select id="likeSelect" resultMap="BaseResultMap" >
select id, name, age, classes, birth
from student where `name` LIKE "%"#{name}"%"
</select>
</mapper>
com.entity
Student.java
package com.entity;
public class Student {
private Integer id;
private String name;
private Integer age;
private String classes;
private String birth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes == null ? null : classes.trim();
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth == null ? null : birth.trim();
}
}
com.generator
Generator.java
package com.generator;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Generator {
/* * targetRuntime="MyBatis3Simple", 不生成Example */
public void generateMyBatis() {
//MBG執行過程中的警告信息
List<String> warnings = new ArrayList<String>();
//當生成的代碼重復時,覆蓋原代碼
boolean overwrite = true ;
//String generatorFile = "/generator/generatorConfig.xml";
String generatorFile = "/generatorConfig.xml";
//讀取MBG配置文件
InputStream is = Generator.class.getResourceAsStream(generatorFile);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config;
try {
config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
//創建MBG
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
//執行生成代碼
myBatisGenerator.generate(null);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (String warning : warnings) {
System.out.println(warning);
}
}
public static void main(String[] args) {
Generator generator = new Generator();
generator.generateMyBatis();
}
}
com.service
StudentService.java
package com.service;
import java.util.List;
import com.entity.Student;
public interface StudentService {
//查詢
List<Student> selectAll(String keyword);
//刪除
int delStudent(int id);
//添加
int insertStudent(Student student);
}
com.service.imp
StudentServiceImpl.java
package com.serviceImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dao.StudentMapper;
import com.entity.Student;
import com.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
StudentMapper mapper;
@Override
// 查詢
public List<Student> selectAll(String keyword) {
if (keyword == null || keyword.trim().equals("")) {
List<Student> selectAll = mapper.selectAll();
return selectAll;
} else {
List<Student> likeSelect = mapper.likeSelect(keyword);
return likeSelect;
}
}
// 刪除
@Override
public int delStudent(int id) {
int del = mapper.deleteByPrimaryKey(id);
return del;
}
// 添加
@Override
public int insertStudent(Student student) {
int insert = mapper.insert(student);
return insert;
}
}
mybatis
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 別名 -->
<typeAliases>
<package name="com.entity" />
</typeAliases>
</configuration>
spring
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
<!-- 指定spring容器讀取db.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 將連接池注冊到bean容器中 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="Url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 設置MyBatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
<!-- 設置數據源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Mapper掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 設置Mapper掃描包 -->
<property name="basePackage" value="com.dao" />
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 開啟注解方式管理AOP事務 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
<!-- 配置Service掃描 -->
<context:component-scan base-package="com" />
</beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
<!-- 配置Controller掃描 -->
<context:component-scan base-package="com.controller" />
<!-- 配置注解驅動 -->
<mvc:annotation-driven />
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后綴 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 配置生成器 -->
<generatorConfiguration>
<context id="MySQLContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- 配置前置分隔符和后置分隔符 -->
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<!-- 配置注釋信息 -->
<commentGenerator>
<!-- 不生成注釋 -->
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
<property name="addRemarkComments" value="true" />
</commentGenerator>
<!-- 數據庫連接配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/stu_db" userId="root" password="root">
</jdbcConnection>
<!-- targetPackage:生成實體類存放的包名, targetProject:指定目標項目路徑,可以使用相對路徑或絕對路徑 -->
<javaModelGenerator targetPackage="com.entity" targetProject="src">
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 配置SQL映射器Mapper.xml文件的屬性 -->
<sqlMapGenerator targetPackage="com.dao" targetProject="src" />
<!-- type="XMLMAPPER":所有的方法都在XML中,接口調用依賴XML文件 -->
<javaClientGenerator targetPackage="com.dao" type="XMLMAPPER" targetProject="src" />
<!-- 生成所有表的映射 -->
<table tableName="%"></table>
</context>
</generatorConfiguration>
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/stu_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
WebContent
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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>library.two</display-name>
<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>
<!--spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 監聽器,加載spring配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 設置post請求的字符編碼過濾器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
JSP
Home.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()+path;
%>
<html>
<head>
<meta charset="utf-8">
<title>學生信息管理系統</title>
</head>
<body>
<script type="text/javascript"> window.location.href="<%=basePath%>/selectStudent.do"; </script>
</body>
</html>
addStudent.jsp
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<div class="addMoot">
<form action="insertStudent.do" method="post">
<input type="hidden" name="id" value="${studentList.id}" />
<p class="add_title">錄入新同學</p>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>學生姓名:</td>
<td><input type="text" name="name" value="${student.name}"></td>
</tr>
<tr>
<td>年齡:</td>
<td><input type="text" name="age" value="${student.age}"></td>
</tr>
<tr>
<td>班級名稱:</td>
<td><input type="text" name="classes" value="${student.classes}"></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="date" name="birth" value="${student.birth}"></td>
</tr>
<tr>
<td class="btn" colspan="2"><input class="button" type="submit" value="新增" /> <input class="reset" type="button" onclick="history.go(-1);" value="取消" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
student.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<head>
<meta charset="utf-8">
<title>學生信息管理系統</title>
</head>
<style type="text/css"> tr:hover { background: orange; } </style>
<body>
<form action="">
<fieldset style="height: 60px">
<legend> 搜索</legend>
姓名:<input type="text" placeholder="請輸入姓名搜索" name="keyword" style="height: 25px;" />
<button type="submit">查詢</button>
<button>
<a href="jumpInsert.do">新增</a>
</button>
</fieldset>
</form>
<table border="1px">
<tr>
<th>編號</th>
<th>學生姓名</th>
<th>年齡</th>
<th>班級名稱</th>
<th>生日</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${studentList }" varStatus="item">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.classes}</td>
<td>${student.birth}</td>
<td><a onclick="del(${student.id})">刪除</a></td>
</tr>
</c:forEach>
</table>
<div>
<span>共${studentList.size()}條數據</span>
</div>
<script type="text/javascript"> function del(id){ if(confirm("確認要刪除嗎?")){ return window.location.href="delStudent.do?id="+id; }else { return false; } } </script>
</body>
</html>
