• SSM整合


    SSM项目

    • SpringMVC:表现层框架,处理浏览器发送的请求,找到具体的数据,将具体数据响应到服务器,
    • Spring:整合形框架,通过Ioc管理对象,比如Mybatis中操作数据库的SqlSession对象,MyBatis操作数据库时需要的事务功能,也可以使用Spring中的AOP的重要应用,来声明事务实现,
    • MyBatis:持久层框架,连接数据库,操作数据库,访问数据库

    创建项目

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述

    pom.xml

    
    
    <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.singroupId>
      <artifactId>SSMDemoartifactId>
      <version>1.0-SNAPSHOTversion>
      
      <packaging>warpackaging>
    
      <name>SSMDemo Maven Webappname>
      
      <url>http://www.example.comurl>
    
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <spring.version>5.3.22spring.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.10version>
        dependency>
        
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatis-springartifactId>
          <version>2.0.7version>
        dependency>
        
        
        <dependency>
          <groupId>com.alibabagroupId>
          <artifactId>druidartifactId>
          <version>1.2.8version>
        dependency>
        
        
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          
          <version>5.1.47version>
        dependency>
    
        
        <dependency>
          <groupId>log4jgroupId>
          <artifactId>log4jartifactId>
          <version>1.2.17version>
        dependency>
        
        <dependency>
          <groupId>ch.qos.logbackgroupId>
          <artifactId>logback-classicartifactId>
          <version>1.2.11version>
        dependency>
    
        
        
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>4.0.1version>
        dependency>
    
        
        
        <dependency>
          <groupId>org.thymeleafgroupId>
          <artifactId>thymeleaf-spring5artifactId>
          <version>3.0.15.RELEASEversion>
        dependency>
        
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
      dependencies>
    
      <build>
        <finalName>SSMDemofinalName>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-clean-pluginartifactId>
              <version>3.1.0version>
            plugin>
            
            <plugin>
              <artifactId>maven-resources-pluginartifactId>
              <version>3.0.2version>
            plugin>
            <plugin>
              <artifactId>maven-compiler-pluginartifactId>
              <version>3.8.0version>
            plugin>
            <plugin>
              <artifactId>maven-surefire-pluginartifactId>
              <version>2.22.1version>
            plugin>
            <plugin>
              <artifactId>maven-war-pluginartifactId>
              <version>3.2.2version>
            plugin>
            <plugin>
              <artifactId>maven-install-pluginartifactId>
              <version>2.5.2version>
            plugin>
            <plugin>
              <artifactId>maven-deploy-pluginartifactId>
              <version>2.8.2version>
            plugin>
          plugins>
        pluginManagement>
      build>
    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
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228

    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">
        
        
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:applicationContext.xmlparam-value>
        context-param>
    
        
        
        <listener>
            
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
    
        
        
        
        <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>
    
        
        
        <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>forceResponseEncodingparam-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>
    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
    • 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
    • 130
    • 131

    applicationContext.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"
           xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
           xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
           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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        
    
        
        
        <context:component-scan base-package="com">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        context:component-scan>
    
        
        
        <context:property-placeholder location="classpath:jdbc.properties"/>
        
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        bean>
    
        
        <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:mybatis-config.xml">property>
            <property name="dataSource" ref="dataSource">property>
            <property name="mapperLocations" value="classpath:mapper/*.xml">property>
        bean>
    
        
        
        <mybatis-spring:scan base-package="com.sin.mapper"/>
    
        
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource">property>
        bean>
    
        
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            
            <tx:attributes>
                
                <tx:method name="*"/>
                
                <tx:method name="get*" read-only="true"/>
            tx:attributes>
        tx:advice>
    
        
        
        <aop:config>
            
            <aop:pointcut id="pt" expression="execution(* com.sin.service.*.*(..))"/>
            
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt">aop:advisor>
        aop:config>
    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
    • 63
    • 64
    • 65
    • 66

    db.properties

    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://localhost:3306/demo
    jdbc.username = root
    jdbc.password = 123456
    
    • 1
    • 2
    • 3
    • 4

    SpringMVC.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"
           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" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.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>
        
        <mvc:default-servlet-handler/>
    
        
        <mvc:annotation-driven/>
    
    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

    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="cacheEnabled" value="true"/>
        settings>
        
        <typeAliases>
            <package name="com.sin"/>
        typeAliases>
        <databaseIdProvider type="DB_VENDOR">
            
            <property name="MySQL" value="mysql"/>
            <property name="Oracle" value="oracle"/>
            <property name="SQL Server" value="sqlserver"/>
        databaseIdProvider>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    log4j.xml

    
    
    <configuration status="TRACE">
        
        <appenders>
            
            <console name="Console" target="SYSTEM_OUT">
                
                <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            console>
        appenders>
        
        
        <logger name="java.sql">
            <level value="debug" />
        logger>
        <logger name="org.apache.ibatis">
            <level value="info" />
        logger>
        <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
    • 24
    • 25

    SQL

    use demo;
    
    -- 创建数据表
    -- primary key : 逐渐id
    -- auto_increment : 字段自增
    -- comment : 字段的注释
    -- character set utf8 : 字段的编码格式
    -- default null : 字段默认为null
    -- charset = utf8 : 将表的编码格式设置为utf8
    create table users(
        id int primary key auto_increment comment '主键id',
        name varchar(20) character set utf8 default null comment '姓名',
        email varchar(20) default null comment '邮箱'
    )charset = utf8;
    
    insert into users(name,email) values ('张三','123456@126.com');
    
    select id,name,email from users;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Users.java

    package com.sin.pojo;
    
    public class Users {
        private int id;
        private String  name;
        private String email;
    
        public int getId(){
            return id;
        }
        public void setId(int id){
            this.id=id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    }
    
    
    • 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

    UsersMapper.java

    package com.sin.mapper;
    
    import com.sin.pojo.Users;
    import org.apache.ibatis.annotations.Mapper;
    
    
    import java.util.List;
    
    @Mapper 
    public interface UsersMapper {
    
        List<Users> getAllUsers();
    
        Users getUserById(int id);
    
        void addUser(Users user);
    
        void updateUser(Users user);
    
        void deleteUser(int id);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    UsersMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.sin.mapper.UsersMapper">
        <select id="getAllUsers" resultType="com.sin.pojo.Users">
            SELECT * FROM users;
        select>
    
        <select id="getUserById" parameterType="int" resultType="com.sin.pojo.Users">
            SELECT * FROM users WHERE id = #{id};
        select>
    
        <insert id="addUser" parameterType="com.sin.pojo.Users">
            INSERT INTO users (name, email) VALUES (#{name}, #{email});
        insert>
    
        <update id="updateUser" parameterType="com.sin.pojo.Users">
            UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id};
        update>
    
        <delete id="deleteUser" parameterType="int">
            DELETE FROM users WHERE id = #{id};
        delete>
    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

    UsersService.java

    package com.sin.service;
    
    import com.sin.pojo.Users;
    
    import java.util.List;
    
    public interface UsersService {
        List<Users> getAllUsers();
        Users getUserById(int id);
        void addUser(Users user);
        void updateUser(Users user);
        void deleteUser(int id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    UsersServiceImpl.java

    package com.sin.service.impl;
    
    import com.sin.mapper.UsersMapper;
    import com.sin.pojo.Users;
    import com.sin.service.UsersService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service // 表示为Service层
    public class UsersServiceImpl implements UsersService {
    
        @Autowired(required = false)
        private UsersMapper usersMapper;
    
        @Override
        public List<Users> getAllUsers() {
            return usersMapper.getAllUsers();
        }
    
        @Override
        public Users getUserById(int id) {
            return usersMapper.getUserById(id);
        }
    
        @Override
        public void addUser(Users user) {
            usersMapper.addUser(user);
        }
    
        @Override
        public void updateUser(Users user) {
            usersMapper.updateUser(user);
        }
    
        @Override
        public void deleteUser(int id) {
            usersMapper.deleteUser(id);
        }
    }
    
    
    • 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

    UsersController.java

    package com.sin.controller;
    
    import com.sin.pojo.Users;
    import com.sin.service.UsersService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    
    import java.util.List;
    
    @Controller
    public class UsersController {
    
        @Autowired(required = false)
        private UsersService userService;
    
        @GetMapping("/users")
        public String getAllUsers(Model model) {
            List<Users> users = userService.getAllUsers();
            model.addAttribute("users", users);
            return "userList";
        }
    
        @GetMapping("/users/{id}")
        public String getUserById(@PathVariable int id, Model model) {
            Users user = userService.getUserById(id);
            model.addAttribute("user", user);
            return "userDetails";
        }
    
        @GetMapping("/users/add")
        public String addUserForm(Model model) {
            model.addAttribute("user", new Users());
            return "addUser";
        }
    
        @PostMapping("/users")
        public String addUser(@ModelAttribute("user") Users user) {
            userService.addUser(user);
            return "redirect:/users";
        }
    
        @GetMapping("/users/{id}/edit")
        public String editUserForm(@PathVariable int id, Model model) {
            Users user = userService.getUserById(id);
            model.addAttribute("user", user);
            return "editUser";
        }
    
        @PostMapping("/users/{id}")
        public String updateUser(@PathVariable int id, @ModelAttribute("user") Users user) {
            userService.updateUser(user);
            return "redirect:/users";
        }
    
        @GetMapping("/users/{id}/delete")
        public String deleteUser(@PathVariable int id) {
            userService.deleteUser(id);
            return "redirect:/users";
        }
    }
    
    • 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

    userList.html

    DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>用户列表title>
    head>
    <body>
    <h1>用户列表h1>
    <table>
        <thead>
        <tr>
            <th>IDth>
            <th>姓名th>
            <th>邮箱th>
            <th>操作th>
        tr>
        thead>
        <tbody>
        <tr th:each="user : ${users}">
            <td th:text="${user.id}">td>
            <td th:text="${user.name}">td>
            <td th:text="${user.email}">td>
            <td>
                <a th:href="@{/users/{id}(id=${user.id})}">查看a>
                <a th:href="@{/users/{id}/edit(id=${user.id})}">编辑a>
                <a th:href="@{/users/{id}/delete(id=${user.id})}">删除a>
            td>
        tr>
        tbody>
    table>
    <a href="/users/add">添加用户a>
    body>
    html>
    
    • 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

    addUser.html

    DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>添加用户title>
    head>
    <body>
    <h1>添加用户h1>
    <form th:action="@{/users}" method="post" th:object="${user}">
        <label>姓名: <input type="text" th:field="*{name}" />label><br/>
        <label>邮箱: <input type="email" th:field="*{email}" />label><br/>
        <input type="submit" value="添加" />
    form>
    <a href="/users">返回列表a>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    editUser.html

    DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>修改用户title>
    head>
    <body>
    <h1>修改列表h1>
    <form th:action="@{/users/{id}(id=${user.id})}" method="post" th:object="${user}">
        <input type="hidden" th:field="*{id}" />
        <label>姓名: <input type="text" th:field="*{name}" />label><br/>
        <label>邮箱: <input type="email" th:field="*{email}" />label><br/>
        <input type="submit" value="修改" />
    form>
    <a href="/users">返回列表a>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    userDetails.html

    DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>用户详情title>
    head>
    <body>
    <h1>用户详情h1>
    <p>id: <span th:text="${user.id}">span>p>
    <p>姓名: <span th:text="${user.name}">span>p>
    <p>邮箱: <span th:text="${user.email}">span>p>
    <a href="/users">返回列表a>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    效果

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

  • 相关阅读:
    IService的query()和update()
    postgresql数据库定时备份到远程数据库
    通过源码了解Java的自动装箱拆箱
    BS框架说明
    Java 实现图书管理系统
    kubernetes集群搭建Zabbix监控平台
    快手伸手“供给侧”,找到直播电商的“源头活水”?
    深度学习之 7 深度前馈网络
    JAVA毕业设计家教信息管理系统计算机源码+lw文档+系统+调试部署+数据库
    使用共享内存进行进程间通信
  • 原文地址:https://blog.csdn.net/qq_44715376/article/details/132732307