ssm框架搭建的配置文件以及單元測試中遇到的問題

自己不懂後臺還硬要跟着視頻做ssm,當然就是一步一個坑bug,但bug永遠要靠自己去解決。
我是根據網易雲課堂-雷老師的《SSM高級整合視頻》做出來,具體的類之類的自己去寫,我只把坑和配置文件(我只做到了13課時單元測試)列一下。
1、eclipse爲什麼會出現maven依賴包的圖標顏色變暗的這種情況,且引入類時報錯;
原因:在pom-xml中,

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
</dependency>
這裏面不能隨意的添加<scope>test</scope>;但是我不知道爲什麼!

2、給主表插入信息時,報錯:“Cannot add or update a child row, MySQL外鍵錯誤解析”;
原因:在做外鍵約束時,把tpl_emp的主鍵作爲了外鍵,導致給主表增加信息報錯;

3、The errors below were detected when validating the file “spring-tx-4.3.xsd” via the file “applicationContext.xml”.
In most cases these errors can be detected by validating “spring-tx-4.3.xsd” directly. However it is possible that errors will only occur when spring-tx-4.3.xsd is validated in the context of applicationContext.xml.
解決:把版本號去掉,不知道爲什麼;

4、Cannot make a static reference to the non-static field employeeMapper
原因:多了一個static,它並不是一個靜態的;

	public static List<Employee> getAll() {
		// TODO Auto-generated method stub
		return employeeMapper.selectByExampleWithDept(null);
	}

5、java.lang.NullPointerException at com.dingpiao.test.MvcTest.testPage(MvcTest.java:43)
原因:這個錯誤簡直太隱蔽了,我請教了一圈的人都沒搞出來,自己試出來了。其實就是沒有走controller
解決:查看一下你的springMvc配置中的這段:

<context:component-scan base-package="com.dingpiao" use-default-filters="false">
<!-- 因爲不能掃描所有,只掃描控制器 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

但我寫的是include-filter,因爲我複製別人的也不知道啥意思,而且這個標籤不能缺少,否則報一樣的錯。

6、底部面板中的controller報錯:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.dingpiao.service.EmployeeService’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
單元測試的junit中報錯:java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
原因:在你的單元測試文件(我的是com.dinpiao.test包下的MvcTest.java)中,@ContextConfiguration(locations = {"classpath:applicationContext*.xml", "file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})多了一個*
解決:應該去掉,當時查閱錯誤時就添加上了,以爲可以解決問題。。。

接下來附上我的配置文件吧。。。。。。

先上結構圖

在這裏插入圖片描述

pom.xml(這個應該是maven管理依賴的文件吧)
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.dingpiao</groupId>
	<artifactId>ssm-dingpiao</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<!-- 引入項目依賴jar包 -->
	<!-- spring mvc -->
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependencies>
		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
			<version>1.4.9</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.7.RELEASE</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.7.RELEASE</version>
		</dependency>


		<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>4.3.7.RELEASE</version>
		</dependency>


		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.2</version>
		</dependency>

		<!-- MyBatis整合spring的適配器 -->
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.1</version>
		</dependency>

		<!-- 數據庫連接池 ,驅動 -->
		<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
		<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.4</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>


		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

		<!-- MBG -->
		<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.5</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version> 3.2.4.RELEASE  </version>
			<scope>provided</scope>
		</dependency>

		<!-- 引入分頁插件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.0.0</version>
		</dependency>


		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.8.8</version>
		</dependency>



	</dependencies>


</project>
applicationContext.xml(spring文件)
<?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:tx="http://www.springframework.org/schema/tx"
	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.3.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<!-- spring的配置文件,主要配置與業務邏輯有關的,比如讀數據源等-->
	
    <!-- spring的容器,不掃控制器,其他都掃-->
    <context:component-scan base-package="com.dingpiao">
   		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
	<context:property-placeholder location="classpath:jdbc.properties" />
	<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!-- 配置和MyBatis的整合 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
		 <!-- 指定mybatis的全局配置文件的位置 -->
		 <property name="configLocation" value="classpath:mybatis-config.xml"></property>
		 <property name="dataSource" ref="pooledDataSource"></property>
		 <!--指定mybatis.mapper文件位置 -->
		 <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
	</bean>
	
	<!-- 配置掃描器:將mybatis接口的實現加入到ioc控制中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	   <!-- 掃描所有dao的接口的實現,加入ioc容器中 -->
	   <property name="basePackage" value="com.dingpiao.dao"></property>
    </bean>
   
    <!-- 事務控制器的配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	    <!-- 控制數據源 -->
	    <property name="dataSource" ref="pooledDataSource"></property>
    </bean>
    
	<!-- 可以執行批量操作的sqlSession -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
		<constructor-arg name="executorType" value="BATCH"></constructor-arg>
	</bean>
	
    <!-- 開啓基於註解的實物,用的是配置的方式 -->
    <aop:config>
	    <!-- 切入點表達式 -->
	    <aop:pointcut expression="execution(* com.dingpiao.service..*(..))" id="txPoint"/>
	    <!-- 配置事務增強 -->
	    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    
    <!-- 配置事務增強 事務如何切入 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 傳播行爲 -->
			<tx:method name="*"/>
			<tx:method name="get*" read-only="true" />
		</tx:attributes>
	</tx:advice>
    
</beans>
dispatcherServlet-servlet.xml(springMvc文件)
<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.3.xsd 
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<!-- springMVC的配置文件,包含網絡跳轉邏輯的控制,配置 -->
	<context:component-scan base-package="com.dingpiao" use-default-filters="false">
		<!-- 因爲不能掃描所有,只掃描控制器 -->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
	</context:component-scan>
	
	<!-- 配置視圖解析器 方便頁面返回 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 兩個標準配置  -->
	<!-- 將springmvc不能處理的請求交給tomacat,實現動態靜態資源都能訪問 -->
	<mvc:default-servlet-handler />
	<!-- 能支持springmvc更高級的一些功能,比如jsr303校驗,映射動態請求(最重要) -->
	<mvc:annotation-driven />
</beans>
mybatic-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="mapUnderscoreToCamelCase" value="true" />
	</settings>
	<typeAliases>
		<package name="com.dingpiao.bean" />
	</typeAliases>
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor">
		</plugin>
	</plugins>
</configuration>
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"
	id="WebApp_ID" version="3.0">
	<!-- 1、啓動spring的容器 -->
	<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>
	
	<!-- 2、spring的前端控制器 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 3、字符編碼過濾器 -->
	<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>forceRequestEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>forceResponseEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 4、使用Rest風格的URl,將頁面普通的post轉換成delete或者put -->
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/ssm_dingpiao?useUnicode=true&characterEncoding=UTF-8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
mbg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		
		<!-- 配置數據庫鏈接 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/ssm_dingpiao"
			userId="root" password="root">
		</jdbcConnection>

		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- 指定javaBean生成的位置 -->
		<javaModelGenerator
			targetPackage="com.dingpiao.bean" targetProject=".\src\main\java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!-- 指定sql映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="mapper"
			targetProject=".\src\main\resources">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<!-- 指定dao接口生成的位置,mapper接口 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.dingpiao.dao" targetProject=".\src\main\java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>

		<!-- table指定每個表的生成策略 -->
		<table tableName="tpl_emp" domainObjectName="Employee"></table>
		<table tableName="tpl_dept" domainObjectName="Department"></table>

	</context>
</generatorConfiguration>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章