简单的SpringMVC+Mybatis整合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xj80231314/article/details/50810902

先申明几点:

1.这个整合呢,我也是看的其他大神的各种资料来学习的。

2.SpringMVC框架的原理、能做什么、有什么长短处什么的,我这边就不写了,我相信你在学这个框架的时候,你肯定得先去了解。

3.我尽量简短的写,然后呢,我尽可能的把已经整合好的框架源码丢上来,到时候你们下载下去自己研究,这样呢,记忆深刻一点。

4.这次整合呢,我也是第一次,肯定会有一些你们看不懂的问题,到时候呢,加我QQ:728793963我们相互学习。


直接来干货吧!!!


1.jar包准备


2.web.xml详细配置如下:

扫描二维码关注公众号,回复: 4844798 查看本文章

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</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>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

3.sping-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
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.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.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">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="net.fhxy.web" />
<!-- <bean name="/index" class="net.fhxy.web.UserController" ></bean> -->
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<!-- <bean id="mappingJacksonHttpMessageConverter" -->
<!-- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> -->
<!-- <property name="supportedMediaTypes"> -->
<!-- <list> -->
<!-- <value>text/html;charset=UTF-8</value> -->
<!-- </list> -->
<!-- </property> -->
<!-- </bean> -->
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 自定义参数绑定 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="net.fhxy.pojo.converter.UserDateConverter"></bean>
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<!-- <bean id="multipartResolver"   -->
<!--         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   -->
        <!-- 默认编码 -->
<!--         <property name="defaultEncoding" value="utf-8" />   -->
        <!-- 文件大小最大值 -->
<!--         <property name="maxUploadSize" value="10485760000" />   -->
        <!-- 内存中的最大值 -->
<!--         <property name="maxInMemorySize" value="40960" />   -->
<!--     </bean>  -->

</beans>

4.SpringMVC和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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
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.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.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="net.fhxy" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<!-- <property name="initialSize" value="${initialSize}"></property> -->
<!-- 连接池最大数量 -->
<!-- <property name="maxActive" value="${maxActive}"></property> -->
<!-- 连接池最大空闲 -->
<!-- <property name="maxIdle" value="${maxIdle}"></property> -->
<!-- 连接池最小空闲 -->
<!-- <property name="minIdle" value="${minIdle}"></property> -->
<!-- 获取连接最大等待时间 -->
<!-- <property name="maxWait" value="${maxWait}"></property> -->
</bean>


<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:net/fhxy/pojo/mapping/*.xml"></property>
</bean>


<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.fhxy.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>


<!--事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userDAO" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="net.fhxy.dao.IUserDAO" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>


<!-- <bean id="userDAO" class="net.fhxy.dao.UserDAO"> -->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
<!-- </bean> -->
<!-- <bean id="userService" class="net.fhxy.service.impl.UserServiceImpl"> -->
<!-- <property name="userDAO" ref="userDAO"></property> -->
<!-- </bean> -->
<!-- <bean id="userController" class="net.fhxy.web.UserController"> -->
<!-- <property name="userService" ref="userService"></property> -->
<!-- </bean> -->
</beans>

5.jdbc配置如下:(jdbc的连接呢,常用的三种数据库,参照这个写就可以)



#-------------------------------------------------------------------------------
# Oracle Settings


#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@server:1521:orcl
#jdbc.username=admin
#jdbc.password=admin
#
#hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
#hibernate.show_sql=true
#hibernate.hbm2ddl.auto=update
#-------------------------------------------------------------------------------
# MySQL Settings
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/fhxy
jdbc.username=root
jdbc.password=admin


#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#hibernate.show_sql=true
#hibernate.hbm2ddl.auto=update


#-------------------------------------------------------------------------------
# SQL Server Settings
# jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
# jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=AssetDataBase#创建数据库"AssetDataBase"
# jdbc.username=root
# jdbc.password=admin


# hibernate.dialect=org.hibernate.dialect.SQLServerDialect
# hibernate.show_sql=true
# hibernate.hbm2ddl.auto=update

6.log日志文件的配置如下:(如果大家有空的话,可以去研究一下log4j这个的详细配置,对以后的项目开发很有好处!!!)

log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n

7.整合项目架构图如下:

8.注明

1>这个框架呢,我是在公司整理的,jdk不一样,所以你们到时候用的时候,首先要该jdk,我这个是1.7的,tomcat是7.0的

2>如果遇到权限问题呢,删掉字节码文件,重新编译一下就ok,数据库的sql我忘了拷贝回来,就两张表,动手写一下几分钟的事情,他们之间有一个外键关联。

3>如果还是有问题的话,可以问我,最好是先百度谷歌,这样你自己的记忆也深刻一点。

4>源码问我要吧,有问题Q我!


猜你喜欢

转载自blog.csdn.net/xj80231314/article/details/50810902
今日推荐