前言
学习完了三大框架,但是始终没有做一个真正的整合,这次打算在这里记录下。
依赖
简单梳理下这三大框架和一些必须的依赖:
- 单元测试
junit4,5的话不太了解,这个有机会再看看
- web相关的包,分别是
javax.servlet-api、jsp-api 和jstl-api
- 数据库相关的,我这里用的是mysql,所以驱动用的是
mysql-connector-java,以及相关的连接池druid
- mybatis框架需要的
mybatis和mybatis-spring
- spring需要的就…太多了
依赖搞定之后就可以上手了,其实三个框架先配哪一个我个人觉得问题不大,我个人习惯是先创建好数据库,然后在项目中写好对应的Controller、Dao、Service等基本骨架,然后开搞。
Spring
我首先选择配置spring,这样我只需要一个spring的配置文件,然后之后用注解的方式将所有的对象交给IOC容器来管理。所以首先在resource下面创建一个applicationContext.xml文件,写好约束并且配置好开启注解扫描。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?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: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/context http://www.springframework.org/schema/context/spring-context.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">
<context:component-scan base-package="cn.chenlangping"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
|
然后随便找个服务类,进行测试,看是否可以使用IOC容器获取对象即可。
Spring到此就完成了。
SpringMVC
第一步,配置web.xml,分别写好三大组件——filter、servlet和listener(listener是为了和spring框架进行整合,如果单独测试可以暂时不需要listener,我下面是带了listener的版本):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app> <display-name>Archetype Created Web Application</display-name>
<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>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
</web-app>
|
第二步,到对应的springmvc.xml文件中,配置好四步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.chenlangping"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean>
<mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" />
<mvc:annotation-driven/>
</beans>
|
第三步,如果你在第一步中没有配置监听器,就配置监听器,这样就能让servletContext在加载的时候加载一下applicationContext.xml,也就是能让ioc容器生效。到此就可以通过在controller里面进行配置来调用service对象了。
即SpringMVC和Spring的结合就此完成。
Mybatis
第一步,配置核心数据库的连接内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?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> <properties resource="jdbcConfig.properties"></properties> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"/> <dataSource type="pooled"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments>
<mappers> <package name="cn.chenlangping.dao"/> </mappers> </configuration>
|
第二步,直接进行测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class TestMybatis {
@Test public void testMybatis() throws IOException { InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = factory.openSession(); AccountDao mapper = session.getMapper(AccountDao.class); List<Account> list = mapper.findAll(); for (Account account : list) { System.out.println(account); } session.close(); inputStream.close(); } }
|
如果能获得数据库的内容,说明连接成功。
最后只需要将mybatis和前面两者进行整合就可以了,思路是这样的,mybatis会帮我们生成代理对象,所以我们只需要让spring的IOC容器替我们管理这些代理对象即可完成整合了。核心思想是需要配置三件套:
- 连接池,这个不多说,肯定需要
- SqlSessionFactory,也是必须的,有了它就可以创建sqlSession
- 配置接口所在的包。这里最难理解,是因为需要有了接口所在的包才能够生成代理对象。
上面的第一点和第三点,其实就是相当于之前单独的mybatis的主配置文件,这么一讲是不是更容易理解了呢?
所以我们只需要在applicationContext中加入三个对象,让它们收到IOC容器的管理即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <!--数据库连接池--> <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="jdbc:mysql://....."/> <property name="username" value="root"/> <property name="password" value="......"/> </bean>
<!--sqlSessionFactory对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource"/> </bean>
<!--要扫描的包--> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.chenlangping.dao"/> </bean>
|
配置完这一步我们可以发现,其实之前的SqlMapConfig.xml已经完全没用了,删除即可。
到此,三大框架整合完毕。