zoukankan      html  css  js  c++  java
  • 整合SSM

    环境要求

    • IDEA
    • MySQL 5.7.19
    • Tomcat9
    • Maven 3.5.4

    数据库准备

    CREATE DATABASE ssmbuild;
    USE ssmbuild;
    CREATE TABLE `books`(
    `bookID` INT NOT NULL AUTO_INCREMENT COMMENT '书id',
    `bookName` VARCHAR(100) NOT NULL COMMENT '书名',
    `bookCounts` INT NOT NULL COMMENT '数量',
    `detail` VARCHAR(200) NOT NULL COMMENT '描述',
    KEY `bookID`(`bookID`)
    )ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
    (1,'Java',1,'从入门到放弃'),
    (2,'MySQL',10,'从删库到跑路'),
    (3,'Linux',5,'从进门到进牢')
    

    基本环境搭建

    创建一个普通的maven项目

    依赖和资源过滤

    <!--        junit测试-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
    <!--        数据库驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
    <!--        数据库连接池-->
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
    
    <!--        Servlet JSP-->
            <!--        Servlet依赖-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <!--        JSP依赖-->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.3</version>
                <scope>provided</scope>
            </dependency>
            <!--        jstl表达式的依赖-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    
    <!--        Mybatis-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.5</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.2</version>
            </dependency>
    
    <!--        Spring-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.2.7.RELEASE</version>
            </dependency>
    

    idea连接数据库

    jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=GMT%2B8
    

    编写包结构和配置文件

    image-20200826093300094

    applicationContext.xml

    <?xml version="1.0" encoding="UTF8"?>
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    </beans>
    

    mybatis-config.xml

    <?xml version="1.0" encoding="UTF8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
    </configuration>
    

    Mybatis层编写

    db.properties

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

    配置数据源mybatis-config.xml

    <?xml version="1.0" encoding="UTF8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <typeAliases>
            <package name="com.jmu.pojo"/>
        </typeAliases>
    </configuration>
    

    编写Mapper层

    • 接口(CRUD)
    package com.jmu.mapper;
    
    import com.jmu.pojo.Books;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public interface BookMapper {
        int addBook(Books book);
        int deleteBook(@Param("bookID") int id);
        int updateBook(Books book);
        Books selectBook(@Param("bookID") int id);
        List<Books> selectAllBook();
    }
    
    • xml
    <?xml version="1.0" encoding="UTF8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.jmu.mapper.BookMapper">
        <insert id="addBook" parameterType="Books">
            insert into books(bookName, bookCounts, detail)
            values(#{bookName},#{bookCounts},#{detail});
        </insert>
        <delete id="deleteBook">
            delete from books where bookID=#{bookID};
        </delete>
        <update id="updateBook" parameterType="Books">
            update books set
            bookName=#{bookName},bookCounts=#{bookCounts},
            detail=#{detail} where bookID=#{bookID};
        </update>
        <select id="selectBook" resultType="Books">
            select * from books where bookID=#{bookID};
        </select>
        <select id="selectAllBook" resultType="Books">
            select * from books;
        </select>
    
    </mapper>
    

    将mapper绑定到配置文件

    <?xml version="1.0" encoding="UTF8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <typeAliases>
            <package name="com.jmu.pojo"/>
        </typeAliases>
        <mappers>
            <mapper class="com.jmu.mapper.BookMapper" />
        </mappers>
    </configuration>
    

    编写业务层

    package com.jmu.service;
    
    import com.jmu.pojo.Books;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public interface BookService {
        int addBook(Books book);
        int deleteBook(@Param("bookID") int id);
        int updateBook(Books book);
        Books selectBook(@Param("bookID") int id);
        List<Books> selectAllBook();
    }
    
    package com.jmu.service;
    
    import com.jmu.mapper.BookMapper;
    import com.jmu.pojo.Books;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    
    public class BookServiceImpl implements  BookService{
    
        //service调用mapper层 组合mapper
       private BookMapper bookMapper;
    
        public void setBookMapper(BookMapper bookMapper) {
            this.bookMapper = bookMapper;
        }
    
        public int addBook(Books book) {
            return bookMapper.addBook(book);
        }
    
        public int deleteBook(int id) {
            return bookMapper.deleteBook(id);
        }
    
        public int updateBook(Books book) {
            return bookMapper.updateBook(book);
        }
    
        public Books selectBook(int id) {
            return bookMapper.selectBook(id);
        }
    
        public List<Books> selectAllBook() {
            return bookMapper.selectAllBook();
        }
    }
    

    Spring层编写

    • 连接池
      • dbcp:半自动操作 不能自动连接
      • c3p0:自动化操作(自动化的加载配置文件,并且可以设置到对象中)
      • druid

    编写spring-mapper.xml

    <?xml version="1.0" encoding="UTF8"?>
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--   1.联数据库配置文件-->
        <context:property-placeholder location="classpath:db.properties" />
    <!--    2.连接池-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driver}" />
            <property name="jdbcUrl" value="${url}"/>
            <property name="user" value="${username}" />
            <property name="password" value="${password}"/>
        </bean>
    <!--    3.sqlSessionFactory-->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
            <property name="dataSource" ref="dataSource" />
    <!--        绑定mybatis配置文件-->
            <property name="configLocation" value="classpath:mybatis-config.xml" />
        </bean>
    
    <!--    配置mapper接口扫描包,动态实现mapper接口可以注入到Spring接口中-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!--注入sqlSessionFactory-->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><!--value 就是3的id-->
            <!--要扫描的mapper包-->
            <property name="basePackage" value="com.jmu.mapper" />
        </bean>
    </beans>
    

    编写spring-service.xml

    注意每次新建的时候,添加到ApplicationContext

    image-20200826200346198

    <?xml version="1.0" encoding="UTF8"?>
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--    1.扫描service下的包-->
        <context:component-scan base-package="com.jmu.service" />
    <!--    2.将我们的所有业务类,注入到Spring.可以通过配置,或者注解实现-->
        <bean id="BookServiceImpl" class="com.jmu.service.BookServiceImpl">
            <property name="bookMapper" ref="bookMapper" />
        </bean>
    <!--    3.声明式事务配置-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--        注入数据源-->
            <property name="dataSource" ref="dataSource" />
        </bean>
    </beans>
    

    SpringMVC层编写

    创建项目为web

    image-20200826200926460

    编写web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <!--    DispatchServlet-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!--    乱码过滤-->
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- 允许返回html视图 -->
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>
        <!--    设置首页-->
        <welcome-file-list>
            <welcome-file>WEB-INFviewindex.html</welcome-file>
        </welcome-file-list>
    </web-app>
    

    编写web.xml中的spring-mvc.xml

    编写spring-mvc.xml

    <?xml version="1.0" encoding="UTF8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/context 
            https://www.springframework.org/schema/context/spring-context.xsd">
        <!--1.注解驱动 -->
        <mvc:annotation-driven />
        <!--2.静态资源过滤 -->
        <mvc:default-servlet-handler />
        <!--3.扫描包:controller-->
        <context:component-scan base-package="com.jmu.controller" />
        <!--4.视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
        <!--静态资源映射-->
        <mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
    </beans>
    

    新建spring-mvc.xml中的文件夹jsp

    整合

    import导入3个classpath

    <?xml version="1.0" encoding="UTF8"?>
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        <import resource="classpath:spring-mapper.xml" />
        <import resource="classpath:spring-service.xml"/>
        <import resource="classpath:spring-mvc.xml" />
    </beans>
    

    查询图书

    controller

    package com.jmu.controller;
    
    import com.jmu.pojo.Books;
    import com.jmu.service.BookService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.List;
    
    @Controller
    public class BookController {
        //controller调用service层
        @Autowired
        @Qualifier("BookServiceImpl")
        private BookService bookService;
    
        //查询全部书籍,并且返回到一个书籍展示页面
        @RequestMapping("/allbook")
        public String list(Model model){
            List<Books> list = bookService.selectAllBook();
            model.addAttribute("list",list);
            return "allBook";
        }
    }
    

    页面

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>查询全部书籍</title>
    </head>
    <body>
    <p>书籍展示</p>
    <table>
        <thead>
        <tr>
            <th>图书编号</th>
            <th>图书名称</th>
            <th>图书数量</th>
            <th>图书详情</th>
        </tr>
        </thead>
        <tbody>
            <c:forEach var="book" items="${list}">
                <tr>
                    <td>${book.bookID}</td>
                    <td>${book.bookName}</td>
                    <td>${book.bookCounts}</td>
                    <td>${book.detail}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
    </body>
    </html>
    

    配置Tomcat

    注意在项目结构中添加lib文件夹加jar包

    不然会出现报错,一看到ClassNotFoundException,就去看看lib那边是不是加了jar

    java.lang.ClassNotFoundException: 
    
  • 相关阅读:
    Linux下的sleep()和sched_yield()(转)
    各种字符串Hash函数(转)
    linux 实时监控网速脚本(转)
    linux安装chrome及chromedriver(转)
    Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 解决办法(转)
    linux 运行时限制CPU核数、内存、读写速度
    C语言函数sscanf()的用法-从字符串中读取与指定格式相符的数据(转)
    golang在线学习与编译网站
    电子书转换网站推荐
    入门级网站经典 w3cschool
  • 原文地址:https://www.cnblogs.com/10134dz/p/13585338.html
Copyright © 2011-2022 走看看