本次整合,真实有效,也是耗费了挺长时间才弄出来的,谢谢,喜欢的话,就喜欢喽!!!
平台是Windows10+eclipse+jdk1.8+spring4.2.4+mybatis3,先来个所有jar包的截图
这是所有的jar包,我相信很多人在整合框架的时候,最蒙蔽的就是jar包了,我也一样,但是这个东西真的要自己动手错几次才能有点感觉:jar包中最需要注意的就是mchange-commons-java-0.2.11.jar这个包,除此之外,我相信,小伙伴们还希望知道,组建jar包的原则,我就说一下自己的感受:
首先,既然是整合,每个单独的框架所需要的jar包是必须的,这个应该很容易理解,具体操作一般情况下,mybatis里面提供的所有jar包,照搬不误!!
spring的jar包,首先4+2(这个是必须的),就是四个基本包,外加两个日志包,当然实际上,目前只需要一个日志包,但是对于菜鸟的我们就不想那么多了,就是4+2,初次之外,是不是还有aop的包,事务包,当然,真正开发的时候,基本上或多或少都会遇见,所有,你也可以全部添加,好像是20个。我必须说一下,aop的那几个包一定要注意,因为不属于spring本身的包,需要从外面找!!!
然后,数据库连接池,驱动。
最后,就是整合了,mybatis-spring,其实这就完事了。。。。
//视图结构
jar包导入完成后,就是配置文件了,我们先理一下:spring的主配文件-applicationContext.xml;mybatis的主配文件-mybatis-config.xml;web应用主配文件web.xml;springMVC的主配文件-spring-mvc.xml。就是这些,当然其中的映射文件,我们记作mapper*.xml.
最基本的: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_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>Spring-mybatis</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置springmvc 的DispatcherServlet -->
<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:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
其实很简单,最核心的就是配置dispatcherServlet,当然也别忘了<init-param>
//applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<context:property-placeholder
location="classpath:db.properties" />
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"></property>
<property name="jdbcUrl" value="${db.url}"></property>
<property name="user" value="${db.username}"></property>
<property name="password" value="${db.passwd}"></property>
</bean>
<bean name="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation"
value="classpath:mybatis-config.xml"></property>
</bean>
<bean name="mapperfactory"
class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="mapperInterface"
value="com.axis.lance.PersonMapper"></property>
</bean>
<bean name="loginRegister" class="com.axis.service.LoginRegister">
</bean>
<bean name="persitence" class="com.axis.persitence.Persitence"></bean>
</beans>
最主要的是sqlSessionFactory和mapperfactory,mybatis官方推荐使用接口映射,所以,这个是针对接口映射的
//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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
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-4.2.xsd">
<context:component-scan
base-package="com.axis.controller"></context:component-scan>
<mvc:annotation-driven />
<bean name="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>
这里面也没什么好说的,但是<mvc:annotation-driven>是个亮点,配置这一个东西,相当于配置了另外两个必须的<bean>推荐指数好多星。
//mybatis-config.xml
<?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>
<mappers>
<mapper resource="com/axis/lance/person.xml" />
</mappers>
</configuration>
这个就不说了吧,整合的时候,主要web.xml,和applicationContext.xml
//db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/axis
db.username=root
db.passwd=lance
这样的话,配置就已经结束了,当然其中有很多不足之处,但是雏形以及确定了,剩下的就是优化了
最后给大家发一个GitHub链接,可以直接下载源码查看,代码中很多不足,完美没有各种容错机制,只是为了说明这个过程