• SSM整合


    SSM整合

    1. 准备工作


    1.依赖问题

    
    <dependencies>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>
    
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.25version>
        dependency>
    
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.2.8version>
        dependency>
    
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
    
        
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.1version>
            <scope>providedscope>
        dependency>
    
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
    
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.9version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.6version>
        dependency>
    
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.3.15version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.3.15version>
        dependency>
        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.9.6version>
        dependency>
    
    
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.22version>
            <scope>providedscope>
        dependency>
    
        
        <dependency>
            <groupId>org.thymeleafgroupId>
            <artifactId>thymeleaf-spring5artifactId>
            <version>3.0.14.RELEASEversion>
        dependency>
    dependencies>
    
    • 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

    2.静态资源导出问题(对于src/main/java目录下的非.java文件也拷贝到classes目录下)

    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory> 
                <includes>
                    <include>**/*.propertiesinclude> 
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>  
            resource>
        resources>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.包结构创建出来

    main

    ----java (com.sutong.xxx)

    ----resource (xx.xml, xx.properties)

    ----webapp (WEB-INF, web.xml)

    test

    ----java

    ----resource


    2 MyBatis工作

    0.sql-表结构

    CREATE TABLE `t_books` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `count` int(11) DEFAULT NULL,
      `detail` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1.MyBatis配置文件 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"/>
            <setting name="aggressiveLazyLoading" value="false"/>
            <setting name="cacheEnabled" value="true"/>
        settings>
    
        <typeAliases>
            <package name="com.sutong.pojo"/>
        typeAliases>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.pojo准备好(偷懒可以导入lombok包,IDEA需要安装插件支持)

    @Data            // @ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor 集合
    @AllArgsConstructor // 生成全参构造器
    @NoArgsConstructor  // 生成无参构造器
    public class Books {
        private Integer id;
        private String name;
        private Integer count;
        private String detail;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.解决BookMapper.java接口和BookMapper.xml(增删查改!!)文件都放在dao包下

    public interface BookMapper {
        void addBook(Books book);
        void deleteBook(Integer id);
        void updateBook(Books book);
        Books getBookById(Integer id);
        List<Books> getAllBook();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.sutong.dao.BookMapper">
        <insert id="addBook">
            insert into t_books(`name`, `count`, `detail`) values(#{name}, #{count}, #{detail})
        insert>
    
        <delete id="deleteBook">
            delete from t_books where id = #{id}
        delete>
    
        <update id="updateBook">
            update t_books set `name` = #{name}, `count` = #{count}, `detail` = #{detail} where `id` = #{id}
        update>
    
        <select id="getBookById" resultType="books">
            select `id`, `name`, `count`, `detail` from t_books where `id` = #{id};
        select>
    
        <select id="getAllBook" resultType="books">
            select `id`, `name`, `count`, `detail` from t_books;
        select>
    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

    4.编写BookService.javaBookServiceImpl.java类,Service调用Dao层

    service增删改可能有事务问题!!!

    @Transactional  // 所有方法开启事务了(不开启是操作一条数据提交一条)
    public class BookServiceImpl implements BookService {
    
        private BookMapper bookMapper; // 这里下面的配置文件注入,也可以用注解
    
        @Override
        public void addBook(Books book) { bookMapper.addBook(book); }
        @Override
        public void deleteBook(Integer id) { bookMapper.deleteBook(id); }
        @Override
        public void updateBook(Books book) { bookMapper.updateBook(book); }
        @Override
        public Books getBookById(Integer id) { return bookMapper.getBookById(id); }
        @Override
        public List<Books> getAllBook() { return bookMapper.getAllBook(); }
    
        // 配置文件中注入bookMapper用的
        public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3 Spring工作

    database.properties

    # 如果使用MySQL8+版本需要加上一个时区的配置,而且driver也不一样
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
    jdbc.username=root
    jdbc.password=9527
    
    • 1
    • 2
    • 3
    • 4
    • 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">
    
        
        <context:property-placeholder location="classpath:database.properties"/>
    
        
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
              destroy-method="close">
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
            <property name="driverClassName" value="${jdbc.driver}" />
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            
            
            
        bean>
    
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            
            <property name="basePackage" value="com.sutong.dao"/>
        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

    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"
           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
                               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
                               https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        
        <context:component-scan base-package="com.sutong.service"/>
    
        
        <bean id="bookServiceImpl" class="com.sutong.service.impl.BookServiceImpl">
            
            <property name="bookMapper" ref="bookMapper"/>
        bean>
    
        
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        bean>
    
        
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
        <aop:aspectj-autoproxy>aop:aspectj-autoproxy>
    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

    spring-mvc.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.sutong.controller"/>
    
        
        <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:view-controller path="/" view-name="index"/>
    
        
        <mvc:annotation-driven>
            <mvc:message-converters>
                
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="defaultCharset" value="UTF-8" />
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/htmlvalue>
                            <value>text/plainvalue>
                            <value>application/jsonvalue>
                        list>
                    property>
                bean>
            mvc:message-converters>
        mvc:annotation-driven>
    
        
        <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
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    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>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>
    
        
        <servlet>
            <servlet-name>dispatcherServletservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:applicationContext.xmlparam-value> 
                
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>dispatcherServletservlet-name>
            <url-pattern>/url-pattern>
        servlet-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

    4 application.xml

    application.xml总配置,去包含其他三个Spring配置文件!!

    
    <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">
    
        <import resource="spring-dao.xml"/>
        <import resource="spring-service.xml"/>
        <import resource="spring-mvc.xml"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5 Controller层

    @Controller
    @RequestMapping("/book")
    public class BookController {
    
        @Autowired  // 这里只有byType也行
        @Qualifier("bookServiceImpl")
        private BookService bookService;
    
        // 查询全部图书返回给页面显示
        @RequestMapping("/list")
        public String list(Model model) {
            List<Books> books = bookService.getAllBook();
            model.addAttribute("books", books);
            return "allBook";
        }
        
        @RequestMapping("/delete")
        public String delete(Integer id) {
            bookService.deleteBook(id);
            return "redirect:/book/list";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    WEB-INF/templates/index.html

    <body>
        <h3>
            <a th:href="@{/book/list}">进入书籍页面a>
        h3>
    body>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    WEB-INF/templates/allBook.html

    <body>
        <table>
            <tr>
                <th>书名th>
                <th>数量th>
                <th>描述th>
            tr>
            <tr th:each="book : ${books}">
                <td th:text="${book.name}">td>
                <td th:text="${book.count}">td>
                <td th:text="${book.detail}">td>
            tr>
        table>
    body>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    测试:http://localhost:8080/ 进入首页,点击超链接就可以进入allbook.html显示所有图书了

  • 相关阅读:
    JDK8 — 17特性
    直击第一届中国测绘地理信息大会,华测导航强势出圈!
    Nested嵌套对象类型还挺实用
    开源免费的对象存储Minio
    HTTPS(对称加密+非对称加密+证书)
    git设置并记忆用户名密码
    【深度学习】——深度学习中基本的网络结构(2)
    栈(扩容)的初始化、判满、扩容、入栈、获取栈顶元素且删除、获取栈顶元素不删除等等
    麒麟v10 安装jenkins
    php &&和and的区别
  • 原文地址:https://blog.csdn.net/m0_53492310/article/details/126896716