1 首先配置web.xml
1.1配置的信息主要有:
<!-- 在web启动的时候自动加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--
将配置文件放到src下的config文件夹下后的路径配置
<param-value>classpath*:config/applocationContext.xml, ....</param-value>
-->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
1.2 配置过滤器
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1.3 配置dispatchServlets:所有的request的请求都转到这然后根据URL路径转到相应的Controller中处理
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- springmvc的配置文件的默认格式是<servlet-name>-servlet.xml 在进行参数设置后就可以随意的起名字了就像现在这样是的 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<!-- 这样能够拦截静态文件.jpg,.css,.js等文件会被拦截,但是后面的配置中解决会有办法解决 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
2 编写相应的java文件本例比较简单只有一个实体类
- package com.ouma.spring.model;
- public class User {
- private int id;
- private String userName;
- private int userAge;
- private String userAddress;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public int getUserAge() {
- return userAge;
- }
- public void setUserAge(int userAge) {
- this.userAge = userAge;
- }
- public String getUserAddress() {
- return userAddress;
- }
- public void setUserAddress(String userAddress) {
- this.userAddress = userAddress;
- }
- }
3 创建映射文件和接口
- public interface UserMapper {
- public User selectUserById(int id);
- public List<User> listUsers();
- public void addUser(User user);
- public void updateUser(User user);
- public void deleteUserById(int id);
- }
- <?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.ouma.spring.mapper.UserMapper">
- <resultMap type="User" id="resultListUser">
- <id column="id" property="id"/>
- <result column="userName" property="userName"/>
- <result column="userAge" property="userAge"/>
- <result column="userAddress" property="userAddress"/>
- </resultMap>
- <select id="selectUserById" parameterType="int" resultType="user">
- select * from user where id=#{id}
- </select>
- <select id="listUsers" resultType="user">
- select * from user
- </select>
- <insert id="addUser" parameterType="user">
- insert into user(id, userName, useraAge, userAddress) value(#{id},#{userName}, #{userAge}, #{userAddress})
- </insert>
- <update id="updateUser" parameterType="user">
- update user set userName=#{userName}, userAge=#{userAge}, userAddress=#{userAddress}
- </update>
- <delete id="deleteUserById" parameterType="int">
- delete from user where id=#{id}
- </delete>
- </mapper>
4 配置spring的配置文件applicationContext.xml
- <!-- 读取配置文件 -->
- <context:property-placeholder location="/WEB-INF/database.property"/>
- <!-- 本例采用DHCP数据源,应先将DHCP的jar包复制到工程的lib目录下 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <!-- name中的参数必须是这样的不能修改 -->
- <!-- <property name="driverClassName" value="${drivername}" />
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" /> -->
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/ibatis" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </bean>
- <!-- 下面的bean就是spring来管理mybatis了-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="/WEB-INF/mybatis.xml" />
- <!-- 这里可以配置mapper的映射xml文件,当然当你的映射文件和你的接口的文件一样的话就不需要了 -->
- <!--
- <property name="mapperLocations" vaue="路径名/*.xml">
- -->
- </bean>
- <!-- 通过扫描的模式将该目录下的文件(就是映射文件和相关的接口) -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.ouma.spring.mapper"></property>
- </bean>
5 配置mybatis的配置文件mybatis.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <typeAliases>
- <typeAlias type="com.ouma.spring.model.User" alias="user"/>
- </typeAliases>
6 配置springMVC的配置文件mvc.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-3.1.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
- <!-- <mvc:annotation-driven /> -->
- <!-- <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
- 并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
- 后面,我们处理响应ajax请求时,就使用到了对json的支持。
- 面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。 -->
- <!-- DefaultAnnotationHandlerMapping 通过注解,把一个URL映射到Controller类上 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- <!-- AnnotationMethodHandlerAdapter类,通过注解,把一个URL映射到Controller类的方法上 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <!-- 访问jsp,路径拼接 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix">
- <value>/WEB-INF/pages/</value>
- </property>
- <property name="suffix">
- <value>.jsp</value>
- </property>
- </bean>
- <!-- 对静态文件的访问前面我们在配置dispatchServlet的时候讲过那种拦截会将静态的文件进行拦截 这里我们就可以这样配置后使用静态文件了 -->
- <!-- <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/> -->
- <!-- 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean -->
- <context:component-scan base-package="com.ouma.spring.controller" />
- </beans>
- </configuration>
7 编写controller类
- @Controller
- @RequestMapping("/user")
- public class UserController {
- @Autowired
- UserMapper userMapper;
- @RequestMapping("/list")
- public ModelAndView listall(HttpServletRequest request,HttpServletResponse response){
- List<User> listUser = userMapper.listUsers();
- ModelAndView mav=new ModelAndView("listUser");
- mav.addObject("listUser", listUser);
- return mav;
- }
- }