ssm 各层的配置文件
1、spring与mybatis整合的配置文件为springmvc-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--注解扫描包路径-->
<context:component-scan base-package="com.test.dao"/>
<!--开启注解配置-->
<context:annotation-config/>
<!--关联数据库文件-->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClass}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!--c3p0连接池的私有属性-->
<!--<property name="maxPoolSize" value="10"/>-->
<!--<property name="minPoolSize" value="1"/>-->
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatisConfig.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.test.dao"/>
</bean>
</beans>
2、spring与service整合的配置文件:springmvc-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"
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">
<!-- 扫描service相关的bean -->
<context:component-scan base-package="com.test.service" />
<!--<!– 配置事务管理器 –>-->
<!--<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
<!--<!– 注入数据库连接池 –>-->
<!--<property name="dataSource" ref="dataSource" />-->
<!--</bean>-->
</beans>
3、spring与controller整合的配置文件springmvc-servlet.xml
备注:
注意事项1:添加如下信息时,执行报如下500的错,将如下信息注释掉,访问正常,故先注释掉
注意事项2: 前缀WEB-INF/jsp/ 前一定要加 “/” ,
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--注解扫描包路径-->
<context:component-scan base-package="com.jin.controller"/>
<!--使用注解驱动,配置请求控制器和适配器-->
<mvc:annotation-driven>
<!--<mvc:message-converters register-defaults="true">-->
<!--<bean class="org.springframework.http.converter.StringHttpMessageConverter">-->
<!--<constructor-arg value="UTF-8"/>-->
<!--</bean>-->
<!--<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
<!--<property name="objectMapper">-->
<!--<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">-->
<!--<property name="failOnEmptyBeans" value="false"/>-->
<!--</bean>-->
<!--</property>-->
<!--</bean>-->
<!--</mvc:message-converters>-->
</mvc:annotation-driven>
<!--配置静态处理 css-->
<mvc:default-servlet-handler/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/" />
<!--后缀-->
<property name="suffix" value=".jsp" />
</bean>
</beans>
错误1:
<mvc:annotation-driven>
<!--<mvc:message-converters register-defaults="true">--> <!--<bean class="org.springframework.http.converter.StringHttpMessageConverter">--> <!--<constructor-arg value="UTF-8"/>--> <!--</bean>--> <!--<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">--> <!--<property name="objectMapper">--> <!--<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">--> <!--<property name="failOnEmptyBeans" value="false"/>--> <!--</bean>--> <!--</property>--> <!--</bean>--> <!--</mvc:message-converters>-->
</mvc:annotation-driven>
错误2:
#
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--注解扫描包路径-->
<context:component-scan base-package="com.test.controller"/>
<!--使用注解驱动,配置请求控制器和适配器-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--配置静态处理 css-->
<mvc:default-servlet-handler/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="WEB-INF/jsp/" />
<!--后缀-->
<property name="suffix" value=".jsp" />
</bean>
</beans>
4、应用程序上下文:appllicationConfig.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<import resource="classpath:springmvc-dao.xml"/>
<import resource="classpath:springmvc-service.xml"/>
<import resource="classpath:springmvc-servlet.xml"/>
</beans>
5、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置请求转发器-->
<servlet>
<servlet-name>springmvc-01</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置servlet加载文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-01</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--字符集过滤-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
+++++++++++++++++++++++++++++++++++++
mybatisConfig.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><!-- 配置 -->
<properties resource="dbconfig.properties"></properties>
<settings>
<!--mybatis标准日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--指定日志输出格式,需配置properties-->
<!--<setting name="logImpl" value="LOG4J"/>-->
</settings>
<typeAliases>
<typeAlias type="com.jin.entity" alias="User"/>
</typeAliases>
<mappers>
<mapper resource="com/test/dao/UserMapper.xml"/>
</mappers><!-- 映射器 -->
</configuration>
还没有评论,来说两句吧...