1.new web project(而不是web service project)
2.lib文件夹放入所有jar包
build path === 添加所有的jar包
*****重要 添加servlet-api.jar这个jar包(引用tomcat/lib文件夹下面的),不添加无法使用servlet类,必须引用
3.build path把jar调整和tomcat的jar版本一致
4.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>xianrenzhang</display-name> //描述性说明
<filter> //字符编码过滤器
<filter-name>charSetFilter</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>
</filter>
<filter-mapping>
<filter-name>charSetFilter</filter-name
<!--这里不能留空或者直接写 \' / \' ,否者不起作用-->
<url-pattern>/*</url-pattern>
</filter-mapping>
//////////////<context-param>
作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。
param-name 设定上下文的参数名称。必须是唯一名称
param-value 设定的参数名称的值
//////////////
<context-param> //声明上下文,配置spring文件
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/zzq/config/spring/applicationContext-*.xml</param-value> //指定spring的配置文件
</context-param>
//Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次,随web应用的停止而销毁。
//主要作用是: 做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet> //声明serverlet参数,
<!-- servlet名称,与servlet-mapping中的servlet-name必须一致 -->
<servlet-name>mvc</servlet-name>
<!-- servlet类所在的包路径,即上面T所在包路径 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/zzq/config/springmvc/spring-mvc.xml</param-value> //springmvc配置文件
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- servlet名称,与上面中的servlet-name必须一致 -->
<servlet-name>mvc</servlet-name>
<!-- servlet过滤的路径,也就是你在jsp中访问servlet时应该在action中填写的东西 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>toIndex.do</welcome-file>
</welcome-file-list>
</web-app>
=====================================================================================================================================================================================
5.spring整合mybatis的配置文件
applicationContext-mybatis
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:com/zzq/config/commons/*.properties"/>
<!-- 配置数据源, 必须是javax.sql.DataSource的实现类 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.driver}"></property>
<property name="url" value="${mysql.url}"></property>
<property name="username" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
</bean>
<!-- 会话工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 属性注入 -->
<!-- 数据源, 如何访问数据库 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 要读取的映射文件 -->
<property name="mapperLocations">
<array>
<value>classpath:com/zzq/mapper/*.xml</value>
</array>
</property>
</bean>
<!-- 自动扫描 将Mapper接口生成代理注入到Spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zzq.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
=====================================================================================================================================================================================
6.spring配置文件
applicationContext-service.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置服务对象的配置文件 -->
//在XML中配置了这个标签后,spring可以自动扫描base-package下面或者子包下面的java文件,
//如果扫描有@Component @Service @Controller等这些注解的类,则把这些类注册为bean。
<context:component-scan base-package="com.zzq.service"></context:component-scan>
</beans>
=================================================================================================================================================================
7.事物配置
applicationContext-sw.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置声明式事务的配置文件 -->
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务通知, sw命名空间 -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"
rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution( * com.zzq.service.*.*(..) )"/>
</aop:config>
</beans>
=================================================================================================================================================================
8.springmvc的配置
spring-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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置springmvc相关信息 -->
<!-- 开启注解。注解驱动,设置默认的核心组件 -->
<mvc:annotation-driven />
<!-- 配置拦截器 -->
<!-- <mvc:interceptors>
登录拦截器 对非登陆用户过滤 以及不合法的路径控制拦截
<mvc:interceptor>
<mvc:mapping path="/*"/>
<mvc:exclude-mapping path="/logout.do"/>
<mvc:exclude-mapping path="/login.do"/>
<mvc:exclude-mapping path="/login.jsp"/>
<mvc:exclude-mapping path="/toIndex.do"/>
<mvc:exclude-mapping path="/sendVerifyCode.do"/>
<mvc:exclude-mapping path="/findUserByUserName.do"/>
<mvc:exclude-mapping path="/findUserByPhone.do"/>
<mvc:exclude-mapping path="/loginByVerifyCode.do"/>
<mvc:exclude-mapping path="/AddUser.do"/>
<bean class="com.bjsxt.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
登陆日至拦截器
<mvc:interceptor>
<mvc:mapping path="/login.do"/>
<mvc:mapping path="/loginByVerifyCode.do"/>
<bean class="com.bjsxt.interceptor.LoginlogsInterceptor"></bean>
</mvc:interceptor>
操作日至拦截器
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/login.do"/>
<mvc:exclude-mapping path="/logout.do"/>
<mvc:exclude-mapping path="/jsp/login.jsp"/>
<mvc:exclude-mapping path="/toIndex.do"/>
<bean class="com.bjsxt.interceptor.OperatorlogsInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors> -->
<!-- 静态资源映射 -->
<mvc:resources mapping="/load/**" location="/WEB-INF/load/" />
<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
<mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
<mvc:resources mapping="/img/**" location="/WEB-INF/img/" />
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置文件上传试图解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880"></property>
<property name="maxInMemorySize" value="1024"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<!--设置注解扫描, 扫描控制器代码. -->
<context:component-scan base-package="com.zzq.controller"></context:component-scan>
</beans>
=================================================================================================================================================================
9. db.properties
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://127.0.0.1:3306/jsras?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false/数据库名称
mysql.username=xxx
mysql.password=xxx
10. 在src下面编写controller、service、mapper等
11.前台调用:$.post(G.HOST + "eleManager2/queryAllBookcase.do",function(data){}); //eleManager2是工程名称
controller层:
@RequestMapping(value = "/queryAllBookcase", produces = "text/html;charset=utf-8")
@ResponseBody
public String queryAllBookcase() throws JsonProcessingException {}