• SSM整合demo及个人思考


    项目整体架构说明

    在这里插入图片描述

    1. 创建Maven项目

    网上创建Maven的教程有很多,在这里就不再累赘了,有以下几个注意点:

    1. pom.xml中的打包方式一定要为war包,不配置默认是jar包,在后面配置tomcat的时候会找不到Artifacts情况;
    2. 创建新的Maven项目是没有web.xml的,需要进入Project Structure进行配置,如下图所示;
      在这里插入图片描述

    pom.xml依赖如下所示,如果有需要可以直接copy使用:

    注意: 如果在项目启动过程中出现:validateConnection false等错误,通常原因是因为druid数据源的版本与MySql 8.0版本不匹配导致的,可以参考文章:MySQL 8.0 驱动与阿里druid版本兼容操作

    
    <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>org.examplegroupId>
        <artifactId>day25_ssmartifactId>
        <version>1.0-SNAPSHOTversion>
        <packaging>warpackaging>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <spring.version>5.3.1spring.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-beansartifactId>
                <version>${spring.version}version>
            dependency>
    
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${spring.version}version>
            dependency>
            <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-aspectsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring.version}version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.5.7version>
            dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>2.0.6version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.1.10version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.11version>
            dependency>
            
            <dependency>
                <groupId>log4jgroupId>
                <artifactId>log4jartifactId>
                <version>1.2.17version>
            dependency>
            <dependency>
                <groupId>com.github.pagehelpergroupId>
                <artifactId>pagehelperartifactId>
                <version>5.2.0version>
            dependency>
            
            <dependency>
                <groupId>ch.qos.logbackgroupId>
                <artifactId>logback-classicartifactId>
                <version>1.2.3version>
            dependency>
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.1.0version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.12.1version>
            dependency>
            <dependency>
                <groupId>commons-fileuploadgroupId>
                <artifactId>commons-fileuploadartifactId>
                <version>1.3.1version>
            dependency>
            
            <dependency>
                <groupId>org.thymeleafgroupId>
                <artifactId>thymeleaf-spring5artifactId>
                <version>3.0.12.RELEASEversion>
            dependency>
        dependencies>
    
    project>
    
    • 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
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129

    2. 配置web.xml

    web.xml配置的文件内容

    1. 配置Spring的编码过滤器CharacterEncodingFilter;
    2. 配置处理请求方式PUTDELETE的过滤器HiddenHttpMethodFilter;
    3. 配置SpringMVC的前端控制器DispatcherServlet;
    4. 设置Spring的配置文件的位置和名称;
    5. 配置Spring监听器ContextLoaderListener;

      1、2、3点都很容易理解,在SpringMVC配置中都需要,那么整合的时候也必须可少。那下面就分析一下为什么还需要4、5点。一个web项目启动起来至少有两个IOC容器:一个是SpringMVC管理的IOC容器,一个是Spring管理的IOC容器。这两个容器有什么关系吗?

      答案是肯定的,根据MVC三层架构知,controller层依赖service层,service层依赖mapper层;而SpringMVC管理的IOC容器只管理controller层组件bean,Spring管理的组件IOC容器管理除controler层的所有组件bean。

      所以这两个容器之间肯定是存在关系的,Spring管理的IOC容器必须要早于SpringMVC管理的IOC容器生成。并结合官方文档可知:SpringMVC管理的IOC容器是一个父容器,Spring管理的IOC容器是一个子容器,所以Spring管理的IOC容器必须早于SpringMVC管理的IOC容器生成,所以就设置了4、5条内容。

    Spring提供了监听器ContextLoaderListener,实现ServletContextListener接口,可监听ServletContext的状态,在web服务器的启动,读取Spring的配置文件,创建Spring的IOC容器。web应用中必须在web.xml中配置

    
    <context-param>
    		<param-name>contextConfigLocationparam-name>
    		<param-value>classpath:spring.xmlparam-value>
    context-param>
    
    <listener>
    	
    	<listenerclass>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    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">
    
        
        <filter>
            <filter-name>CharacterEncodingFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
            
            <init-param>
                <param-name>forceEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    
        
        <filter>
            <filter-name>HiddenHttpMethodFilterfilter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
        filter>
        <filter-mapping>
            <filter-name>HiddenHttpMethodFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    
        
        <servlet>
            <servlet-name>DispatcherServletservlet-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>DispatcherServletservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
        
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring.xmlparam-value>
        context-param>
    
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
    
    
    web-app>
    
    • 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
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    4. 配置springmvc.xml

    springmvc.xml可以配置的文件内容:

    1. 扫描controller层的包;
    2. 配置Thymeleaf视图解析器;
    3. 配置文件上传解析器multipartResolver;(multipartResolver是一个接口,实现类为CommonsMultipartResolver)
    4. 配置访问首页的试图控制;(需要再webapp/WEB-INF/templates目录下创建 一个index.html页面)
    5. 配置默认的servlet处理静态资源;
    6. 开启MVC的注解驱动;
    7. 拦截器和异常解析器也在该配置文件里面配置,这里就不展示了;

    springmvc.xml全部内容,可以直接copy使用:

    
    <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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        
        <context:component-scan base-package="qc.lsm.controller">context:component-scan>
    
        
        <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
            <property name="order" value="1"/>
            <property name="characterEncoding" value="UTF-8"/>
            <property name="templateEngine">
                <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                    <property name="templateResolver">
                        <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                            
                            
                            <property name="prefix" value="/WEB-INF/templates/"/>
                            
                            <property name="suffix" value=".html"/>
                            <property name="templateMode" value="HTML5"/>
                            <property name="characterEncoding" value="UTF-8" />
                        bean>
                    property>
                bean>
            property>
        bean>
    
        
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
    
        
        <mvc:view-controller path="/" view-name="index">mvc:view-controller>
    
        
        <mvc:annotation-driven>mvc:annotation-driven>
    
        
        <mvc:default-servlet-handler>mvc:default-servlet-handler>
    
    beans>
    
    • 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

    5. 配置spring.xml

    Spring配置文件中的内容

    1. 扫描除了controller层的所有组件;
    2. 配置Druid数据源并引入外部文件jdbc.properties;
    3. 配置JdbcTemplate;
    4. 配置事务管理器组件DataSourceTransactionManager;
    5. 开启事务的注解驱动;
    6. 开启aop自动代理;
    7. 配置用于创建SqlSessionFactory的工程bean,这样就可以简化mybatis配置文件;
    8. 配置mapper接口的扫描配置:由mybatis-spring提供,可以将指定包下所有的mapper接口创建动态代理并将这些代理动态作为IOC容器的bean管理;

    在SSM整合的时候添加了7-8的原因在于简化mybatis, 之前学习mybaits的时候很麻烦,要先加载mybaits-config.xml配置文件,然后创建SqlSessionFactory,获取Session以及对应的mapper等。

    在SSM整合的时候进行了简化,第7项:可以直接在spring.xml配置文件里面进行创建SqlSessionFactory的工程bean,该组件里面的属性包括加载mybatis配置文件的路径,设置数据源,设置类型别名所对应的包以及映射文件的路径等,大大简化的mybatis-config.xml的配置。在spring.xml中进行配置后就不需要再mybatis-config.xml中进行配置了。

    
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            
            <property name="configLocation" value="classpath:mybatis-config.xml">property>
            
            <property name="dataSource" ref="dataSource">property>
            
            <property name="typeAliasesPackage" value="qc.lsm.pojo">property>
            
    
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    第8项个人感觉非常牛皮,配置之后可以将指定包下所有的mapper接口创建动态代理并将这些代理动态作为IOC容器的bean管理。

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="qc.lsm.mapper">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4

    spring.xml全部内容,可以直接copy使用:

    
    <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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
        
        <context:component-scan base-package="qc.lsm">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        context:component-scan>
    
        
        <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
    
        
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}">property>
            <property name="url" value="${jdbc.url}">property>
            <property name="username" value="${jdbc.username}">property>
            <property name="password" value="${jdbc.password}">property>
        bean>
    
        
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource">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>
    
        
        <aop:aspectj-autoproxy>aop:aspectj-autoproxy>
    
        
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            
            <property name="configLocation" value="classpath:mybatis-config.xml">property>
            
            <property name="dataSource" ref="dataSource">property>
            
            <property name="typeAliasesPackage" value="qc.lsm.pojo">property>
            
    
        bean>
    
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="qc.lsm.mapper">property>
        bean>
    
    
    beans>
    
    • 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
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    6. 配置mybatis-config.xml以及创建mapper接口和mapper配置文件

    在学习mybatis的时候,mybatis-config.xml配置文件内容如下:

    
    DOCTYPE configuration
            PUBLIC "-//MyBatis.org//DTD Config 3.0//EN"
            "http://MyBatis.org/dtd/MyBatis-3-config.dtd">
    <configuration>
        
        <properties resource="jdbc.properties">properties>
        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <setting name="lazyLoadingEnabled" value="true"/>
        settings>
    
        <plugins>
            
            <plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
        plugins>
    
        <typeAliases>
            
            
            
            
            <package name="com.atguigu.bean"/>
        typeAliases>
        
        <environments default="mysql_test">
            
            <environment id="mysql_test">
                
                <transactionManager type="JDBC"/>
                
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/> 
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                dataSource>
            environment>
        environments>
        
        <mappers>
            
            
            <package name="com.atguigu.mapper"/>
        mappers>
    configuration>
    
    
    • 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
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    但是为了简化mybaits,咱们在spring.xml配置文件里面设置了SqlSessionFactory的工厂bean,并在里面配置了下面的内容:

    1. 设置mybatis配置文件的路径;
    2. 设立数据源;
    3. 设置类型别名所对应的包;
    4. 设置映射文件路径,若映射文件所在路径和mapper接口所在路径一致,则不需要设置;

    所以mybatis-config.xml中的内容如下所示:

    
    DOCTYPE configuration
            PUBLIC "-//MyBatis.org//DTD Config 3.0//EN"
            "http://MyBatis.org/dtd/MyBatis-3-config.dtd">
    <configuration>
    
        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <setting name="lazyLoadingEnabled" value="true"/>
        settings>
    
        <plugins>
            
            <plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
        plugins>
    
    
    configuration>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    7. 配置log4j.xml

    log4j 8种日志级别:

    1. All: 最低等级的,用于打开所有日志记录.
    2. Trace: 是追踪,就是程序推进以下,你就可以写个trace输出,所以trace应该会特别多,我们可以设置最低日志级别不让他输出.
    3. Debug: 指出细粒度信息事件对调试应用程序是非常有帮助的.
    4. Info: 消息在粗粒度级别上突出强调应用程序的运行过程.
    5. Warn: 输出警告及warn以下级别的日志.
    6. Error: 输出错误信息日志.
    7. Fatal: 输出每个严重的错误事件将会导致应用程序的退出的日志.
    8. OFF: 最高等级的,用于关闭所有日志记录.

    日志级别等级按照从低到高为All < Trace < Debug < Info < Warn < Error < Fatal < OFF

    Log4j建议只使用四个级别,优先级从高到低分别是ERROR、WARN、INFO、DEBUG。

    
    DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
        <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
            <param name="Encoding" value="UTF-8" />
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}%m (%F:%L) \n" />
            layout>
        appender>
        <logger name="java.sql">
            <level value="debug" />
        logger>
        <logger name="org.apache.ibatis">
            <level value="info" />
        logger>
        <root>
            <level value="debug" />
            <appender-ref ref="STDOUT" />
        root>
    log4j:configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    8. 后端CURD测试

    8.1 在数据库中插入数据

    在这里插入图片描述

    8.2 pojo中的实体类Employee

    public class Employee {
        private Integer empId;
        private String empName;
        private Integer age;
        private String sex;
        private String email;
    
       // 无参构造器,有参构造器,set/get方法,toString方法...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    8.3 mapper层的EmployMapper接口

    package qc.lsm.mapper;
    import qc.lsm.pojo.Employee;
    import java.util.List;
    
    public interface EmployeeMapper {
    
        /**
         * 根据id查询单个员工的信息
         * @param id
         * @return
         */
        Employee selectEmployeeById(Integer id);
    
        /**
         * 查询所有员工的信息
         * @return
         */
        List<Employee> selectAllEmployee();
    
        /**
         * 添加员工信息
         * @param employee
         * @return
         */
        int addEmployee(Employee employee);
    
        /**
         * 根据员工id删除员工信息
         * @param empId
         * @return
         */
        int deleteEmployee(Integer empId);
    
        /**
         * 根据员工id修改员工信息
         * @param employee
         * @return
         */
        int updateEmployee(Employee employee);
    }
    
    
    • 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

    8.4 mapper层的EmployeeMapper.xml配置文件(resources目录下)

    注意:由于表t_emp的字段名与类Employee中的属性名不一致,所以在查询的时候使用了resultMap标签;

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="qc.lsm.mapper.EmployeeMapper">
        
    
        <resultMap id="employeeResultMap" type="Employee">
            <id property="empId" column="emp_id">id>
            <result property="empName" column="emp_name">result>
            <result property="age" column="age">result>
            <result property="sex" column="sex">result>
            <result property="email" column="email">result>
        resultMap>
    
        
        <select id="selectEmployeeById" resultMap="employeeResultMap">
            select * from t_emp where emp_id = #{id};
        select>
    
        
        <select id="selectAllEmployee" resultMap="employeeResultMap">
            select * from t_emp;
        select>
    
        
        
        <insert id="addEmployee" parameterType="qc.lsm.pojo.Employee">
            insert into t_emp values(null, #{empName}, #{age}, #{sex},#{email})
        insert>
    
        
        <delete id="deleteEmployee" parameterType="Integer">
            delete from t_emp where emp_id = #{id}
        delete>
    
        
        <update id="updateEmployee" parameterType="Employee">
            update t_emp set emp_name = #{empName}, age = #{age}, sex = #{sex}, email = #{email} where emp_id = #{empId};
        update>
    
    mapper>
    
    
    • 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

    8.5 service层的EmployeeService接口 和 service.impl层的EmployeeServiceImpl类

    EmployeeService接口

    public interface EmployeeService {
        /**
         * 根据id查询单个员工的信息
         * @param id
         * @return
         */
        Employee selectEmployeeById(Integer id);
    
        /**
         * 查询所有员工的信息
         * @return
         */
        List<Employee> selectAllEmployee();
    
        /**
         * 添加员工信息
         * @param employee
         * @return
         */
        int addEmployee(Employee employee);
    
        /**
         * 根据员工id删除员工信息
         * @param empId
         * @return
         */
        int deleteEmployee(Integer empId);
    
        /**
         * 根据员工id修改员工信息
         * @param employee
         * @return
         */
        int updateEmployee( Employee employee);
    }
    
    
    • 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

    EmployeeService接口的实现类EmployeeServiceImpl

    package qc.lsm.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import qc.lsm.mapper.EmployeeMapper;
    import qc.lsm.pojo.Employee;
    import qc.lsm.service.EmployeeService;
    import java.util.List;
    
    @Service
    public class EmployeeServiceImpl implements EmployeeService {
    
        @Autowired
        private EmployeeMapper employeeMapper;
    
        @Override
        public Employee selectEmployeeById(Integer id) {
            System.out.println("employeeMapper: " + employeeMapper);
            return employeeMapper.selectEmployeeById(id);
        }
    
        @Override
        public List<Employee> selectAllEmployee() {
            System.out.println("employeeMapper: " + employeeMapper);
            return employeeMapper.selectAllEmployee();
        }
    
        @Override
        public int addEmployee(Employee employee) {
            return employeeMapper.addEmployee(employee);
        }
    
        @Override
        public int deleteEmployee(Integer empId) {
            return employeeMapper.deleteEmployee(empId);
        }
    
        @Override
        public int updateEmployee(Employee employee) {
            return employeeMapper.updateEmployee(employee);
        }
    }
    
    
    • 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

    8.6 统一返回结果Result以及响应码Code

    响应码Code类

    package qc.lsm.utils;
    
    public class Code {
        public static final Integer ADD_OK = 10001;
        public static final Integer ADD_ERROR = 10000;
    
        public static final Integer SELETE_OK = 20001;
        public static final Integer SELETE_ERROR = 20000;
    
        public static final Integer UPDATE_OK = 30001;
        public static final Integer UPDATE_ERROR = 30000;
    
        public static final Integer DELETE_OK = 40001;
        public static final Integer DELETE_ERROR = 40000;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    统一返回结果类Result

    package qc.lsm.controller;
    
    /**
     * 定义统一返回格式类
     */
    public class Result {
        // 描述统一格式中的数据
        private Object data;
        // 描述统计格式中的编码,用于区分操作,可以简化配置0/1表示成功失败
        private Integer code;
        // 描述统一格式中的消息,可选属性
        private String msg;
    
        public Result() {
        }
    
        public Result(Object data, Integer code) {
            this.data = data;
            this.code = code;
        }
    
        public Result(Object data, Integer code, String msg) {
            this.data = data;
            this.code = code;
            this.msg = msg;
        }
    
    
        // set/get方法
    
        @Override
        public String toString() {
            return "Result{" +
                    "data=" + data +
                    ", code=" + code +
                    ", msg='" + msg + '\'' +
                    '}';
        }
    }
    
    
    • 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

    8.7 controller层的EmployeeController

    package qc.lsm.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    import qc.lsm.pojo.Employee;
    import qc.lsm.service.EmployeeService;
    import qc.lsm.utils.Code;
    
    import java.util.List;
    
    /**
     * @author Mr.Lu
     * @version 1.0
     * @date 2023-09-08 20:13
     */
    
    @RestController
    @RequestMapping("/employee")
    public class EmployeeController {
        @Autowired
        private EmployeeService employeeService;
    
        @RequestMapping("/selectEmployeeById/{id}")
        public Result selectEmployeeById(@PathVariable("id") Integer id){
            Employee employee = employeeService.selectEmployeeById(id);
            if(employee == null){
                return new Result(null, Code.SELETE_ERROR, "查询失败");
            }
            return new Result(employee,Code.SELETE_OK, "查询成功");
        }
    
        @RequestMapping("/selectAll")
        public Result selectAllEmployee(){
            List<Employee> employees = employeeService.selectAllEmployee();
            if(employees == null){
                return new Result(null, Code.SELETE_ERROR, "查询失败");
            }
            return new Result(employees, Code.SELETE_OK, "查询成功");
        }
    
        @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
        public Result addEmployee(@RequestBody Employee employee){
            System.out.println("employee: " + employee);
            int count = employeeService.addEmployee(employee);
            if(count > 0) {
                List<Employee> employees = employeeService.selectAllEmployee();
                return new Result(employees, Code.ADD_OK, "添加成功");
            }
            return new Result(null, Code.ADD_ERROR, "添加失败");
        }
    
        @RequestMapping("/deleteEmployeeById/{id}")
        public Result deleteEmployeeById(@PathVariable("id") Integer id){
            int count = employeeService.deleteEmployee(id);
            if(count > 0){
                List<Employee> employees = employeeService.selectAllEmployee();
                return new Result(employees, Code.DELETE_OK, "删除成功");
            }
            return new Result(null, Code.DELETE_ERROR, "删除失败");
        }
    
    
        @RequestMapping("/updateEmployee")
        public Result updateEmployee(@RequestBody Employee employee){
            int count = employeeService.updateEmployee(employee);
            if(count > 0){
                return new Result(employeeService.selectEmployeeById(employee.getEmpId()), Code.UPDATE_OK, "更新成功");        }
    
            return new Result(null, Code.UPDATE_ERROR, "更新失败");
        }
    }
    
    • 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
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    8.8 postman测试

    测试1:根据id查询员工信息http://localhost/ssm/employee/selectEmployeeById/2

    @RequestMapping("/selectEmployeeById/{id}")
    public Result selectEmployeeById(@PathVariable("id") Integer id){
        Employee employee = employeeService.selectEmployeeById(id);
        if(employee == null){
            return new Result(null, Code.SELETE_ERROR, "查询失败");
        }
        return new Result(employee,Code.SELETE_OK, "查询成功");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    测试2:查询所有员工信息http://localhost/ssm/employee/selectAll

       @RequestMapping("/selectAll")
        public Result selectAllEmployee(){
            List<Employee> employees = employeeService.selectAllEmployee();
            if(employees == null){
                return new Result(null, Code.SELETE_ERROR, "查询失败");
            }
            return new Result(employees, Code.SELETE_OK, "查询成功");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    测试3:添加员工http://localhost/ssm/employee/addEmployee

    @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public Result addEmployee(@RequestBody Employee employee){
        System.out.println("employee: " + employee);
        int count = employeeService.addEmployee(employee);
        if(count > 0) {
            List<Employee> employees = employeeService.selectAllEmployee();
            return new Result(employees, Code.ADD_OK, "添加成功");
        }
        return new Result(null, Code.ADD_ERROR, "添加失败");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    测试4:根据id删除员工信息http://localhost/ssm/employee/deleteEmployeeById/2

    @RequestMapping("/deleteEmployeeById/{id}")
    public Result deleteEmployeeById(@PathVariable("id") Integer id){
        int count = employeeService.deleteEmployee(id);
        if(count > 0){
            List<Employee> employees = employeeService.selectAllEmployee();
            return new Result(employees, Code.DELETE_OK, "删除成功");
        }
        return new Result(null, Code.DELETE_ERROR, "删除失败");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    测试5:更新员工信息http://localhost/ssm/employee/updateEmployee

    @RequestMapping("/updateEmployee")
    public Result updateEmployee(@RequestBody Employee employee){
        int count = employeeService.updateEmployee(employee);
        if(count > 0){
            return new Result(employeeService.selectEmployeeById(employee.getEmpId()), Code.UPDATE_OK, "更新成功");        }
    
        return new Result(null, Code.UPDATE_ERROR, "更新失败");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

  • 相关阅读:
    Arthas(2):使用Web Console
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java情报综合管理系统36zgo
    Golang学习:基础知识篇(二)—— 数组及切片
    飞书API(5):查看多维表 28 种数据类型的数据结构
    金融行业多活架构设计及容灾发展趋势
    27.4 Java集合之Map学习
    HashMap源码分析(一)
    史上最难618,TCL夺得电视行业京东和天猫份额双第一
    sqlite 合并两个数据库中的特定表
    GPIO的输入模式
  • 原文地址:https://blog.csdn.net/qq_43751200/article/details/132857510