资源下载:https://download.csdn.net/download/weixin_44893902/45603211
练习点设计: 模糊查询、删除、添加
一、语言与环境
- 实现语言:JAVA语言。
- 环境要求:MyEclipse/Eclipse Tomcat MySql。
- 使用技术:
JspServletJavaBean或SpringMVCSpringMybatis。
二、实现功能
随着数字信息的发展,学生信息管理系统的主要功能如下: 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>