首先根據我淺顯的認知,回顧一下Spring,Spring MVC,Mybatis各個小demo中須要作的配置工做。。。html
Spring是一個基於IOC和AOP的結構J2EE系統的框架,IOC 反轉控制 是Spring的基礎,Inversion Of Control 。
簡單說就是建立對象由之前的程序員本身new 構造方法來調用,變成了交由Spring建立對象
DI 依賴注入 Dependency Inject. 簡單地說就是拿到的對象的屬性,已經被注入好相關值了,直接使用便可。java
Spring的核心配置文件 是applicationContext.xml。能夠經過以下方式,不須要本身new對象,直接獲取已經被注入值的對象mysql
<bean name="u" class="com.spring.pojo.User">程序員
<property name="name" value="User 1" />web
</bean>spring
若是使用註解的方式進行配置,應該在applicationContext.xml中寫上<context:annotation-config/>。sql
屬性的註解方式有@Autowired(加在屬性前或者加在setter前),@Resource(加在屬性前,寫好值),這樣則無需在配置文件中寫<property>。對於實體類,也能夠進行註解,在class前寫上@Component,爲屬性賦值。數據庫
WEB-INF目錄下須要建立web.xml。在其中進行servlet的配置。包括<servlet-name/>、<servlet-class/>、<url-pattern/>。express
在WEB-INF目錄下建立springmvc-servlet.xml。其中這裏的<servlet-name>與web.xml中的相同。此文件中對訪問路徑進行相應Controller配置,表面訪問某路徑交由對應的bean處理。而xxxController實現接口Controller,提供方法handleRequest,獲取ModelAndReview的數據,渲染頁面。數組
準備mysql的數據庫ssm,創建表格category_。插入必定數據。
在eclipse中創建pojo的package,創建Category.java實體類。
準備接口CategoryMapper,寫上各種增刪查改方法名。在mapper的package下,新建Category.xml。在Category.xml中,<mapper namespace="..."/>這裏必須寫上同一個包下CategoryMapper的完整類名,com.ssm.mapper.CategoryMapper。增刪查改內容同以前學過的Mybatis。代碼以下:
<?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.ssm.mapper.CategoryMapper"> <insert id="add" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert> <delete id="delete" parameterType="Category" > delete from category_ where id= #{id} </delete> <select id="get" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="update" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update> <select id="list" resultType="Category"> select * from category_ </select> </mapper>
分別創建service和service.impl的package。在兩個包下分別創建CategoryService和CategoryServiceImpl。值得注意的是CategoryServiceImpl必定要繼承CategoryService。因爲在applicationContext.xml中,已經進行了相關bean配置。因此能夠經過@Autowired的註解方式獲取CategoryMapper對象,並調用list方法。
package com.ssm.service; import java.util.List; import com.ssm.pojo.Category; public interface CategoryService { List<Category> list(); }
package com.ssm.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ssm.mapper.CategoryMapper; import com.ssm.pojo.Category; import com.ssm.service.CategoryService; @Service public class CategoryServiceImpl implements CategoryService{ @Autowired CategoryMapper categoryMapper; public List<Category> list(){ return categoryMapper.list(); } }
如上所述,訪問請求先經過DispatcherServlet,而後訪問對應的xxxController類。也就是說,在xxxController類中,開始請求的任務。利用註解在類前標註@Controller,代表這是一個Controller的類。而後經過註解獲取categoryService對象。利用categoryService獲取種類數據以後,存放在數組中,最終渲染到頁面上。
package com.ssm.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.ssm.pojo.Category; import com.ssm.service.CategoryService; @Controller @RequestMapping("") public class CategoryController { @Autowired CategoryService categoryService; @RequestMapping("listCategory") public ModelAndView listCategory(){ ModelAndView mav = new ModelAndView(); List<Category> cs= categoryService.list(); // 放入轉發參數 mav.addObject("cs", cs); // 放入jsp路徑 mav.setViewName("listCategory"); return mav; } }
如下是各種配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- spring的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring mvc核心:分發servlet --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- spring mvc的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:SpringMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:annotation-config/> <context:component-scan base-package="com.ssm.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven /> <mvc:default-servlet-handler /> <!-- 視圖定位 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config /> <context:component-scan base-package="com.ssm.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>admin</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.ssm.pojo" /> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.mapper"/> </bean> </beans>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <table align='center' border='1' cellspacing='0'> <tr> <td>id</td> <td>name</td> </tr> <c:forEach items="${cs}" var="c" varStatus="st"> <tr> <td>${c.id}</td> <td>${c.name}</td> </tr> </c:forEach> </table>