上一節我們介紹了項目的建立和Jar包的導入,接下來我們介紹項目配置的搭建。
架構搭建

詳解
配置檔案詳解
①db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:/crm
jdbc.user=root
jdbc.password=
②log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p [%t] - %m%n
③sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--導入MyBatis限制-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--為類起别名-->
<typeAliases>
<!--掃描包下的所有類和子包-->
<package name="com.crm.domain"></package>
</typeAliases>
<!--environments環境在Spring配置 這裡省略-->
<!--mapper标簽省略,在Spring中使用MyBatis動态代理-->
</configuration>
④applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--導入Spring限制-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置C3P0連接配接池 -->
<!-- 導入db.properties檔案 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置資料源-->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sqlSessionFactory-->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置資料庫連接配接池-->
<property name="dataSource" ref="dataSource"></property>
<!--導入sqlMapConfig.xml-->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
<!--MyBatis動态代理增強(掃描)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定基本包-->
<property name="basePackage" value="com.crm.dao"></property>
</bean>
</beans>
⑤springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--導入SpringMVC限制-->
<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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--自動掃描com.crm下的所有元件
@Component
@Controller 控制層
@Service 服務層
@Repository dao層
-->
<context:component-scan base-package="com.crm"></context:component-scan>
<!--
預設自動注冊RequestMappingHandlerMapping和RequestMappingHandlerAdapter兩個Bean
-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
⑥web.xml(重要)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--引入Spring容器,使容器随項目的啟動二建立-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--指定Spring配置檔案的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--處理Post中文送出亂碼-->
<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>
<!--配置SpringMVC-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定SpringMVC配置檔案的位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
1./* 攔截所有JSP .js .png .css 真的攔截 不建議使用
2.*.action,*.do 攔截以action結尾的請求 肯定能使用 使用在背景
3./ 攔截所有 不包括JSP 但是包括.js .png .css檔案 強烈建議使用 使用在前台 放行靜态資源
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--配置首頁-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>