运用java编写网页版学生信息管理系统
能够对数据库进行增、删、改、查操作使用springmvc框架进行搭建。
创建Stu.java文件,内容有:
public class Stu { //学生的相关属性 private Integer id; //学号 private String name; //姓名 private Integer age; //年龄 private String addr; //居住地址 //生成相应的set和get方法 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; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } //重写toString方法 @Override public String toString() { return "Stu [id=" + id + ", name=" + name + ", age=" + age + ", addr=" + addr + "]"; }
以及一些有关框架的配置文件mybatis-config.xml、applicationContext.xml、springmvc-config.xml
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 1.加载jdbc.properties文件的位置 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.配置druid连接池 ,id是固定值,class是druid连接池类的全路径 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 配置连接数据库的基本信息 --> <property name="driverClassName" value="${db.driverClassName}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> <!-- 3.整合spring和mybatis框架 将SqlSession等对象的创建交给Spring容器 id值(sqlSessionFactory)是固定值 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 3.1.指定mybatis核心配置文件的位置 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <!-- 3.2.配置连接池(数据源) ref指向连接池bean对象的id值 --> <property name="dataSource" ref="dataSource"></property> <!-- 3.3、扫描所有的 XxxMapper.xml映射文件,读取其中配置的SQL语句 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> <!-- 4、定义mapper接口扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描所有XxxMapper接口,将接口实例的创建交给spring容器 --> <property name="basePackage" value="com.tedu.dao"/> </bean> </beans> springmvc-config.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 1.配置前端控制器放行静态资源(html/css/js等,否则静态资源将无法访问) --> <mvc:default-servlet-handler/> <!-- 2.配置注解驱动,用于识别注解(比如@Controller) --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 3.配置需要扫描的包:spring自动去扫描 base-package 下的类, 如果扫描到的类上有 @Controller、@Service、@Component等注解, 将会自动将类注册为bean --> <context:component-scan base-package="com.tedu.controller"> </context:component-scan> <!-- 4.配置内部资源视图解析器 prefix:配置路径前缀 suffix:配置文件后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
进行增、删、改、查的操作控制的相关文件代码
/* * 负责处理用户的所有请求 */ @Controller public class StuController { /* * /{jspName}中的jspName用于接收访问的路径名称 * 例如:访问路径为http://localhost/yonghe/index * 那么{}中的jspName的值就是"index",再将{}中jspName的值 * 传给形参的jspName,最后作为返回值返回,表示跳转到这个名称 * 对应的jsp页面 */ @RequestMapping("/{page}") public String page(@PathVariable String page) { return page; } /* * 获取StuMapper接口的子类实例 */ @Autowired StuMapper stuMapper; /* * 1.查询所有学生信息 */ @RequestMapping("/stuList") public String StuList(Model model) { //查询所有学生信息 List<Stu> list=stuMapper.findAll(); //将学生信息的集合存入model中,再转发到jsp中 model.addAttribute("list", list); //跳转到stu_list.jsp,显示所有学生信息 return "stu_list"; } /* * 2.根据id删除学生信息 */ @RequestMapping("/stuDelete") public String stuDelete(Integer id) { //调用stuMapper的deleteById方法,根据id删除学生信息 stuMapper.deleteById(id); //跳转到学生列表页面,查询所有学生信息 //return "stu_list";直接跳转到学生列表页面,不会显示学生信息 return "forward:/stuList"; } /* * 3.新增学生信息 */ @RequestMapping("/stuAdd") public String stuadd(Stu stu) { //调用stuMapper的add方法 stuMapper.add(stu); //跳转到学生列表页面,查询最新所有学生信息 return "forward:/stuList"; } /* * 4.根据id查询学生信息 */ @RequestMapping("/stuInfo") public String stuInfo(Integer id,Model model) { //调用stuMapper的findById方法,进行id查询学生信息 Stu stu=stuMapper.findById(id); //将学生对象存入Madel中,再带到学生信息修改页面 model.addAttribute("stu", stu); //将查询到的学生信息带到学生信息修改页面,进行数据的回显 return "stu_update"; } /* * 5.根据id修改学生信息 */ @RequestMapping("/stuUpdate") public String stuUpdate(Stu stu) { //调用stuMapper的updateById方法 stuMapper.updateById(stu); //转发到查询所有门店的方法,跳转到学生列表页面,查询最新学生信息 return "redirect:/stuList"; } } <?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"> <!-- 学生表的映射文件 namespace值为对应接口的全路径 --> <mapper namespace="com.tedu.dao.StuMapper"> <!-- 1.查询所有学生信息,id值为对应接口中方法的名字 resultType指定将查询的结果封装到哪个pojo对象中 --> <select id="findAll" resultType="com.tedu.pojo.Stu"> select * from stu </select> <!-- 2.根据id删除学生信息(id是传过来的) --> <delete id="deleteById"> delete from stu where id=#{id} </delete> <!-- 3.新增学生信息(name、age、addr都是传过来的) --> <insert id="add"> insert into stu value(null,#{name},#{age},#{addr}) </insert> <!-- 4.根据id查询门店信息 --> <select id="findById" resultType="com.tedu.pojo.Stu"> select * from stu where id=#{id} </select>
<!-- 根据id修改学生信息(修改后的学生name、age、addr也都是传过来的) --> <update id="updateById"> update stu set name=#{name},age=#{age},addr=#{addr} where id=#{id} </update> </mapper>
最后运行结果