部分内容转载自:HOW2J.CN
数据库准备
- 创建数据库/数据表
create database how2java;
use how2java;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30) ,
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
-
插入数据
use how2java; insert into category_ values(null,"category1"); insert into category_ values(null,"category2"); insert into category_ values(null,"category3"); insert into category_ values(null,"category4"); insert into category_ values(null,"category5"); select * from category_
创建项目目录
导入jar包
复制所需jar包到web/WEB-INF/lib
中
把jar包导入到项目中Java Build Path
=>Libraries
=>Add External jars
准备接口/类/配置文件
配置文件
- web.xml(WEB-INF)
- 在web app启动时,通过ContextLoaderListener获取contextConfigLocation配置文件的文件名applicationContext.xml,完成Spring相关配置。
<!-- 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,用DispatcherServlet拦截所有访问。
<!-- 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>
- applicationContext.xml(src)
- 打开注解,将Service的生命周期纳入Spring的管理
<context:annotation-config />
<context:component-scan base-package="com.how2java.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/how2java?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>admin</value>
</property>
</bean>
- 扫描存放SQL语句的xml文件
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.how2java.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
</bean>
- 扫描Mapper,并将其生命周期纳入Spring的管理
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.how2java.mapper"/>
</bean>
- springMVC.xml(src)
- 扫描Controller,并将其生命周期纳入Spring管理
<context:annotation-config/>
<context:component-scan base-package="com.how2java.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
- 注解驱动,是的访问路径与方法的匹配可以通过注解配置
<mvc:annotation-driven />
- 静态页面可以访问
<mvc:default-servlet-handler />
- 视图定位到/WEB-INF/jsp
<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>
- 存放与Mapper类对应SQL语句的xml文件(与mapper类同包)
<mapper namespace="com.how2java.mapper.CategoryMapper">
注: namespace
必须写对应的Mapper类的完整类名。
部署
- class文件输出到/WEB-INF/classes目录下
- 修改tomcat的server.xml
<Context path="/" docBase="E:\\project\\j2ee\\web" debug="0" reloadable="false" />
运行原理图