SSM配置

SSM的配置总结

文章目录

  • SSM的配置:
    • 一、Mybatis的配置
      • 1、mybatis-config.xml 的约束
      • 2、db.properties
      • 3、pom.xml
        • 3.1 mysql驱动
        • 3.2 junit
        • 3.3 mybatis
        • 3.4 log4j
        • 3.5 lombok
        • 3.6 解决静态资源导出问题
      • 5、log4j.properties
      • 6、mybatis-config.xml
    • 二、Spring的配置
      • 1、依赖
        • 1.1 spring-webmvc
        • 1.2 aspectjweaver
        • 1.3 spring-jdbc
        • 1.4 mybatis-spring整合包
      • 2、spring-dao.xml
        • 2.1 约束
        • 2.2 关联数据库文件
        • 2.3 数据库连接池-c3p0
        • 2.4 配置SqlSessionFactory对象
        • 2.5 配置包扫描
        • 2.6 配置声明式事务
    • 三、SpringMVC的配置
      • 1、依赖
        • 1.1 junit
        • 1.2 spring-webmvc
        • 1.3 servlet-api
        • 1.4 jsp-api
        • 1.5 jstl
        • 1.6 jackson-databind
        • 1.7 fastjson
        • 1.8 文件上传的jar包,commons-fileupload
        • 1.9 servlet-api导入高版本的
      • 2、springmvc-servlet.xml
        • 2.1 约束
        • 2.2 添加处理器映射器
        • 2.3 添加处理器适配器
        • 2.4 添加视图解析器
        • 2.5 SpringMVC统一解决JSON乱码 配置
        • 2.6 拦截器
        • 2.7 文件上传配置
      • 3、注解
      • 4、web.xml
        • 4.1 注册servlet
        • 4.2 所有请求都会被springmvc拦截
        • 4.3 配置SpringMVC的乱码过滤器
        • 4.4 SpringMVC统一解决JSON乱码 配置
      • 5、controller
        • 5.1 文件上传的controller
        • 5.2 文件下载
    • 四、SSM总配置
      • 1、pom.xml
        • 1.1 依赖
        • 1.2 静态资源导出问题
      • 2、Spring配置整合文件,applicationContext.xml
      • 3、db.properties
      • 4、mybatis-config.xml
      • 5、spring-dao.xml
      • 6、spring-mvc.xml

SSM的配置:

一、Mybatis的配置

1、mybatis-config.xml 的约束



<configuration>
    
configuration>

2、db.properties

jdbc.driver:com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql://localhost:3306/mybatiskuang?serverTimezone=GMT&useSSL=true&useUnicode=true&characterEncoding=GBK
jdbc.username:root
jdbc.password:root

3、pom.xml

依赖

3.1 mysql驱动


<dependency>
   <groupId>mysqlgroupId>
   <artifactId>mysql-connector-javaartifactId>
   <version>5.1.6version>
dependency>

3.2 junit

<dependency>
   <groupId>junitgroupId>
   <artifactId>junitartifactId>
   <version>4.12version>
dependency>

3.3 mybatis

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.5.2version>
dependency>

3.4 log4j

<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.17version>
dependency>

3.5 lombok

<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
    <version>1.18.12version>
dependency>

3.6 解决静态资源导出问题


<build>
    <resources>
        <resource>
            <directory>src/main/resourcesdirectory>
            <includes>
                <include>**/*.propertiesinclude>
                <include>**/*.xmlinclude>
            includes>
            <filtering>truefiltering>
        resource>
        <resource>
            <directory>src/main/javadirectory>
            <includes>
                <include>**/*.propertiesinclude>
                <include>**/*.xmlinclude>
            includes>
            <filtering>falsefiltering>
        resource>
    resources>
build>

5、log4j.properties

#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file

#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
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=./log/kuang.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n

#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

6、mybatis-config.xml




<configuration>

二、Spring的配置

1、依赖

1.1 spring-webmvc

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>5.2.4.RELEASEversion>
dependency>

1.2 aspectjweaver

<dependency>
    <groupId>org.aspectjgroupId>
    <artifactId>aspectjweaverartifactId>
    <version>1.9.4version>
dependency>

1.3 spring-jdbc


<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>5.1.9.RELEASEversion>
dependency>

1.4 mybatis-spring整合包


<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatis-springartifactId>
    <version>2.0.2version>
dependency>

2、spring-dao.xml

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

2.2 关联数据库文件


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

 
   
   
   
   
  • 1
  • 2

2.3 数据库连接池-c3p0


<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>

2.4 配置SqlSessionFactory对象


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    
    <property name="dataSource" ref="dataSource"/>
    
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
bean>

2.5 配置包扫描


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    
    <property name="basePackage" value="com.kuang.dao"/>
bean>

2.6 配置声明式事务



<bean id="transactionManagement" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
bean>

三、SpringMVC的配置

1、依赖

1.1 junit

1.2 spring-webmvc

1.3 servlet-api

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>servlet-apiartifactId>
    <version>2.5version>
dependency>

 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5

1.4 jsp-api

