首先,我们看一下每一层的内容:
- Dao层:
Mybatis的配置文件:SqlMapConfig.xml
不需要配置任何内容,需要有文件头。文件必须存在。
applicationContext-dao.xml:
mybatis整合spring,通过由spring创建数据库连接池,spring管理SqlSessionFactory、mapper代理对象。需要mybatis和spring的整合包。
- Service层:
applicationContext-service.xml:
所有的service实现类都放到spring容器中管理。并由spring管理事务。
- 表现层:
Springmvc框架,由springmvc管理controller。
Springmvc的三大组件。
我们开始正式整合:
1、web打包为war,其他dao、api、pojo、service都打jar包放到web中web-if下的lib文件夹下,所以整合文件都放到web的resources文件下即可(方便访问)
2、idea中建立resource文件夹
3、resource下建立两个文件夹,mybatis和spring
4、在mybatis文件夹下建立SqlMapConfig.xml文件,此文件不需要任何配置,但需要有文件头:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEconfiguration
PUBLIC"-//mybatis.org//DTDConfig3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>5
5、spring文件夹下建立applicationContext-dao.xml文件,在classpath下(resource下)建立conf文件夹,并在conf文件夹下建立db.properties文件
applicationContext-dao.xml配置如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--数据库连接池-->
<!--加载配置文件-->
<context:property-placeholderlocation="classpath:conf/db.properties"/>
<!--数据库连接池-->
<beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.username}"/>
<propertyname="password"value="${jdbc.password}"/>
<propertyname="driverClassName"value="${jdbc.driver}"/>
<propertyname="maxActive"value="10"/>
<propertyname="minIdle"value="5"/>
</bean>
<!--让spring管理sqlsessionfactory使用mybatis和spring整合包中的-->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据库连接池-->
<propertyname="dataSource"ref="dataSource"/>
<!--加载mybatis的全局配置文件-->
<propertyname="configLocation"value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>
<!--配置包扫描器-->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.jarvis.mapper"/>
</bean>
</beans>
Db.properties内容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jarvis_test_db?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
6、spring下建立applicationContext-service.xml配置文件
其配置如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--配置包扫描器-->
<context:component-scanbase-package="com.jarvis.service"/>
</beans>
7、配置事务,在spring下建立applicationContext-trans.xml文件
其配置如下:
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--事务管理器-->
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--数据源,dao中配置的数据源-->
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!--通知-->
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<!--传播行为-->
<!--以这些开头的开启事务-->
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="create*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<!--以这些开头的不开启事务-->
<tx:methodname="find*"propagation="SUPPORTS"read-only="true"/>
<tx:methodname="select*"propagation="SUPPORTS"read-only="true"/>
<tx:methodname="get*"propagation="SUPPORTS"read-only="true"/>
</tx:attributes>
</tx:advice>
<!--切面,..表示包含子包-->
<aop:config>
<aop:advisoradvice-ref="txAdvice"
pointcut="execution(*com.jarvis.service..*.*(..))"/>
</aop:config>
</beans>
8、配置表现层
其配置为:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--包扫描-->
<context:component-scanbase-package="com.jarvis.controller"/>
<!--注解驱动,包含处理器映射器和处理器适配器(共有三大组件,和下面的视图解析器)-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/jsp/"/>
<propertyname="suffix"value=".jsp"/>
</bean>
</beans>
9、配置web.xml,在web层的main文件夹下新建webapp文件夹,在webapp中建立WEB-INF文件夹,在WEB-INF下建立web.xml文件
Web.xml配置为:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
<display-name>jarvis-manager</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--加载spring容器,初始化spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--解决post乱码-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--springmvc的前端控制器-->
<servlet>
<servlet-name>jarvis-manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--contextConfigLocation不是必须的,如果不配置contextConfigLocation,springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml"-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--拦截请求-->
<servlet-mapping>
<servlet-name>jarvis-manager</servlet-name>
<!--"/"表示拦截所有请求(会拦截静态资源js、css等),不包括jsp-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
注意:
在idea下搭建会有多余对的文件夹以及类,删除即可,否则会报错