springmvc+spring+mybatis整合心得

来源:互联网 发布:便携电脑推荐 知乎 编辑:程序博客网 时间:2024/06/05 08:16

搭建公司门户网站。

很久都没有搭建这样的环境了,今天再次搭建环境感到很兴奋,上次配置的是spring+mybatis的环境,用的是配置文件的方式进行配置的,这次改用了注解的方式来配置。

首先去下载spring的jar,如果你去spring的官网是下载不到spring的jar包的。

在这里附上spring下载地址:http://maven.springframework.org/release/org/springframework/spring/

开始搭建环境,创建项目,导入jar,我采用的是最新的spring框架,还需导入aspectjrt.jar  aspectjweaver.jar   cglib-nodep-2.1_3.jar    commons-dbcp-1.4.jar    commons-logging-1.1.1.jar    commons-pool-1.6.jar    jstl.jar    standard.jar    junit-4.4.jar    log4j-1.2.17.jar


然后导入mybatis的jar

附上mybatis的下载地址:https://github.com/mybatis/mybatis-3/releases


加入配置文件:

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"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"><!-- 配置数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://192.168.1.10:3306/yq"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:mybatis-config.xml"></property><property name="dataSource" ref="dataSource"></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.yq.dao" /></bean></beans>


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><typeAliases><typeAlias type="com.yq.domain.YqCominforsheet" alias="YqCominforsheet"/><typeAlias type="com.yq.domain.YqCorporaevents" alias="YqCorporaevents"/><typeAlias type="com.yq.domain.YqRecruitment" alias="YqRecruitment"/></typeAliases><mappers><mapper resource="com/yq/mapper/YqCominforsheetMapper.xml"/><mapper resource="com/yq/mapper/YqCorporaeventsMapper.xml"/><mapper resource="com/yq/mapper/YqRecruitmentMapper.xml"/></mappers></configuration>


spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        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"><context:component-scan base-package="com.yq" /><mvc:annotation-driven /><mvc:resources location="/styleAndImage/**" mapping="/styleAndImage/**" /><beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /><!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/" /><property name="suffix" value=".jsp" /></bean></beans>


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>yqmh</display-name><!-- 配置过滤器 --><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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext*.xml</param-value></context-param><!-- 配置上下文监听 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</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></web-app>



遇到的问题是在页面中所有的图片在访问.action请求时,然后所有页面图片无法显示。

解决办法是在所有页面写图片路径加上${pageContext.request.contextPath}/





0 0
原创粉丝点击