SSM 项目基本配置

系列文章目录

提示:记录SSM 项目的配置文件配置步骤
可以创建配置模板,减少代码量,但是必须了解每个配置的作用


文章目录

  • 系列文章目录
  • 一、创建数据库和表单
  • 二、导入依赖
  • 三 、springmvc.xml
    • 1、表头
    • 2、开启组件扫描 ,扫描控制层
    • 3、 开启注解
    • 4、配置视图解析器
    • 5、 配置文件上传
    • 6、配置文件上传的基本属性
    • 7、配置拦截器
    • 完整代码
  • 四 、spring-context.xml
    • 1、表头
    • 2、 组件扫描 ,扫描业务层的实现类
    • 3、引入外部数据源
    • 4、数据库连接池
    • 5、配置日志
    • 6、配置sqlsession
    • 7、指定mapper 层接口存放的位置
    • 8、事务
    • 完整代码
  • 5、 web.xml
    • 1、 默认访问首页
    • 2、配置中文乱码
    • 3、配置druid监控sql、防火墙等
    • 4、配置监听器,读写spring-context.xml文件
    • 5、配置DispatcherServlet 读取springmvc.xml文件
    • 6、配置404
    • 7、完整代码
  • 总结


一、创建数据库和表单

CREATE DATABASE /*!32312 IF NOT EXISTS*/`crm` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `crm`;

/*Table structure for table `account` */

DROP TABLE IF EXISTS `account`;

CREATE TABLE `account` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `username` varchar(20) DEFAULT NULL COMMENT '姓名',
  `pwd` varchar(40) DEFAULT NULL COMMENT '密码',
  `img_url` varchar(200) DEFAULT 'default.png' COMMENT '头像地址',
  `create_time` datetime DEFAULT NULL COMMENT '注册时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `role` int(11) DEFAULT NULL COMMENT '角色:1超级管理员,0普通管理员',
  `status` int(11) DEFAULT NULL COMMENT '状态: 1启用,0禁用',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

/*Data for the table `account` */

insert  into `account`(`id`,`username`,`pwd`,`img_url`,`create_time`,`update_time`,`role`,`status`) values (1,'admin','c2f365c379ea6da2c2e42675fae561ac','default.png','2022-06-17 11:02:29','2022-06-21 23:08:43',1,1),(2,'lisi','c2f365c379ea6da2c2e42675fae561ac','default.png','2021-06-17 11:02:50','2022-06-22 07:35:23',0,1),(3,'zhangsan','c2f365c379ea6da2c2e42675fae561ac','default.png','2022-06-18 16:27:14',NULL,0,0),(4,'wangwu','c2f365c379ea6da2c2e42675fae561ac','default.png','2022-06-20 14:31:05',NULL,0,1);

/*Table structure for table `dept` */

DROP TABLE IF EXISTS `dept`;

CREATE TABLE `dept` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(20) DEFAULT NULL COMMENT '部门名称',
  `loc` varchar(200) DEFAULT NULL COMMENT '部门地址',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

/*Data for the table `dept` */

insert  into `dept`(`id`,`name`,`loc`) values (1,'Java开发部','武汉市江夏区久阳科技园5楼'),(2,'Java研发部','长沙市芙蓉区鲁巷广场4楼'),(3,'UI部','石家庄市石头区石头广场3楼'),(7,'JS开发部','石家庄市石头区石头广场3楼'),(8,'人力资源','广东省佛山市耀阳区万达广场'),(9,'这里是小卖部','小卖部小卖部小卖部小卖部');

/*Table structure for table `user` */

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `username` varchar(20) DEFAULT NULL COMMENT '客户姓名',
  `birthday` datetime DEFAULT NULL COMMENT '生日',
  `sex` varchar(2) DEFAULT NULL COMMENT '1男、0女',
  `tel` varchar(20) DEFAULT NULL COMMENT '手机',
  `sal` float(10,2) DEFAULT NULL COMMENT '薪资',
  `profession` varchar(2) DEFAULT NULL COMMENT '1攻城狮、2程序猿、3,码龙',
  `address` varchar(200) DEFAULT NULL COMMENT '客户住址',
  `remark` varchar(200) DEFAULT NULL COMMENT '备注',
  `dept_id` bigint(20) DEFAULT NULL COMMENT '部门编号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