<dependency>
    <groupId>javax.servlet.jspgroupId>
    <artifactId>jsp-apiartifactId>
    <version>2.2version>
dependency>

1.5 jstl

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>jstlartifactId>
    <version>1.2version>
dependency>

1.6 jackson-databind

<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>2.10.2version>
dependency>

1.7 fastjson

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>1.2.60version>
dependency>

1.8 文件上传的jar包,commons-fileupload

<dependency>
    <groupId>commons-fileuploadgroupId>
    <artifactId>commons-fileuploadartifactId>
    <version>1.3.3version>
dependency>

1.9 servlet-api导入高版本的


<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>4.0.1version>
dependency>

2、springmvc-servlet.xml

2.1 约束


<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">
beans>

2.2 添加处理器映射器


<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

2.3 添加处理器适配器


<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

2.4 添加视图解析器



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

2.5 SpringMVC统一解决JSON乱码 配置


<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8"/>
        bean>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                    <property name="failOnEmptyBeans" value="false"/>
                bean>
            property>
        bean>
    mvc:message-converters>
mvc:annotation-driven>

2.6 拦截器


<mvc:interceptors>
    <mvc:interceptor>
        
        
        <mvc:mapping path="/**"/>
        
        
        <bean class="com.kuang.config.MyInterceptor"/>
    mvc:interceptor>

MyInterceptor.java

package com.kuang.config;

LoginInterceptor.java

package com.kuang.config;

2.7 文件上传配置


<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    
    <property name="defaultEncoding" value="utf-8"/>
    
    <property name="maxUploadSize" value="10485760"/>
    <property name="maxInMemorySize" value="40960"/>
bean>

3、注解

springmvc-servlet.xml

3.1 配置自动扫描包


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

3.2 让SpringMVC 不处理静态资源


<mvc:default-servlet-handler/>

3.3 支持注解驱动


<mvc:annotation-driven/>

 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.4 视图解析器


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

4、web.xml

4.1 注册servlet

4.2 所有请求都会被springmvc拦截


<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">
    
    <servlet>
        <servlet-name>SpringMVCservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc-servlet.xmlparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>

4.3 配置SpringMVC的乱码过滤器


<filter>
    <filter-name>encodingfilter-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>encodingfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>

4.4 SpringMVC统一解决JSON乱码 配置


<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8"/>
        bean>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                    <property name="failOnEmptyBeans" value="false"/>
                bean>
            property>
        bean>
    mvc:message-converters>
mvc:annotation-driven>

5、controller

5.1 文件上传的controller

第一种方式:

@RestController
public class FileController {
     

第二种方式:采用file.Transto 来保存上传的文件

/*
     * 采用file.Transto 来保存上传的文件
     */
    @RequestMapping("/upload2")
    public String  fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
     

5.2 文件下载


    //【下载文件】
    @RequestMapping(value="/download")
    public String downloads(HttpServletResponse response , HttpServletRequest request) throws Exception{
     
        System.out.println(" downloads()");
        //要下载的图片地址
        String  path = request.getServletContext().getRealPath("/upload");
        String  fileName = "3.jpg";

四、SSM总配置

1、pom.xml

1.1 依赖


<dependencies>
    
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>

    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.47version>
    dependency>
    
    <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5.2version>
    dependency>

    
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>servlet-apiartifactId>
        <version>2.5version>
    dependency>
    <dependency>
        <groupId>javax.servlet.jspgroupId>
        <artifactId>jsp-apiartifactId>
        <version>2.2version>
    dependency>
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
    dependency>

    
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.5.2version>
    dependency>

    
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>2.0.2version>
    dependency>

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>5.2.4.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>5.1.9.RELEASEversion>
    dependency>
      
     <dependency>
         <groupId>org.projectlombokgroupId>
         <artifactId>lombokartifactId>
         <version>1.18.12version>
      dependency>
dependencies>

1.2 静态资源导出问题


<build>
    <resources>
        <resource>
            <directory>src/main/javadirectory>
            <includes>
                <include>**/*.propertiesinclude>
                <include>**/*.xmlinclude>
            includes>
            <filtering>falsefiltering>
        resource>
        <resource>
            <directory>src/main/resourcesdirectory>
            <includes>
                <include>**/*.propertiesinclude>
                <include>**/*.xmlinclude>
            includes>
            <filtering>falsefiltering>
        resource>
    resources>
build>

2、Spring配置整合文件,applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

3、db.properties

jdbc.driver:com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql://localhost:3306/mybatiskuang?serverTimezone=GMT&useSSL=true&useUnicode=true&characterEncoding=GBK
jdbc.username:root
jdbc.password:root

4、mybatis-config.xml



<configuration>
    
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    settings>
    

5、spring-dao.xml


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

6、spring-mvc.xml


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

7、spring-service.xml


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

推荐学习;
B站 :狂神 https://www.bilibili.com/video/BV1RE41127rv

若有错误的地方,请指正

原作者链接: [link](http://blog.csdn.net/lisa2017_/article/details/106724638).

你可能感兴趣的:(SSM,mybatis,数据库,java,spring,mysql)