提示:如果没看过上篇博客的请,先看上篇
1.昨天我们已经成功的整合了spring和Mybatis今天就是我们的控制层springMVC了,
首先我们需要配置web.xml,为什么我相信大家都知道了,我就
直接上代码了
<servlet>
<servlet-name>seckill-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC需要加载的配置文件
spring-dao,spring-service,spring-web
mybatis->spring->springMVC
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>seckill-dispatcher</servlet-name>
<!-- 默认匹配所有的 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
接下来就需要配值相关的服务层的xml以及控制层的
这里我先给出完整的工程结构
1)建立一个spring-service.xml和spring-web.xml放在spring文件下
其中spring-service中配置与事务相关的bean以及扫描相关的包
1.1spring-service的配置类容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 首先扫描service下面相关的使用了注解的包 -->
<context:component-scan base-package="org.mtest.service"></context:component-scan>
<!-- 本文中没有使用事务,所以在这可以配置也可以不配-->
<bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<!-- 注入数据库的连 在dao层配置过 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
1.2spring-web的内容
<!-- 1 第一步 开启springMVC注解模式 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- servlet-mapping 映射路劲:“/” -->
<!--
允许使用“/”做整体映射
-->
<mvc:default-servlet-handler/>
<!-- 配置jsp显示viewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- web相关包扫描 -->
<context:component-scan base-package="org.mtest.web"></context:component-scan>
完成上面的配置之后,我们要做的是,编写服务层以及控制层
1)我们先建立一个service包,然后创建一个userService接口以及其实现类
userservice:
package org.mtest.service; import org.apache.ibatis.annotations.Param; public interface UserService { String queryUserNameById(@Param("id")int id); }
userServiceImpl(注意添加注解不然会报错)
package org.mtest.service;
import org.mtest.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
/**
* 演示通过页面提交id获取用户姓名
*/
@Override
public String queryUserNameById(int id) {
return userDao.queryById(id).getUserName();
}
}
package org.mtest.web;
import org.mtest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("")
public class Controlller {
@Autowired
private UserService service;
@RequestMapping(value="/name",method=RequestMethod.GET)
public String userName(Model model){
String name = service.queryUserNameById(1);
model.addAttribute("name", name);
return "show";
}
}
3)jsp页面,必须创建在WEB-INF/jsp/下面,因为我们配置的前缀(在这里我只是简单的显示姓名)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${name}
</body>
</html>
以上是我个人学习的总结希望能帮到大家,也希望大神指点一二