/*Data for the table `user` */

insert  into `user`(`id`,`username`,`birthday`,`sex`,`tel`,`sal`,`profession`,`address`,`remark`,`dept_id`) values (1,'大骚跨','2020-03-08 00:00:00','1','18812342278',2000.00,'3','湖北省武汉市江夏区光谷大道','哈哈',2),(2,'凤姐的姐我拼命的写','2020-03-15 00:00:00','0','13887654321',30004.00,'1','湖北省武汉市江夏区光谷大道汤逊湖街道','呜呼呼',2),(5,'傻了','2020-03-12 00:00:00','0','13812345699',3000.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(7,'阿达','2020-03-12 00:00:00','1','13812345699',300023.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(8,'大根儿','2020-03-12 00:00:00','0','15812345699',3000.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(9,'雪亿雕','2020-03-10 00:00:00','0','13812345690',5000.00,'2','湖北省武汉市江夏区光谷','哈哈',3),(10,'叶仁界','2020-03-12 00:00:00','1','13812312312',30000.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(11,'胡图图','2010-03-12 00:00:00','1','13812345749',30000.00,'3','翻斗大街翻斗花园翻斗小区5号','哈哈',2),(12,'小丁丁的丁丁','2020-03-12 00:00:00','0','13812345690',999999.00,'2','湖北省武汉市江夏区光谷大道','我是小丁丁的丁丁',2),(13,'老丁丁','2020-03-17 00:00:00','1','13865748984',3000.00,'2','湖北省武汉市江夏区光谷大道久阳科技园','真的够细',3),(14,'雪亿','2020-03-11 00:00:00','0','13812345690',3000.00,'2','湖北省武汉市江夏区光谷','哈哈',2),(15,'吴顺意','2020-03-12 00:00:00','0','13812345766',3000.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(16,'旺仔','2010-05-12 00:00:00','1','13812345128',2000.00,'3','旺旺碎冰冰旺旺小小酥','哈哈',2),(17,'海绵宝宝','2020-03-12 00:00:00','1','13935647890',3000.00,'2','湖北省武汉市江夏区光谷大道','蟹黄堡真好吃',2),(18,'雪亿','2020-03-11 00:00:00','0','13812345695',3000.00,'2','湖北省武汉市江夏区光谷','哈哈',2),(19,'小鸟','2000-03-12 00:00:00','1','13512345666',30000.00,'1','湖北省武汉市江夏区武鸣校区','哈哈',1),(20,'凤姐的爷','2020-03-12 00:00:00','0','13838383838',3000.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(21,'子龙','2020-03-11 00:00:00','0','13812345695',3000.00,'2','湖北省武汉市江夏区光谷','哈哈',2),(22,'吴顺意','2020-03-12 00:00:00','0','13817345766',3000.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(23,'超人不想飞','2020-03-12 00:00:00','1','15812345888',3000.00,'2','湖北省武汉市江夏区光谷大道','交接',1),(24,'小小鸟','2020-03-12 00:00:00','0','15915915915',8888.00,'2','湖北省武汉市江夏区纸坊','哈哈',2),(25,'壮壮哥','2009-03-12 00:00:00','1','13813245749',35000.00,'3','翻斗大街翻斗花园翻斗小区7号','哈哈',2),(26,'东兴耀阳','2020-03-11 00:00:00','0','13812345695',3000.00,'2','湖北省武汉市江夏区光谷','哈哈',2),(27,'丁丁','2020-03-08 00:00:00','1','13865748988',50000.00,'1','湖北省武汉市江夏区光谷大道久阳科技园','真的够细',1),(28,'丁丁','2020-03-12 00:00:00','1','13814523679',30000.00,'1','湖北省武汉市江夏区光谷大道','哈哈',2),(29,'耀阳','2020-03-11 00:00:00','0','13812345695',3000.00,'2','湖北省武汉市江夏区光谷','哈哈',2),(30,'测试数据一','2050-03-12 00:00:00','0','17648522861',1.00,'2','湖北省武汉市江夏区光谷大大大道','remark',3),(31,'继康','2020-03-12 00:00:00','1','15857522736',2000.00,'3','湖北省武汉市江夏区光谷大道','我爱老丁',3),(32,'阿达的哥','2020-03-12 00:00:00','1','13869874563',300023.00,'2','湖北省武汉市江夏区光谷大道','哈哈',1),(33,'汪汪','2020-03-11 00:00:00','0','13812345695',3000.00,'2','湖北省武汉市江夏区光谷','哈哈',2),(34,'王钰隆','2020-06-22 00:00:00','0','13833344455',6000.00,'2','湖北省襄阳市蜜汁汉堡店','我是老八',2),(35,'翠花','1970-03-12 00:00:00','0','13718345666',999999.00,'3','湖北省武汉市江夏区无赖小区','嘿嘿',2),(36,'老根儿','2020-03-12 00:00:00','0','15812345699',3000.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(37,'嘤嘤嘤','2020-03-12 00:00:00','1','15812345999',3000.00,'2','湖北省武汉市江夏区光谷大道','草',2),(38,'小姐姐','2000-03-12 00:00:00','0','13812345666',9999.00,'2','湖北省武汉市江夏区光谷大道大唐沐足','哈哈',1),(39,'丁真','2020-03-12 00:00:00','0','13574185269',3000.00,'2','萨达撒飒飒阿打算打11','哈哈哈哈',2),(40,'爷的凤姐','2020-03-12 00:00:00','0','13838383837',3000.00,'2','湖北省武汉市江夏区光谷大道','哈哈',2),(41,'凤姐的奶','2020-03-12 00:00:00','0','13812345625',3000.00,'2','湖北省武汉市江夏区光谷','哈哈',2),(42,'我是林北','2020-03-11 00:00:00','0','13812345695',3000.00,'2','湖北省武汉市江夏区光谷','哈哈',2),(43,'赵薇杨','2020-03-12 00:00:00','0','13743274590',2000.00,'1','安徽省宣城市宁国市南霁翔','哈哈',1),(44,'希瓦娜','2020-03-12 00:00:00','0','13814523679',30000.00,'1','湖北省武汉市江夏区光谷大道','哈哈',2),(45,'呆呆','2020-03-12 00:00:00','0','13869874563',6666.00,'2','湖北省武汉市江夏区光谷大道','哈哈',1),(46,'健康哥哥','2009-03-12 00:00:00','1','13813245549',2000.00,'1','翻斗大街翻斗花园翻斗小区阳光幼儿园','哈哈',2),(47,'顶针巨毒','2020-03-12 00:00:00','0','15812345699',3000.00,'3','湖北省武汉市江夏区光谷大道','哈哈',2),(48,'汪武阳','1970-03-12 00:00:00','1','13718345566',5.10,'1','湖北省武汉市江夏区无赖小区','嘿嘿',2);

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

思路 : 预估有三个表 user , account , dept ,主键都设置为自增

二、导入依赖

spring+springmvc相关的依赖

  • spring-webmvc
  • spring-jdbc
  • spring-context-support
  • spring-aspects

mybatis 核心包

  • mybatis

mybatis分页插件

  • pagehelper

spring和mybatis集合包

  • mybatis-spring

mysql驱动包

  • mysql-connector-java

log4j-core日志

  • log4j-core
  • commons-logging
  • log4j

引入lombok

  • lombok

引入jackson包

  • jackson-databind
  • javax.servlet-api

引入hutool工具类

  • hutool-all

文件上传包

  • commons-fileupload

引入druid数据库连接池

  • druid

引入hibernate数据校验

  • hibernate-validator


<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.0modelVersion>

  <groupId>com.powernodegroupId>
  <artifactId>SSMProjectartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>SSMProjectname>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>

  	
    <spring.version>5.3.4spring.version>
    <mybatis.version>3.5.7mybatis.version>
    <pageHelper.version>5.2.0pageHelper.version>
    <mysql.version>5.1.49mysql.version>
    <mybatis-spring.version>2.0.6mybatis-spring.version>
    <log4j-core.version>2.14.1log4j-core.version>
    <logging.version>1.2logging.version>
    <log4j.version>1.2.17log4j.version>
    <lombok.version>1.18.20lombok.version>
    <jackson.version>2.12.5jackson.version>
    <fileUpload.version>1.4fileUpload.version>
    <servlet.version>3.0.1servlet.version>
    <hutool.version>5.7.11hutool.version>
    <druid.version>1.2.8druid.version>
    <hibernate-validator.version>6.2.3.Finalhibernate-validator.version>
  properties>

  
  <dependencies>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>${spring.version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>${mybatis.version}version>
    dependency>
    
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>${pageHelper.version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>${mybatis-spring.version}version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>${mysql.version}version>
    dependency>
    
    <dependency>
      <groupId>org.apache.logging.log4jgroupId>
      <artifactId>log4j-coreartifactId>
      <version>${log4j-core.version}version>
    dependency>
    <dependency>
      <groupId>commons-logginggroupId>
      <artifactId>commons-loggingartifactId>
      <version>${logging.version}version>
    dependency>
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>${log4j.version}version>
    dependency>
    
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>${lombok.version}version>
    dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>${jackson.version}version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>${servlet.version}version>
    dependency>
    
    <dependency>
      <groupId>cn.hutoolgroupId>
      <artifactId>hutool-allartifactId>
      <version>${hutool.version}version>
    dependency>
    
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>${fileUpload.version}version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>${druid.version}version>
    dependency>
    
    <dependency>
      <groupId>org.hibernategroupId>
      <artifactId>hibernate-validatorartifactId>
      <version>${hibernate-validator.version}version>
    dependency>
  dependencies>


  <build>
    <resources>
      <resource>
        <directory>src/main/javadirectory>
        <includes>
          <include>**/*.xmlinclude>
        includes>
      resource>
    resources>
  build>

project>

三 、springmvc.xml

1、表头


<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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

beans>

2、开启组件扫描 ,扫描控制层


<context:component-scan base-package="com.powernode.controller"/>

3、 开启注解

  
    <mvc:annotation-driven/>

4、配置视图解析器

 
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/">property>
        <property name="suffix" value=".html">property>
    bean>

5、 配置文件上传

  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        <property name="defaultEncoding" value="UTF-8">property>
        
        <property name="maxUploadSizePerFile" value="1048576">property>
        
        <property name="maxUploadSize" value="10485760">property>
    bean>

6、配置文件上传的基本属性


  <bean id="uploadBean" class="com.powernode.common.UploadBean">
        <property name="baseUrl" value="D:/07.ssm/upload">property>
    bean>
    

7、配置拦截器

  
 <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            
            <mvc:exclude-mapping path="/account/getLogin.do"/>
            
            <mvc:exclude-mapping path="/captcha/**"/>
            
            <mvc:exclude-mapping path="/forward/toLogin.do"/>
            
            <bean class="com.powernode.interceptors.AuthInterceptor">bean>
        mvc:interceptor>
    mvc:interceptors>

完整代码


<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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="com.powernode.controller"/>

    
    <mvc:annotation-driven/>

    
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/">property>
        <property name="suffix" value=".html">property>
    bean>

    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        <property name="defaultEncoding" value="UTF-8">property>
        
        <property name="maxUploadSizePerFile" value="1048576">property>
        
        <property name="maxUploadSize" value="10485760">property>
    bean>


    
    
    
    
    
    
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            
            <mvc:exclude-mapping path="/account/getLogin.do"/>
            
            <mvc:exclude-mapping path="/captcha/**"/>
            
            <mvc:exclude-mapping path="/forward/toLogin.do"/>
            
            <bean class="com.powernode.interceptors.AuthInterceptor">bean>
        mvc:interceptor>
    mvc:interceptors>


beans>

四 、spring-context.xml

1、表头


<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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

beans>

2、 组件扫描 ,扫描业务层的实现类

 
    <context:component-scan base-package="com.powernode.service.impl"/>

3、引入外部数据源

 
    <context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK"/>

4、数据库连接池

 
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}">property>
        <property name="url" value="${jdbc.url}">property>
        <property name="username" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
        <property name="initialSize" value="5">property>
        <property name="maxActive" value="50">property>
    bean>

5、配置日志

 
    
    <bean id="configuration" class="org.apache.ibatis.session.Configuration">
        <property name="logImpl" value="org.apache.ibatis.logging.log4j.Log4jImpl">property>
        <property name="cacheEnabled " value="true">property>
    bean>

6、配置sqlsession

    
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="mapperLocations" value="classpath*:mapper/*.xml">property>
        
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">bean>
            array>
        property>
        
        <property name="typeAliasesPackage" value="com.powernode.domain">property>
        
        <property name="configuration" ref="configuration">property>
    bean>

7、指定mapper 层接口存放的位置

 
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.powernode.mapper">property>
    bean>

8、事务

  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>
    
    <tx:annotation-driven transaction-manager="transactionManager">tx:annotation-driven>

完整代码


<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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    
    <context:component-scan base-package="com.powernode.service.impl"/>


    
    <context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK"/>


    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}">property>
        <property name="url" value="${jdbc.url}">property>
        <property name="username" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
        <property name="initialSize" value="5">property>
        <property name="maxActive" value="50">property>
    bean>

    
    <bean id="configuration" class="org.apache.ibatis.session.Configuration">
        <property name="logImpl" value="org.apache.ibatis.logging.log4j.Log4jImpl">property>

    bean>

    
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        

        
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">bean>
            array>
        property>
        
        <property name="typeAliasesPackage" value="com.powernode.domain">property>
        
        <property name="configuration" ref="configuration">property>
    bean>

    
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.powernode.mapper">property>
    bean>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>
    
    <tx:annotation-driven transaction-manager="transactionManager">tx:annotation-driven>

beans>

5、 web.xml

1、 默认访问首页

 
    <welcome-file-list>
        <welcome-file>/index.jspwelcome-file>
    welcome-file-list>

2、配置中文乱码


    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <servlet-name>springmvcservlet-name>
    filter-mapping>

3、配置druid监控sql、防火墙等

  
    <servlet>
        <servlet-name>statViewServletservlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
        
        <init-param>
            <param-name>loginUsernameparam-name>
            <param-value>adminparam-value>
        init-param>
        
        <init-param>
            <param-name>loginPasswordparam-name>
            <param-value>123456param-value>
        init-param>
        
        <init-param>
            <param-name>allowparam-name>
            <param-value>param-value>
        init-param>
        
        <init-param>
            <param-name>denyparam-name>
            <param-value>param-value>
        init-param>
    servlet>
    
    <servlet-mapping>
        <servlet-name>statViewServletservlet-name>
        <url-pattern>/druid/*url-pattern>
    servlet-mapping>

4、配置监听器,读写spring-context.xml文件

 
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring-context.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

5、配置DispatcherServlet 读取springmvc.xml文件

 
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

6、配置404


    <error-page>
        <location>/error/404.htmllocation>
    error-page>

7、完整代码


<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">

    
    <welcome-file-list>
        <welcome-file>/index.jspwelcome-file>
    welcome-file-list>

    
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <servlet-name>springmvcservlet-name>
    filter-mapping>

    
    <servlet>
        <servlet-name>statViewServletservlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
        
        <init-param>
            <param-name>loginUsernameparam-name>
            <param-value>adminparam-value>
        init-param>
        
        <init-param>
            <param-name>loginPasswordparam-name>
            <param-value>123456param-value>
        init-param>
        
        <init-param>
            <param-name>allowparam-name>
            <param-value>param-value>
        init-param>
        
        <init-param>
            <param-name>denyparam-name>
            <param-value>param-value>
        init-param>
    servlet>

    <servlet-mapping>
        <servlet-name>statViewServletservlet-name>
        <url-pattern>/druid/*url-pattern>
    servlet-mapping>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring-context.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>
    
    
    <error-page>
        <location>/error/404.htmllocation>
    error-page>

web-app>

项目结构 :
SSM 项目基本配置_第1张图片

总结

提示:这里对文章进行总结:
加油打工人 wwwwwwwwwwwin!!

你可能感兴趣的:(数据库,mysql,sql)