• SSM整合


    一、新建项目

    image.png

    image.png

    二、项目目录

    image.png
    新增的项目没有 Java和resources目录,需要手动新建
    image.png

    三、整合mybatis和spring

    1、导入依赖包

    
        
            org.springframework
            spring-aspects
            5.1.6.RELEASE
        
        
        
            org.springframework
            spring-context
            5.1.6.RELEASE
        
    
        
            mysql
            mysql-connector-java
            5.1.47
        
    
        
        
            com.alibaba
            druid
            1.1.16
        
    
        
        
            org.springframework
            spring-jdbc
            5.1.6.RELEASE
        
    
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
    
        
            org.mybatis
            mybatis
            3.4.6
        
    
        
            junit
            junit
            4.13
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    2、mysql 链接

    jdbc.properties

    #jdbc.properties
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
    jdbc.username=root
    jdbc.password=root
    jdbc.init=1
    jdbc.minIdle=10
    jdbc.maxActive=100
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3、配置log4j

    # Global logging configuration
    log4j.rootLogger=DEBUG, stdout
    # MyBatis logging configuration...
    log4j.logger.org.mybatis.example.BlogMapper=TRACE
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、配置spring xml文件

    applicationContext.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           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
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           ">
     beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    引入jdbc 外部配置文件

    <context:property-placeholder location="classpath:jdbc.properties" />
    
    • 1

    配置数据源

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    
        
        <property name="initialSize" value="${jdbc.init}"/>
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
    
        
        <property name="maxWait" value="60000"/>
    
        
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    
        
        <property name="minEvictableIdleTimeMillis" value="300000"/>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    注入sqlsessionfactory 工场

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="mapperLocations">
            <list>
                <value>classpath:mapper/*.xmlvalue>
            list>
        property>
        
        <property name="typeAliasesPackage" value="com.qf.entity">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    描述 mapper或者dao 接口路径

    
    <bean id="mapperScannerConfigurer9" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.qf.dao">property>
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    事务配置:

    <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>
    
    • 1
    • 2
    • 3

    定义事务通知:

    <tx:advice id="txManager" transaction-manager="tx">
        <tx:attributes>
            
            
            <tx:method name="*" rollback-for="Exception"/>
        tx:attributes>
    tx:advice>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    插入切点:

    切面
    <aop:config>
        <aop:pointcut expression="execution(* com.qf.service.*.*(..))" id="pc"/>
        
        <aop:advisor advice-ref="txManager" pointcut-ref="pc"/>
    aop:config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5、编写dao

    public interface UserDao {
        List<User> findAll();
    
        Integer insert(User user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编写bean

    public class User implements Serializable {
        private Integer id;
        private String name;
        private String password;
        private String sex;
        private Date birthday;
        private Date registTime;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    编写mapper

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.qf.dao.UserDao">
        <select id="findAll" resultType="com.qf.entity.User">
            select * from t_users
        select>
        <insert id="insert" parameterType="com.qf.entity.User">
            INSERT INTO t_users VALUES(#{id},#{name},#{password},#{sex},#{birthday},#{registTime})
        insert>
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    6、编写service

    public interface IUserService {
    
        List<User> findAll();
        Integer insert();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public class UserServiceImpl implements IUserService {
    
        private UserDao userDao;
    
        @Override
        public List<User> findAll() {
            return userDao.findAll();
        }
    
        @Override
        public Integer insert() {
            User user = new User();
            user.setName("疯子");
            user.setPassword("1111");
            user.setSex("nv");
            user.setBirthday(new Date());
            user.setRegistTime(new Date());
            Integer insert = userDao.insert(user);
            int i= 1/0;
            return insert;
        }
    
    
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    四、整合MVC

    1、导入依赖

    <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.1.6.RELEASEversion>
            dependency>
    
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.1.0version>
                <scope>providedscope>
            dependency>
    
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jstlartifactId>
                <version>1.2version>
            dependency>
    
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jsp-apiartifactId>
                <version>2.0version>
                <scope>providedscope>
            dependency>
    
    <dependency>
          <groupId>com.alibabagroupId>
          <artifactId>fastjsonartifactId>
          <version>1.2.54version>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    2、配置web.xml

    
    <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">
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    配置默认的servlet控制器

    <servlet>
        <servlet-name>mvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:mvc.xmlparam-value>
        init-param>
        
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>mvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    配置字符编码集

    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    配置spring beans

    <beans  xmlns="http://www.springframework.org/schema/beans"
              xmlns:context="http://www.springframework.org/schema/context"
              xmlns:mvc="http://www.springframework.org/schema/mvc"
              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
                         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">
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    启动注解和注解扫描包路径

    
    <context:component-scan base-package="com.qf.controller">context:component-scan>
    
    <mvc:annotation-driven>mvc:annotation-driven>
    
    • 1
    • 2
    • 3
    • 4

    配置文件后缀

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/">property>
        
        <property name="suffix" value=".jsp">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    配置静态访问路径

    
    <mvc:resources mapping="/cls/**" location="/html/"/>
    
     <mvc:annotation-driven>
            
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8" />
                bean>
            mvc:message-converters>
        mvc:annotation-driven>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    启动 application.xml

    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <context-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    五 整合分页插件

    1、导入依赖包

    <dependency>
    		<groupId>com.github.pagehelpergroupId>
    		<artifactId>pagehelperartifactId>
    		<version>5.1.10version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、添加配置文件到sqlsessionfactory

    <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countSql
                            autoRuntimeDialect=true
                        value>
                    property>
                bean>
            array>
        property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    六、整合文件上传

    1、导入依赖包

    <dependency>
        <groupId>commons-iogroupId>
        <artifactId>commons-ioartifactId>
        <version>2.4version>
    dependency>
    
    <dependency>
        <groupId>commons-fileuploadgroupId>
        <artifactId>commons-fileuploadartifactId>
        <version>1.3.3version>
        <exclusions>
            <exclusion>
                <groupId>javax.servletgroupId>
                <artifactId>servlet-apiartifactId>
            exclusion>
        exclusions>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2、添加配置

    <bean id="multipartResolver" 
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
        <property name="maxUploadSize" value="1048576">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    七 验证码

    1 导入依赖

    <dependency>
        <groupId>com.github.pengglegroupId>
        <artifactId>kaptchaartifactId>
        <version>2.3.2version>
        <exclusions>
            <exclusion>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
            exclusion>
        exclusions>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2 web.xml添加配置

    <servlet>
        <servlet-name>capservlet-name>
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServletservlet-class>
        <init-param>
          <param-name>kaptcha.borderparam-name>
          <param-value>noparam-value>
        init-param>
        <init-param>
          <param-name>kaptcha.textproducer.char.lengthparam-name>
          <param-value>4param-value>
        init-param>
        <init-param>
          <param-name>kaptcha.textproducer.char.stringparam-name>
          <param-value>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789param-value>
        init-param>
        <init-param>
          <param-name>kaptcha.background.clear.toparam-name>
          <param-value>211,229,237param-value>
        init-param>
        <init-param>
          
          <param-name>kaptcha.session.keyparam-name>
          <param-value>captchaparam-value>
        init-param>
      servlet>
      <servlet-mapping>
        <servlet-name>capservlet-name>
        <url-pattern>/captchaurl-pattern>
      servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    八 整合log4j2

    1、导入依赖

     
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-slf4j-implartifactId>
                <version>2.11.2version>
            dependency>
    
            
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-webartifactId>
                <version>2.11.2version>
            dependency>
    
            
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-jclartifactId>
                <version>2.11.2version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2、导入配置

    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
      listener>
    
      <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListenerlistener-class>
      listener>
      <context-param>
        <param-name>log4jConfigurationparam-name>
        
        <param-value>classpath:log4j2.xmlparam-value>
      context-param>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3、日志配置信息

    error–warn-info–dubeg

    
    <Configuration status="WARN" monitorInterval="30">
        <Properties>
            <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t-%L] %-5level
                %logger{36} - %msg%nproperty>
        Properties>
        <Appenders>
            
            <Console name="Console" target="SYSTEM_OUT">
                
                <ThresholdFilter level="trace" onMatch="ACCEPT"
                                 onMismatch="DENY" />
                <PatternLayout pattern="${PATTERN}" />
            Console>
        Appenders>
        
        <Loggers>
            
            <root level="debug">
                <appender-ref ref="Console" />
            root>
        Loggers>
    Configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    利用Paddle OCR进行文字识别
    深度学习--通过对Keras进行微调提升性能
    【GEE】9、在GEE中生成采样数据【随机采样】
    统计信号处理基础 习题解答6-6
    Spring源码篇(十二)事件机制
    AI 视频 | 文本生视频工具又迎来重大更新,Runway Gen-2 到底有多强?Gen-2 怎么用(保姆级教程)
    使用php解压缩ZipArchive类实现后台管理升级的解决方案
    程序、进程与线程
    2023年9月12日
    SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
  • 原文地址:https://blog.csdn.net/yc_Cabbage/article/details/126295614