关于spring+springmvc+mybatis+maven的框架整合实例
2017-01-11 15:48
639 查看
最近在学习基于maven环境的ssm框架整合,小弟不才,学的不好,然后在百度上搜了一下ssm框架整合的实例,然后按照上面一片点赞很高的进行ctrl+C,ctrl+V,然后再改自己的参数,但是改了后,不是这错,就是那不对。最后让项目经理找原因,项目经理说:配的乱七八糟的, 然后给了我springmvc的框架教程,有需要的在这里http://www.yiibai.com/spring/spring_web_mvc_framework.html,还有一个是搭建简单的springmvc框架教程http://blog.csdn.net/aitcax/article/details/41543829,最后在自己
学习后,总于配置成功。
下面小弟将把自己的配置一步步贴出来,供大家参考。
第一步:创建基于maven的项目:
其中的pom.xml里的配置文件内容根据各自的需要加入就行,这里我不再贴出。
第二步;在resources里咱们的配置文件了
这是里面所有的配置文件
先写第一个:sqlMapConfig.xml
第二步就是编写咱们的applicationContext-dao.xml
第三部编写咱们的applicationContext-service.xml
第四步:编写咱们的applicationContext-transaction.xml
这些都编辑好以后, 编辑咱们的web.xml
最后一步就是编写咱们的Spring-SpringMVC.xml
这些都配置好以后,就可以添加到Tomcat里运行了。 写的太过简单,大家包涵就行。
学习后,总于配置成功。
下面小弟将把自己的配置一步步贴出来,供大家参考。
第一步:创建基于maven的项目:
其中的pom.xml里的配置文件内容根据各自的需要加入就行,这里我不再贴出。
第二步;在resources里咱们的配置文件了
这是里面所有的配置文件
先写第一个:sqlMapConfig.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> <typeAliases> <typeAlias alias="User" type="com.jmt.pojo.User" /> <!-- <typeAlias alias="Role" type="com.jmt.pojo.Role" /> <typeAlias alias="Menu" type="com.jmt.pojo.Menu" /> <typeAlias alias="Office" type="com.jmt.pojo.Office" /> --> <!-- <typeAlias alias="User" type="com.jmt.pojo.SysUser" /> <typeAlias alias="User" type="com.jmt.pojo.SysUser" /> --> </typeAliases> <mappers> <mapper resource="com/jmt/mapping/loginDaoMapper.xml" /> <!-- <mapper resource="com/jmt/mapping/SysRoleMapper.xml" /> <mapper resource="com/jmt/mapping/SysMenuMapper.xml" /> <mapper resource="com/jmt/mapping/SysOfficeMapper.xml" /> <mapper resource="com/jmt/mapping/SysRoleOfficeMapper.xml" /> <mapper resource="com/jmt/mapping/SysUserRoleMapper.xml" /> <mapper resource="com/jmt/mapping/SysRoleMenuMapper.xml" /> --> </mappers> </configuration>
第二步就是编写咱们的applicationContext-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置整合mybatis过程 --> <!-- 1.配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 2.数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <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> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置MyBaties全局配置文件:sqlMapConfig.xml--> <property name="configLocation" value="classpath:sqlMapConfig.xml" /> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="com.jmt.pojo" /> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.jmt.dao" /> </bean> </beans>
第三部编写咱们的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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描service包下所有使用注解的类型 --> <context:component-scan base-package="com.jmt.service" /> </beans>
第四步:编写咱们的applicationContext-transaction.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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" /> </bean> <!-- 配置基于注解的声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
这些都编辑好以后, 编辑咱们的web.xml
<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" id="WebApp_ID" version="3.0"> <!-- springmvc整合 --> <!-- spring MVC的核心就是DispatcherServlet,使用springMVC的第一步就是将下面的servlet放入web.xml servlet-name属性非常重要,默认情况下,DispatchServlet会加载这个名字-servlet.xml的文件,如下,就会加载 dispather-servlet.xml,也是在WEN-INF目录下。 --> <!--配置监听器--> <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>Spring-SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring-SpringMVC.xml</param-value> </init-param> <!--随sevlet的启动加载--> <load-on-startup>1</load-on-startup> </servlet> <!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认servlet会处理所有的请求,包括静态资源 --> <servlet-mapping> <servlet-name>Spring-SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符集过滤器 --> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name>ssmDemo</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
最后一步就是编写咱们的Spring-SpringMVC.xml
<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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.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" default-lazy-init="true"> <!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 --> <!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 --> <mvc:resources location="/*.html" mapping="/**.html" /> <mvc:resources location="/css/*" mapping="/css/**" /> <mvc:resources location="/js/*" mapping="/js/**" /> <mvc:resources location="/images/*" mapping="/images/**" /> <!-- 添加注解驱动 --> <mvc:annotation-driven /> <!-- 默认扫描的包路径 --> <context:component-scan base-package="com.jmt" /> <!-- mvc:view-controller可以在不需要Controller处理request的情况,转向到设置的View --> <!-- 像下面这样设置,如果请求为/,则不通过controller,而直接解析为/index.jsp --> <mvc:view-controller path="/" view-name="index" /> <!-- 总错误处理 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView"> <value>/jsp/error/admin-error.jsp</value> </property> <property name="defaultStatusCode"> <value>500</value> </property> <property name="warnLogCategory"> <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver </value> </property> </bean> <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <!-- 配置jsp路径前缀 --> <property name="prefix" value="/jsp/"></property> <!-- 配置URl后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
这些都配置好以后,就可以添加到Tomcat里运行了。 写的太过简单,大家包涵就行。
相关文章推荐
- SSM框架整合(Maven+Spring+SpringMVC+Mybatis+mysql 附demo实例)
- Spring+SpringMVC+MyBatis+Maven框架整合
- Spring+SpringMVC+MyBatis+Maven框架整合
- Maven+Spring+Spring MVC+MyBatis+MySQL整合SSM框架
- springmvc+mybatis+maven整合实例
- Eclipse Maven建web项目 与SSM三大框架整合教程(Spring+SpringMVC+MyBatis)
- Spring+SpringMVC+MyBatis+Maven框架整合
- Spring+SpringMVC+MyBatis+MySql框架整合实例
- maven项目下的Spring+SpringMVC+Mybatis整合实例
- Spring+SpringMVC+MyBatis+Maven框架整合
- Spring+SpringMVC+MyBatis+Maven框架整合
- 基于Maven的Mybatis+spring+springMVC框架整合(mapper代理方式)
- Spring MVC+Mybatis+Maven+Velocity+Mysql整合实例
- SSM框架整合(Spring + SpringMVC + Mybatis + Maven + IDEA)
- SpringMVC+Mybatis框架整合java源码 maven mysql
- SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
- Spring+SpringMVC+MyBatis+Maven框架整合
- Spring+SpringMVC+MyBatis+Maven框架整合
- Spring MVC+Mybatis+Maven+Velocity+Mysql整合实例