本文主要是介绍Spring MVC4+maven3+MyBatis3+Oracle11和mysql项目搭建,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
环境
MyEclipse 10、JDK 1.7、Tomcat 7.0
搭建
一、新建maven工程,步骤略,建好后目录结构如下图:
二、pom文件配置
[html] view plain copy
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.alan</groupId>
- <artifactId>smis</artifactId>
- <packaging>war</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>smis</name>
- <url>http://maven.apache.org</url>
- <properties>
- <!-- spring版本号 -->
- <spring.version>4.0.6.RELEASE</spring.version>
- <!-- mybatis版本号 -->
- <mybatis.version>3.2.7</mybatis.version>
- <!-- log4j日志文件管理包版本 -->
- <slf4j.version>1.7.21</slf4j.version>
- <log4j.version>1.2.17</log4j.version>
- <sitemesh.version>3.0.0</sitemesh.version>
- </properties>
- <dependencies>
- <!-- spring核心包 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- mybatis核心包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <!-- mybatis/spring包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.4</version>
- </dependency>
- <!-- Oracle驱动包 -->
- <dependency>
- <groupId>com.oracle</groupId>
- <artifactId>ojdbc6</artifactId>
- <version>11.2.0.1.0</version>
- </dependency>
- <!-- junit测试包 -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!-- 阿里巴巴数据源包 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.20</version>
- </dependency>
- <!-- json数据 -->
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
- <!-- 上传下载 -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.4</version>
- </dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>1.9</version>
- </dependency>
- <!-- JSTL标签包开始 -->
- <dependency>
- <groupId>jstl</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>taglibs</groupId>
- <artifactId>standard</artifactId>
- <version>1.1.2</version>
- </dependency>
- <!-- 日志文件管理包 -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- sitemesh -->
- <dependency>
- <groupId>org.sitemesh</groupId>
- <artifactId>sitemesh</artifactId>
- <version>${sitemesh.version}</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>smis</finalName>
- </build>
- </project>
三、web.xml配置
[html] view plain copy
- <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>smis</display-name>
- <!-- 读取spring配置文件 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:application/spring-mybatis.xml
- </param-value>
- </context-param>
- <!-- Spring字符集过滤器 -->
- <filter>
- <filter-name>SpringEncodingFilter</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>SpringEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 日志记录 -->
- <context-param>
- <!-- 日志配置文件路径 -->
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:properties/log4j.properties</param-value>
- </context-param>
- <context-param>
- <!-- 日志页面的刷新间隔 -->
- <param-name>log4jRefreshInterval</param-name>
- <param-value>6000</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- springMVC核心配置 -->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:application/spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <!-- 错误跳转页面 -->
- <error-page>
- <!-- 路径不正确 -->
- <error-code>404</error-code>
- <location>/WEB-INF/error/404.jsp</location>
- </error-page>
- <error-page>
- <!-- 没有访问权限,访问被禁止 -->
- <error-code>405</error-code>
- <location>/WEB-INF/error/405.jsp</location>
- </error-page>
- <error-page>
- <!-- 内部错误 -->
- <error-code>500</error-code>
- <location>/WEB-INF/error/500.jsp</location>
- </error-page>
- </web-app>
四、spring-mvc.xml配置
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:p="http://www.springframework.org/schema/p"
- 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:websocket="http://www.springframework.org/schema/websocket"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- <!-- 添加注解驱动 -->
- <mvc:annotation-driven />
- <!-- 扫描controller(controller层注入) -->
- <context:component-scan base-package="com.alan.smis.controller"/>
- <!-- 避免IE在ajax请求时,返回json出现下载 -->
- <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <!-- 配置国际化拦截器 -->
- <!-- 拦截参数 -->
- <bean id="messageSource"
- class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
- <property name="basename" value="classpath:/messages/messages" />
- <property name="defaultEncoding" value="UTF-8" />
- </bean>
- <bean id="localeChangeInterceptor"
- class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
- <property name="paramName" value="lang" />
- </bean>
- <bean id="localeResolver"
- class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
- <bean id="requestMappingHandlerMapping"
- class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
- <!-- 对模型视图添加前后缀 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="order" value="1" />
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- <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>
五、spring-mybatis.xml配置
[html] view plain copy
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- <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="${jdbc.initialSize}"></property>
- <!-- 连接池最大数量 -->
- <property name="maxActive" value="${jdbc.maxActive}"></property>
- <!-- 连接池最大空闲 -->
- <property name="maxIdle" value="${jdbc.maxIdle}"></property>
- <!-- 连接池最小空闲 -->
- <property name="minIdle" value="${jdbc.minIdle}"></property>
- <!-- 获取连接最大等待时间 -->
- <property name="maxWait" value="${jdbc.maxWait}"></property>
- <!-- 连接属性 -->
- <property name="connectionProperties">
- <value>clientEncoding=UTF-8</value>
- </property>
- </bean>
- <!-- 加载jdbc -->
- <context:property-placeholder location="classpath:properties/jdbc.properties"/>
- <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <!-- 自动扫描mapping.xml文件 -->
- <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
- </bean>
- <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.alan.smis.mapper" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
- </bean>
- <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- </beans>
六、jdbc配置
[html] view plain copy
- jdbc.driver=oracle.jdbc.driver.OracleDriver
- jdbc.url=jdbc:oracle:thin:@192.168.1.236:1521:greendb
- jdbc.username=alan
- jdbc.password=alan123
- jdbc.initialSize=0
- jdbc.maxActive=100
- jdbc.maxIdle=20
- jdbc.minIdle=1
- jdbc.maxWait=60000
七、log4j配置
[html] view plain copy
- log4j.rootLogger=INFO,Console,File
- #\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
- log4j.appender.Console=org.apache.log4j.ConsoleAppender
- log4j.appender.Console.Target=System.out
- #\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F
- log4j.appender.Console.layout = org.apache.log4j.PatternLayout
- log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
- #\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
- log4j.appender.File = org.apache.log4j.RollingFileAppender
- #\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
- log4j.appender.File.File = logs/ssm.log
- #\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
- log4j.appender.File.MaxFileSize = 10MB
- # \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
- 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
mysql数据库:
jdbc配置
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://10.221.10.111:8080/db_zsl
username=demao
password=demao
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
pom配置
<!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.31</version></dependency>
这篇关于Spring MVC4+maven3+MyBatis3+Oracle11和mysql项目搭建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!