构建SSM框架基本流程

1.配置数据库环境

创建一个存放书籍的数据库表

CREATE TABLE `library` (
  `bookID` varchar(10) NOT NULL,
  `bookName` varchar(255) NOT NULL,
  `bookCounts` int(100) NOT NULL,
  `detail` varchar(255) NOT NULL,
  PRIMARY KEY (`bookID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.pom.xml文件配置

2.1导入相关依赖

<dependencies>
<!--        测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
<!--        数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
<!--        数据库连接池 c3p0-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
<!--        servlet jsp-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2.1-b03</version>
        </dependency>
        <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.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
<!--        Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
        </dependency>
    </dependencies>

2.2静态资源导出问题

 <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

2.3Error:java: 不再支持源选项 5。请使用 6 或更高版本。

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
</properties>

建立基本结构和框架配置

  • com.dz.pojo
  • com.dz.dao
  • com.dz.service
  • com.dz.controller
  • mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>
  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">

</beans>

3.编写数据库对应的实体类

com.dz.pojo.BookMapper

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    private String bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}

4.编写Dao层的Mapper接口

实现书籍的增删改查操作

public interface BookMapper {
    //增加一本书
    int addBook(Books books);

    int deleteBookById(String id);

    int updataBook(Books books);

    Books queryBook(String id);

    //查询全部的书
    List<Books> queryAllBooks();
    //通过书名查询书籍
    Books queryBookByName(String BookName);
}

5.编写接口对应的Mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dz.dao.BookMapper">
    <insert id="addBook" parameterType="Books">
        insert into ssmbuild.library (bookID,bookName,BookCounts,detail)
        values (#{bookID},#{bookName},#{bookCounts},#{detail})
    </insert>

    <delete id="deleteBookById" parameterType="String">
        delete from ssmbuild.library where bookID=#{bookID}
    </delete>

    <update id="updataBook" parameterType="Books">
        update ssmbuild.library set
        bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
        where bookID=#{bookID}
    </update>

    <select id="queryBook" resultType="Books">
        select * from ssmbuild.library where bookID=#{bookID}
    </select>

    <select id="queryAllBooks" resultType="Books">
        select * from ssmbuild.library
    </select>

    <select id="queryBookByName" resultType="Books">
        select * from ssmbuild.library where bookName=#{bookName}
    </select>

</mapper>

6.编写Service层的接口和实现类

接口

public interface BookService {
    //增加一本书
    int addBook(Books books);

    int deleteBookById(String id);

    int updataBook(Books books);

    Books queryBook(String id);

    //查询全部的书
    List<Books> queryAllBooks();

    //通过书名查询书籍
    Books queryBookByName(String BookName);
}

实体类

public class BooksServiceImpl implements BookService {
    //业务层调Dao层
    private BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }

    public int deleteBookById(String id) {
        return bookMapper.deleteBookById(id);
    }

    public int updataBook(Books books) {
        return bookMapper.updataBook(books);
    }

    public Books queryBook(String id) {
        return bookMapper.queryBook(id);
    }

    public  List<Books> queryAllBooks() {
        return bookMapper.queryAllBooks();
    }

    @Override
    public Books queryBookByName(String BookName) {
        return bookMapper.queryBookByName(BookName);
    }
}

7.配置mybatis-config.xml文件

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

<!--    将写的Mapper接口注册到mybatis中-->
    <mappers>
        <mapper class="com.dz.dao.BookMapper"/>
    </mappers>

</configuration>

8.Spring层

8.1Spring-dao.xml

作用:连接数据库以及将Dao中的接口注入到Spring容器内

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--    连接数据库-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mysql?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--        <property name="mapperLocations" value="classpath:com/dz/Mapper/*.xml"/>-->
    </bean>

<!--  配置Dao接口扫描包,动态实现Dao接口注入到Spring容器中-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
       <property name="basePackage" value="com.dz.dao"/>
   </bean>

</beans>

8.2 spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--  扫描Service下的包-->
    <context:component-scan base-package="com.dz.Service"/>

<!--    将所有的业务类,注入到Spring中-->
    <bean id="BookSreviceImpl" class="com.dz.Service.BooksServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>
<!--    声明式事务配置-->
    <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--        注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

9.SpringMVC层

9.1web.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">
    
    <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>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

9.2 spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:component-scan base-package="com.dz.Controller"/>

    <mvc:default-servlet-handler/>

    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

9.3 applicationContext.xml(Spring配置整合文件)

<?xml version="1.0" encoding="UTF-8"?>
<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="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-mvc.xml"/>
    <import resource="classpath:spring-service.xml"/>
</beans>

10. 编写Controller层和视图层

10.1 查询全部书籍

1.编写BookController类——查询书籍

@Controller
@RequestMapping("/book")
public class BookControlller {
    //controllerc层调Service层
    @Autowired
    //指定实现类
    @Qualifier("BookSreviceImpl")
     private BookService bookService;

    //查询所有的书籍,并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> books = bookService.queryAllBooks();
        model.addAttribute("books",books);
        return "allBook";
    }

2.编写首页——index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
   <style>
           a{
               text-decoration: none;
               color: deepskyblue;
               font-size: 18px;
           }
       </style>
  </head>
  <body>
<h3>
    <a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a>
</h3>
  </body>
</html>

3.书籍列表界面——allBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>书籍展示</title>
<%--    BootStrap美化界面--%>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>书籍列表</small>
                </h1>
            </div>
        </div>
        <div class="row">
            <div col-md-4 column>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddbook">新增书籍</a>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示全部书籍</a>
            </div>
            <div col-md-4 column>
<%--               查询书籍--%>
                <form action="${pageContext.request.contextPath}/book/queryBook" method="post" class="form-inline" style="float: right">
                    <input type="text" name="queryBookByName" class="form-control" placeholder="请输入要查询的书籍名称">
                    <input type="submit" value="查询" class="btn btn-primary">
                </form>
            </div>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                  <tr>
                      <th>书籍编号</th>
                      <th>书籍名称</th>
                      <th>书籍数量</th>
                      <th>书籍详情</th>
                      <th>修改</th>
                  </tr>
                </thead>
                <tbody>
                 <c:forEach var="book" items="${books}">
                     <tr>
                         <td>${book.bookID}</td>
                         <td>${book.bookName}</td>
                         <td>${book.bookCounts}</td>
                         <td>${book.detail}</td>
                         <td>
                             <a href="${pageContext.request.contextPath}/book/toupdata?id=${book.bookID}">修改</a>
                             &nbsp; | &nbsp;
                             <a href="${pageContext.request.contextPath}/book/deleteBook?id=${book.bookID}">删除</a>
                         </td>
                     </tr>
                 </c:forEach>
                </tbody>
            </table>
        </div>

    </div>
</div>

</body>
</html>

10.2 添加书籍

1.BookController类编写——添加书籍

//跳转到增加书籍页面
    @RequestMapping("/toAddbook")
    public String toAddBook(){
        return "addBook";
    }
     //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        bookService.addBook(books);
        //添加成功后,回到首页   重定向
        return "redirect:/book/allBook";
    }

2.添加书籍页面——addBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加书籍</title>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>新增书籍</small>
                </h1>
            </div>
        </div>
    </div>
</div>

<form action="${pageContext.request.contextPath}/book/addBook" method="get">
    <div class="form-group">
        <label>书籍编号</label>
        <input type="text" name="bookID" class="form-control" required>
    </div>
    <div class="form-group">
        <label>书籍名称</label>
        <input type="text" name="bookName" class="form-control" required>
    </div>
    <div class="form-group">
        <label>书籍数量</label>
        <input type="text" name="bookCounts" class="form-control" required>
    </div>
    <div class="form-group">
        <label>书籍描述</label>
        <input type="text" name="detail" class="form-control" required>
    </div>
    <div class="form-group">
        <input type="submit" class="form-control" value="添加">
    </div>
</form>

</body>
</html>

10.3 修改书籍

1. 编写BookController类——修改书籍

//跳转到修改页面
    @RequestMapping("/toupdata")
    public String toUpdatapage(String id,Model model){
        Books books = bookService.queryBook(id);
        model.addAttribute("books",books);
        return "updataBook";
    }
    //修改书籍
    @RequestMapping("updataBook")
    public String updataBook(Books books){
        System.out.println("updataBook="+books);
        bookService.updataBook(books);
        return "redirect:/book/allBook";
    }

2. 修改书籍页面——updataBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改书籍</title>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>修改书籍</small>
                </h1>
            </div>
        </div>
    </div>
</div>

<form action="${pageContext.request.contextPath}/book/updataBook" method="get">
    <div class="form-group">
        <label>书籍编号</label>
        <input type="text" name="bookID" class="form-control" value="${books.bookID}" required>
    </div>
    <div class="form-group">
        <label>书籍名称</label>
        <input type="text" name="bookName" class="form-control"  value="${books.bookName}" required>
    </div>
    <div class="form-group">
        <label>书籍数量</label>
        <input type="text" name="bookCounts" class="form-control"  value="${books.bookCounts}" required>
    </div>
    <div class="form-group">
        <label>书籍描述</label>
        <input type="text" name="detail" class="form-control"  value="${books.detail}" required>
    </div>
    <div class="form-group">
        <input type="submit" class="form-control" value="修改">
    </div>
</form>

</body>
</html>

10.4 删除书籍

编写bookController类——删除书籍

 //删除书籍
    @RequestMapping("/deleteBook")
    public String deleteBook(String id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

10.5 通过书籍名称查询书籍

编写bookController类——通过书籍名称查询书籍

@RequestMapping("queryBook")
    public String queryBook(String queryBookByName,Model model){
        Books book = bookService.queryBookByName(queryBookByName);
        List<Books> books = new ArrayList<>();
        books.add(book);
        model.addAttribute("books",books);
        return "allBook";
    }

后话

配置Tomcat,进行运行。

项目结构图

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值