Spring+SpringMVC+MyBatis 整合,想拿高工资
<select id="queryStudentByStuNo" parameterType="int" resultType="com.guor.entity.Student">
select * from student where id = #{stuNo}
</select>
<insert id="addStudent" parameterType="com.guor.entity.Student">
insert into student(id,name,age) values (#{id},#{name},#{age})
</insert>
</mapper>
(3)service(面向接口编程)
package com.guor.service;
import com.guor.entity.Student;
public interface IStudentService {
public void addStudent(Student student);
public Student queryStudentByStuNo(int id);
}
package com.guor.service.impl;
import com.guor.entity.Student;
import com.guor.mapper.StudentMapper;
import com.guor.service.IStudentService;
public class StudentServiceImpl implements IStudentService {
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public void addStudent(Student student) {
studentMapper.addStudent(student);
}
@Override
public Student queryStudentByStuNo(int id) {
return studentMapper.queryStudentByStuNo(id);
}
}
(4)controller
package com.guor.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.guor.entity.Student;
import com.guor.service.IStudentService;
@Controller
@RequestMapping("studentController")
public class StudentController {
@Autowired
@Qualifier("studentService")
private IStudentService
studentService;
public void setStudentService(IStudentService studentService) {
this.studentService = studentService;
}
@RequestMapping("queryStudentByStuNo/{id}")//映射
public String queryStudentByStuNo(@PathVariable("id") Integer id,Map<String,Object> map) {
Student student = studentService.queryStudentByStuNo(id);
map.put("student", student);
return "result";
}
}
四、Spring 整合 SpringMVC
1、jar 包
一个就行,spring-webmvc-4.3.9.RELEASE.jar
2、配置
<?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" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.guor.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
mvc:annotation-driven</mvc:annotation-driven>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArithmeticException">
error
</prop>
<prop key="java.lang.NullPointException">
error
</prop>
</props>
</property>
</bean>
</beans>
3、index.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>
<a href="studentController/queryStudentByStuNo/3">queryStudentByStuNo</a>
</body>
</html>
评论