SSM 框架整合-1

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";
    }
}
发布了50 篇原创文章 · 获赞 17 · 访问量 5111
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章