题干
利用三大框架整合技术,向Student表的插入一条数据,具体要求完成如下操作
1.配置Spring MVC核心配置文件springmvc-config.xml,重点配置注解扫描包和视图解析器
2.参照下图Student表结构,完成数据层的Mapper接口及映射文件的编写
3.编写业务层的StudentService接口及其实现类
4.参照下图前台界面,编写信息录入页面login.jsp,并编写Controller接收请求数据,并进行处理。
5.若插入成功,将插入的信息在success.jsp页面中进行显示。
头文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
代码
1、配置Spring MVC核心配置文件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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 只扫描controller包 -->
<context:component-scan base-package="com.haust.controller"></context:component-scan>
<!-- 自定义视图解析器 -->
<bean id="viemResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2、数据层的Mapper接口及映射文件
//StudentMapper接口
public interface StudentMapper {
public Student insertStudent(Student student);
}
<!-- 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.haust.mapper.StudentMapper" >
<insert id="insertStudent" parameterType="com.haust.Student">
insert into student
values(#{student_id},#{sname},#{password},#{sex},#{note})
</insert>
</mapper>
3、业务层的StudentService接口及其实现类
//StudentService接口
public interface StudentService {
public Student insertByStudent(Student student);
}
//实现类
package com.haust.service.impl;
import javax.annotation.Resource;
import com.haust.mapper.StudentMapper;
import com.haust.pojo.Student;
import com.haust.service.StudentService;
public class StudentServiceImpl implements StudentService{
@Resource
private StudentMapper studentMapper;
public Student insertByStudent(Student student){
return studentMapper.insertStudent(student);
}
}
4、login.jsp,并编写Controller接收请求数据,并进行处理
<%@ 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>
<!-- 只用记这部分 -->
<form action="${pageContext.request.contextPath}/doLogin" method="post">
编号:<input type="text" name="student_id"></br>
姓名:<input type="text" name="sname"></br>
密码:<input type="text" name="password"></br>
备注:<textarea rows="3" cols="" name="note" ></textarea></br>
性别:<input type="radio" name="sex" value="1">男
<input type="radio" name="sex" value="0">女 </br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
//StudentController类接收请求数据
@Controller
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("/login")
public String tologin(){
return "login";
}
@RequestMapping("/doLogin")
public String insert(Student student, Model model) {
studentService.insert(student);
model.addAttribute("student", student);
return "success";
}
}
5、success.jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${student.student_id}
${student.sname}
</body>
</html>