SSM框架快速搭建
2017-04-17 16:20
316 查看
**
4000
**
首先配置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_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>New</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置log4j --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>WEB-INF/cfg/log4j.properties</param-value> </context-param> <!-- spring配置开始 --> <!-- 配置Spring启动容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指明容器的配置文件(spring配置文件 applicationcontext.xml)--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/cfg/applicationContext.xml</param-value> </context-param> <!-- spring配置结束 --> <!-- springMVC配置开始 --> <!-- springMVC启动也需要配置 DispatcherServlet分发--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化参数 也就是springMVC的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/cfg/springmvc.xml</param-value> </init-param> <!-- 表示容器启动的时候就加载文件初始化 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置DispatcherServlet响应路径 --> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- springMVC配置结束 --> <!-- 配置编码的过滤器 --> <filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
4000
**
还有需要加错误页面的自行配置就好了,这里不多说,还有log4j.properties文件 需要的就加上去。
**
# # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) # # The five logging levels used by Log are (in order): # # 1. DEBUG (the least serious) # 2. INFO # 3. WARN # 4. ERROR # 5. FATAL (the most serious) # Set root logger level to WARN and append to stdout log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n # Print only messages of level ERROR or above in the package noModule. log4j.logger.noModule=FATAL #log4j.logger.com.opensymphony.xwork2=DEBUG #log4j.logger.org.apache.struts2=DEBUG
还有数据库的db.properties文件 不用连接池的可以不加,别忘了dbcp包
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl jdbc.username=scott jdbc.password=admin #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000
下面接着配置Spring 配置SpringXML文件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: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.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"> <!-- spring使用注解 配置扫描 com.ys下的扫描 但是在springmvc里边已经扫描一次了 这时就要加上属性 use-default-filters="true" 这个属性就是使用默认的扫描 默认就扫描com.ys下所有 设置为false 在下边配置需要扫描的部分--> <!-- 现在的配置就只会扫描带@Service和@Repository注解的类 --> <context:component-scan base-package="com.ys" use-default-filters="false"> <!-- org.springframework.stereotype.Service就是注解@service 这个注解使用在service里 所以就是扫描service包 org.springframework.stereotype.Repository Repository仓库--> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <!-- 读取properties文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="/WEB-INF/cfg/db.properties" /> </bean> <!-- 配置连接池数据源 文档搜索BasicDataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- results in a setDriverClassName(String) call --> <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> <!-- 配置 sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 加载Mybatis全局配置文件 --> <property name="configLocation" value="/WEB-INF/cfg/mybatis.cfg.xml"/> </bean> <!-- 扫描的dao包(映射文件) 本身应该在mybatis里 现在交给spring --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ys.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 事务配置 --> <!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启事务 引用transactionManager --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
接着就是SpringMVC了 springmvc.xml
。<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.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"> <!-- 静态资源无法访问时 需要配置 由tomcat处理 需要在web.xml中配置--> <mvc:default-servlet e1ef -handler/> <!-- 开启注解(注解的处理器适配器和处理器映射器) 同 注解的处理器适配器 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 注解的处理器映射器 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> --> <mvc:annotation-driven/> <!-- 配置controller 扫描controller--> <context:component-scan base-package="com.ys.controller"/> <bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- jsp文件的前缀和后缀 --> <property name="prefix" value="/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--start:使用Jackson 2.x的配置,需要导入的jar包:jackson-core-xxx.jar、jackson-annotations-xxx.jar、jackson-databind-xxx.jar--> <!--通过处理器映射DefaultAnnotationHandlerMapping来开启支持@Controller注解--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!--通过处理器适配器AnnotationMethodHandlerAdapter来开启支持@RequestMapping注解--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <!-- 设置返回字符串编码 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name = "supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- json转换器 --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> </beans>
我这添加了MVC的Jackson配置 不需要的不用加,使用Jackson的话controller方法加@RequestBody注解 。 最后就是Mybatis配置了,mybatis.cfg.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> <!-- 批量定义别名 ,指定包名,自动扫描包中的类,别名即为类名,首字母大小写无所谓--> <package name="com.ys.dao"/> </typeAliases> <mappers> <!-- 通过扫描包的方式来进行批量加载映射文件 --> <package name="com.ys.mapping"/> </mappers> </configuration>
mybatis配置很少 ,大多我都放到Spring里了 使用映射文件的直接扫描包就好了, mybatis这配置太少了 注掉也没什么影响 直接dao层里用注解就好了。 很基本的配置 好多东西都没有加上 aop、拦截器等等 有需要的加上就好了,就写这些吧。 觉得有用的给个赞。
。相关文章推荐
- maven中ssm框架快速搭建
- myeclipse+mybatis-generator-gui-0.6.1快速搭建ssm框架并且实现登录
- ssm环境下快速搭建shiro框架
- 快速搭建springboot框架以及整合ssm+shiro+安装Rabbitmq和Erlang、Mysql下载与配置
- SSM框架之SpringAOP快速搭建
- Spring boot 入门系列(一):快速搭建一个简单web系统(简单的SSM框架)
- (SSM框架)SpringMVC+Spring4+Mybatis+Maven环境快速搭建开发案例
- 【SpringBoot】10分钟快速搭建ssm框架
- SSM框架快速搭建
- Maven+SSM框架搭建(二)
- SSM框架搭建中的相关配置
- ssm框架搭建之配置文件
- SpringBoot搭建SSM框架的简单功能实现源码
- SSM框架搭建流程(本小白个人用)
- 基于SSM框架的Maven项目的搭建
- IDEA搭建SSM框架 以及环境配置
- ssm+shiro基础框架搭建(二)
- maven搭建ssm分模块框架
- 使用IDEA 4000 maven搭建SSM框架
- 移动端之路-阿里Weex框架快速体验与环境搭建