文章目錄
IDEA 整合 SSM 框架
項目環境搭建
新建Maven 項目
補全目錄結構
配置 Tomcat
添加SSM整合所需要的全部依賴
<properties>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
</properties>
<!-- spring官方提供的依賴管理器,便於統一管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>5.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 引入測試所需的依賴,使用spring4.x版本整合需要引入junit4.12及以上 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- ################# Spring 相關依賴######################## -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<!-- 整合SpringMVC會自動引入其他所需的spring核心依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- 事務管理 AOP 編程依賴 -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<!-- Spring MVC 所需 內置 json 解析工具 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- #################數據庫相關依賴######################## -->
<!-- 添加mybatis依賴 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--mysql 驅動包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- 阿里巴巴 連接池工具 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<!-- 添加mybatis與sping整合依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!-- Mybatis 分頁插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
<!-- #################web工程 JSP 相關依賴######################## -->
<!-- 引入web工程所需的依賴 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- 添加JSTL標籤庫 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--文件上傳 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- ################ 其他 ##############-->
<!-- 阿里巴巴fastjson json 解析工具 , 可以配置替代jackson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
<!-- logback日誌依賴 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
日誌記錄
log4j.properties
#定義LOG輸出級別
log4j.rootLogger=INFO,Console,File
#定義日誌輸出目的地爲控制檯
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活地指定日誌輸出格式,下面一行是指定具體的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到達指定尺寸的時候產生一個新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定輸出目錄
log4j.appender.File.File = logs/ssm.log
#定義文件最大大小
log4j.appender.File.MaxFileSize = 10MB
#輸出所以日誌,如果換成DEBUG表示輸出DEBUG以上級別日誌
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
三層包結構
controller
service
dao/mapper
數據庫設計
sql 數據庫表
DROP DATABASE IF EXISTS `ssm-crm` ;
CREATE DATABASE `ssm-crm`;
USE `ssm-crm`;
-- ----------------------------
-- 數據字典表
-- ----------------------------
DROP TABLE IF EXISTS `base_dict`;
CREATE TABLE `base_dict` (
`dict_id` varchar(32) NOT NULL COMMENT '數據字典id(主鍵)',
`dict_type_code` varchar(10) NOT NULL COMMENT '數據字典類別代碼',
`dict_type_name` varchar(64) NOT NULL COMMENT '數據字典類別名稱',
`dict_item_name` varchar(64) NOT NULL COMMENT '數據字典項目名稱',
`dict_item_code` varchar(10) DEFAULT NULL COMMENT '數據字典項目代碼(可爲空)',
`dict_sort` int(10) DEFAULT NULL COMMENT '排序字段',
`dict_enable` char(1) NOT NULL COMMENT '1:使用 0:停用',
`dict_memo` varchar(64) DEFAULT NULL COMMENT '備註',
PRIMARY KEY (`dict_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- 數據字典表數據
-- ----------------------------
INSERT INTO `base_dict` VALUES ('1', '001', '客戶行業', '教育培訓 ', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('10', '003', '公司性質', '民企', null, '3', '1', null);
INSERT INTO `base_dict` VALUES ('12', '004', '年營業額', '1-10萬', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('13', '004', '年營業額', '10-20萬', null, '2', '1', null);
INSERT INTO `base_dict` VALUES ('14', '004', '年營業額', '20-50萬', null, '3', '1', null);
INSERT INTO `base_dict` VALUES ('15', '004', '年營業額', '50-100萬', null, '4', '1', null);
INSERT INTO `base_dict` VALUES ('16', '004', '年營業額', '100-500萬', null, '5', '1', null);
INSERT INTO `base_dict` VALUES ('17', '004', '年營業額', '500-1000萬', null, '6', '1', null);
INSERT INTO `base_dict` VALUES ('18', '005', '客戶狀態', '基礎客戶', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('19', '005', '客戶狀態', '潛在客戶', null, '2', '1', null);
INSERT INTO `base_dict` VALUES ('2', '001', '客戶行業', '電子商務', null, '2', '1', null);
INSERT INTO `base_dict` VALUES ('20', '005', '客戶狀態', '成功客戶', null, '3', '1', null);
INSERT INTO `base_dict` VALUES ('21', '005', '客戶狀態', '無效客戶', null, '4', '1', null);
INSERT INTO `base_dict` VALUES ('22', '006', '客戶級別', '普通客戶', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('23', '006', '客戶級別', 'VIP客戶', null, '2', '1', null);
INSERT INTO `base_dict` VALUES ('24', '007', '商機狀態', '意向客戶', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('25', '007', '商機狀態', '初步溝通', null, '2', '1', null);
INSERT INTO `base_dict` VALUES ('26', '007', '商機狀態', '深度溝通', null, '3', '1', null);
INSERT INTO `base_dict` VALUES ('27', '007', '商機狀態', '簽訂合同', null, '4', '1', null);
INSERT INTO `base_dict` VALUES ('3', '001', '客戶行業', '對外貿易', null, '3', '1', null);
INSERT INTO `base_dict` VALUES ('30', '008', '商機類型', '新業務', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('31', '008', '商機類型', '現有業務', null, '2', '1', null);
INSERT INTO `base_dict` VALUES ('32', '009', '商機來源', '電話營銷', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('33', '009', '商機來源', '網絡營銷', null, '2', '1', null);
INSERT INTO `base_dict` VALUES ('34', '009', '商機來源', '推廣活動', null, '3', '1', null);
INSERT INTO `base_dict` VALUES ('4', '001', '客戶行業', '酒店旅遊', null, '4', '1', null);
INSERT INTO `base_dict` VALUES ('5', '001', '客戶行業', '房地產', null, '5', '1', null);
INSERT INTO `base_dict` VALUES ('6', '002', '客戶信息來源', '電話營銷', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('7', '002', '客戶信息來源', '網絡營銷', null, '2', '1', null);
INSERT INTO `base_dict` VALUES ('8', '003', '公司性質', '合資', null, '1', '1', null);
INSERT INTO `base_dict` VALUES ('9', '003', '公司性質', '國企', null, '2', '1', null);
-- ----------------------------
-- 客戶信息表
-- ----------------------------
CREATE TABLE `customer`(
`cust_id` BIGINT PRIMARY KEY AUTO_INCREMENT,
`cust_name` VARCHAR(100) NOT NULL COMMENT '客戶姓名',
`cust_phonenum` VARCHAR(11) COMMENT '客戶聯繫方式',
`cust_gender` INT(1) COMMENT '客戶性別',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客戶信息來源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別',
`cust_address` VARCHAR(200) COMMENT '客戶地址',
`cust_picpath` VARCHAR(100) COMMENT '客戶圖片位置',
`createdate` DATE COMMENT '客戶信息創建時間'
)ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=utf8;;
-- ----------------------------
-- 客戶信息表 數據
-- ----------------------------
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'馬雲','13687613109',0,'杭州',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'馬化騰','13687613109',0,'廣州',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'李彥宏','13687613109',0,'北京',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'張龍','17823499999',0,'開封',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'趙虎','13687243109',0,'汴京',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'王朝','13687533109',0,'黑風寨',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'馬漢','13623313109',0,'花果山',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'展昭','13652343109',0,'高老莊',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'公孫勝','13535347109',0,'獅駝嶺',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'阮小七','13687609109',0,'流沙河',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'阮小五','11187613109',0,'通天河',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(6,2,22,'戴宗','13245613109',0,'小西天',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'武松','13687613554',0,'景陽岡',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'卡莎','13687611234',1,'虛空',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'薇恩','11234613109',1,'德瑪西亞',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'凱瑟琳','13623453109',1,'皮特沃夫',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'厄運小姐','13687456709',1,'比爾吉沃特',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'提莫','13687698769',0,'德瑪西亞',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'小炮','13681234109',1,'弗雷爾卓德',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'亞索','13623453109',0,'無畏先鋒',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'飛機','13653423109',0,'扭曲叢林',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'盧錫安','13132513109',0,'祖安',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'千珏','13687346309',1,'黑色玫瑰',NULL,SYSDATE());
INSERT INTO `customer` (`cust_source`,`cust_industry`,`cust_level`,`cust_name`,`cust_phonenum`,`cust_gender`,`cust_address`,`cust_picpath`,`createdate`) VALUES(7,1,23,'希維爾','13687524309',1,'艾歐尼亞',NULL,SYSDATE());
逆向工程 生成 Mapper 層
逆向工程依賴
<!-- 自動生成器核心插件 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
數據庫配置文件
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm-crm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
jdbc.username=root
jdbc.password=123456
jdbc.initialSize=5
jdbc.maxActive=10
jdbc.maxWait=3000
jdbc.maxIdle=8
jdbc.minIdle=3
自動生成配置文件
generatorConfig.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>
<properties resource="db.properties"/>
<context id="tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<!-- 不生成註釋 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的數據庫連接 -->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!-- java類型處理器 -->
<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- java模型創建器,是必須要的元素 -->
<javaModelGenerator targetPackage="cn.bdqn.entity" targetProject="src/main/java">
<!-- 是否對model添加 構造函數 -->
<property name="constructorBased" value="true"/>
<property name="enableSubPackages" value="false"/>
<!-- 是否取消生成 set 方法 -->
<property name="immutable" value="false"/>
<!-- 是否對類CHAR類型的列的數據進行trim操作 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目錄 爲每一個數據庫的表生成對應的SqlMap文件org.louis.hometutor.domain -->
<sqlMapGenerator targetPackage="cn.bdqn.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼 -->
<javaClientGenerator targetPackage="cn.bdqn.mapper"
targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 指定數據庫表 此處還有很多自定義配置,根據個人需求進行設置即可 -->
<table tableName="customer" domainObjectName="Customer"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
<!-- 如果設置爲true,生成的model類會直接使用column本身的名字,而不會再使用駝峯命名方法,比如BORN_DATE,生成的屬性名字就是BORN_DATE,而不會是bornDate -->
<property name="useActualColumnNames" value="flase"/>
</table>
</context>
</generatorConfiguration>
配置讀取java 目錄中的配置文件
pom文件中
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
自動生成插件
pom文件中
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
設計Spring基礎配置
Spring 核心配置文件
applicationContext-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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置掃描 本地配置文件 -->
<context:property-placeholder location="classpath:db.properties"
ignore-unresolvable="true" />
<!-- 配置數據源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化連接大小 -->
<property name="initialSize" value="${jdbc.initialSize}" />
<!-- 連接池最大使用連接數量 -->
<property name="maxActive" value="${jdbc.maxActive}" />
<!-- 連接池最小空閒 -->
<property name="minIdle" value="0" />
<!-- 獲取連接最大等待時間 -->
<property name="maxWait" value="60000" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打開removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分鐘 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 關閉abanded連接時輸出錯誤日誌 -->
<property name="logAbandoned" value="true" />
<!-- 監控數據庫 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<!-- myBatis文件 手動加載配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置數據庫表對應的java實體類 -->
<property name="typeAliasesPackage" value="cn.bdqn.entity" />
<!-- 加載 Mybatis 的核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 自動掃描entity目錄, 省掉Configuration.xml裏的手工配置 -->
<!-- <property name="mapperLocations" value="classpath:cn/bdqn/mapper/*.xml" /> -->
</bean>
<!-- 面向接口開發 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.bdqn.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
applicationContext-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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 註解掃描 service 包 -->
<context:component-scan base-package="cn.bdqn.service"/>
<!-- 事務 配置等 -->
</beans>
SpringMVC 核心配置文件
springmvc-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: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.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">
<!-- 掃描 controller 包 -->
<context:component-scan base-package="cn.bdqn.controller"/>
<!-- 註解組件 -->
<mvc:annotation-driven/>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置前端控制器 不要攔截 /static/** 的請求 -->
<mvc:resources mapping="/static/**" location="/static/"/>
<!-- SpringMVC上傳文件時,需要配置MultipartResolver處理器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<!-- 指定所上傳文件的總大小不能超過200KB。注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="1024000" />
<property name="maxInMemorySize" value="4096" />
</bean>
</beans>
Mybatis 的核心配置文件
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>
</configuration>
web.xml
<!-- 配置 Spring 核心容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application*.xml</param-value>
</context-param>
<!--監聽器 自動加載 spring核心 容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springmvc 的 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解決亂碼問題 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
測試controller 頁面
controller 層
@Controller
public class BaseController{
@RequestMapping("/hello")
public String hello(){
return "hello";
}
@RequestMapping("/")
public String baseUrl(){
return "customer";
}
}