部分內(nèi)容轉(zhuǎn)載自:HOW2J.CN
數(shù)據(jù)庫準(zhǔn)備
- 創(chuàng)建數(shù)據(jù)庫/數(shù)據(jù)表
create database how2java;
use how2java;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30) ,
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
-
插入數(shù)據(jù)
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_
創(chuàng)建項(xiàng)目目錄
導(dǎo)入jar包
復(fù)制所需jar包到web/WEB-INF/lib
中
把jar包導(dǎo)入到項(xiàng)目中Java Build Path
=>Libraries
=>Add External jars
準(zhǔn)備接口/類/配置文件
配置文件
- web.xml(WEB-INF)
- 在web app啟動(dòng)時(shí)末早,通過ContextLoaderListener獲取contextConfigLocation配置文件的文件名applicationContext.xml洛勉,完成Spring相關(guān)配置术辐。
<!-- 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核心:分發(fā)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" />
- 配置數(shù)據(jù)庫
<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>
- 注解驅(qū)動(dòng),是的訪問路徑與方法的匹配可以通過注解配置
<mvc:annotation-driven />
- 靜態(tài)頁面可以訪問
<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類對應(yīng)SQL語句的xml文件(與mapper類同包)
<mapper namespace="com.how2java.mapper.CategoryMapper">
注: namespace
必須寫對應(yīng)的Mapper類的完整類名仪召。
部署
- class文件輸出到/WEB-INF/classes目錄下
- 修改tomcat的server.xml
<Context path="/" docBase="E:\\project\\j2ee\\web" debug="0" reloadable="false" />
運(yùn)行原理圖