Spring+SpringMVC+Mybatis 整合【非maven项目】
来源:互联网 发布:java应用 编辑:程序博客网 时间:2024/05/20 00:12
入行一年多了,总是在用框架,却很少自己搭建过框架,最近有时间终于自己搭了一套简单的框架,分享一下,以便后面在用的到。
项目采用Spring4.0和Mybatis3.3的版本。
源码下载地址
1、准备工作。
首先是准备工作,创建数据库连接地址的配置文件jdbc.properties,日志配置文件log4j.properties,Mybatis特性配置文件mybatis-config.xml。
jdbc.properties文件内容:
#MySQL数据库连接地址jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/firstSpringMVCproject?useUnicode=true&characterEncoding=utf8jdbc.username=rootjdbc.password=root#定义初始连接数initialSize=0#定义最大连接数maxActive=20#定义最大空闲maxIdle=20#定义最小空闲minIdle=1#定义最长等待时间maxWait=60000
log4j.properties文件内容:
log4j.rootLogger=INFO,Console,File,DEBUG#定义日志输出目的地为控制台 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out #可以灵活地指定日志输出格式,下面一行是指定具体的格式 log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到达指定尺寸的时候产生一个新的文件 log4j.appender.File = org.apache.log4j.RollingFileAppender #指定输出目录 log4j.appender.File.File = logs/ssm.log #定义文件最大大小 log4j.appender.File.MaxFileSize = 10MB # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n #输出sql语句及参数log4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUGlog4j.logger.java.sql.ResultSet=DEBUG
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> <settings> <setting name="cacheEnabled" value="true" /><!-- 全局映射器启用缓存 --> <setting name="useGeneratedKeys" value="true" /> <setting name="callSettersOnNulls" value="true"/><!--返回null的字段 --> <setting name="defaultExecutorType" value="REUSE" /> <setting name="defaultExecutorType" value="REUSE" /> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings></configuration>
2、导入相关jar包
创建好配置文件之后,导入相关的Spring,SpringMVC,Mybatis的jar包,【jar包链接地址,可自行下载】
3、创建Spring配置文件
创建Spring配置文件spring-context.xml,有些人喜欢起名applicationContext.xml,这个都行,根据个人喜好随意起名。犹豫Spring是一个容器,所以可以将任何框架都已Bean的形式注入到Spring容器中,这里就采用这种方式将数据源(dataSource)也以Bean的形式注入Spring中。值得注意的是在Spring的配置文件中,是不扫描controller的,只扫描service层和dao层还有mapper层。如果项目中没有用到aop或者事务部分的东西,可以暂时不用配置。注意扫包的路径配置正确。
spring-context.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" default-lazy-init="true"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!-- 自动扫描 --> <!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 --> <!-- <context:component-scan base-package="com.weikai" /> --> <context:component-scan base-package="com.weikai"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <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> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:mapper/**/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.weikai.*.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 加载service --> <context:component-scan base-package="com.weikai.*.service" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>至此实际上已经完成了Spring和Mybatis的整合,接下来需要加入SpringMVC的配置。
4、配置SpringMVC配置文件
接下来就是配置spring-mvc.xml,将SpringMVC配置在项目中。需要注意的是 一定要在配置文件中 打开注解扫描,不然请求不到controller地址,其次就是视图的配置,配置jsp,html单视图多视图都可以(可参考之前文章)。
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" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="com.weikai" use-default-filters="false"> <!-- base-package 如果多个,用“,”分隔 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 开启Spring注解扫描的功能 这个一定要打开,不然扫不到相关注解--> <mvc:annotation-driven /> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> JSON转换器 </list> </property> </bean> --> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="order" value="0" /> <property name="prefix" value="/views/"/> <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> <!-- 对静态资源文件的访问, 将无法mapping到Controller的path交给default servlet handler处理 --> <mvc:default-servlet-handler /> <!-- 静态资源映射 --> <mvc:resources mapping="/static/*" location="/static/" cache-period="31536000"/> <!-- 定义无Controller的path<->view直接映射 --> <mvc:view-controller path="/" view-name="/index"/> </beans>至此三大框架已经整合完毕,接下来需要在web.xml中注册。
5、注册web.xml
再启动项目之前,需要将刚才配置的几个xml文件注册到web.xml中,让tomcat启动的时候来加载容器。
因为Spring是一个容器,后期会向其中加入更多的配置内容进去,所以web.xml中采用了通配符的方式来配置.
web.xml文件内容:
<?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"> <display-name>Archetype Created Web Application</display-name> <!-- Spring和mybatis的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-context.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配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring-context*.xml</param-value> </context-param> <!-- log4j日志文件配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!-- 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>springServlet</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>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <error-page> <error-code>500</error-code> <location>/views/common/500.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/views/common/404.jsp</location> </error-page></web-app>
至此所以配置工作已经全部完成,可以启动项目。若项目启动成功,并能到首页,则说明配置没有问题。若不能欢迎留下你的问题。
6、项目测试
连接数据库,对库中单表进行操作,并将数据返回页面。即在项目中写一套查寻方法,然后通过SpringMVC的http请求来访问项目,验证SpringMVC是否配置正确。
代码已上传可下载查看。下载地址
第一个项目搭建完毕,可以后续向其中加入shiro验证或者dubbo等配置。
- Spring+SpringMVC+Mybatis 整合【非maven项目】
- SpringMvc+Spring+Mybatis+Maven整合
- spring springmvc mybatis maven整合
- Springmvc+spring+maven+Mybatis整合
- SpringMvc+Spring+Mybatis+Maven整合
- SpringMVC+Spring+Mybatis+Maven整合
- MAVEN整合Spring+SpringMVC+Mybatis
- SpringMvc+Spring+MyBatis+Maven整合
- spring+springmvc+mybatis+maven 整合
- Spring+SpringMVC+MyBatis+Maven整合
- maven SpringMVC,Spring,Mybatis整合
- maven+springmvc+spring+mybatis整合
- maven+spring+springmvc+mybatis整合
- maven+Spring+SpringMVC+Mybatis 整合
- maven+mybatis+spring+springmvc整合
- Spring+SpringMVC+Mybatis+maven整合
- SpringMVC-Mybatis-Maven项目整合
- maven项目下的Spring+SpringMVC+Mybatis整合实例
- 新的一天
- 利用Jquery的load函数实现页面的动态加载
- git 基本命令操作。
- 一个简单的demo说明集成reactnative到android项目
- python连接redis库发布消息
- Spring+SpringMVC+Mybatis 整合【非maven项目】
- Linux 信号signal处理机制【转】
- 交叉熵代价函数
- python读取Excel到Mysql
- 2017第八届广州国际新能源汽车工业展览会会刊(参展商名录)
- 【MySQL笔记】Error Code: 1175
- 在 CentOS 上安装 zookeeper-3.4.11 服务
- uri 与 url 的区别
- 爬虫小工具合集 | 不会编程也能爬数据