ssm整合实现图书管理系统

Posted 谦谦均

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm整合实现图书管理系统相关的知识,希望对你有一定的参考价值。

最近把ssm框架基本上学习了一遍,这里以ssm框架为基础实现一些简单的图书管理系统,加深对ssm的理解。
一、环境要求:

  • IDEA版本不限,本人用的2019.3.5 x64
  • MySQL 8.0.22 winx64 (5.7版本以上的都可以)
  • Tomcat 9
  • Maven 3.6

如果想对系统有个比较好的理解,最好对mysql数据库,springJavaWebMybatis有一定的了解,以及简单的前端知识。
二:准备数据库
创建一个存放书籍的数据库表

CREATE DATABASE ssmbuild;

CREATE TABLE books(
bookID INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
bookName VARCHAR(100) NOT NULL COMMENT '书名',
bookCount INT(11) 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,'从进门到坐牢');

数据表创建好了之后如下图所示
在这里插入图片描述
三:MavenWeb环境搭建
idea里面新建一个Maven项目

在这里插入图片描述
名字叫做ssmbuild,当然这个名字直随便在这里插入图片描述
完成上述操作后可以看到ssmbuild这个文件夹右下角有个蓝色的部分。
接下来添加一下web的支持,鼠标放在ssmbuild这个文件夹上面,鼠标右键选择如下图所示选项

在这里插入图片描述
进去后选择Web Application
在这里插入图片描述
可以看到项目文件夹下面有个web文件夹,中间有个蓝色圆点。
配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>wb_ssmbuild</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <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</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

接下来建立基本结构和配置框架,文件夹结构如下图所示:

在这里插入图片描述
resources这个文件夹下面新建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>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <package name="com.wb.pojo"/>
    </typeAliases>
    <mappers>
        <mapper class="com.wb.dao.BookMapper"/>
    </mappers>
</configuration>

resources这个文件夹下面新建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">
    <import resource="classpath:spring-dao.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>
</beans>

Mybatis层编写:
resources这个文件夹下面新建数据库配置文件database.properties,配置如下:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull
jdbc.username = root
jdbc.password = root

上面分别是你自己的数据库基本设置,mysql8.0以上的按这个配置就可以,5.7和5.8的可以不加时区设置。

Idea关联数据库:
在这里插入图片描述
从这里进去设置选择mysql,步骤如下
在这里插入图片描述

关联好了之后可以看到刚才创建的数据库在这里插入图片描述
编写数据库对应的实体类 com.wb.pojo.Books

package com.wb.pojo;
/**
 * 实体类
 */
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}

编写Dao层的 Mapper接口!com.wb.dao.BookMapper

package com.wb.dao;
import com.wb.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
 * 增删改查
 */
public interface BookMapper {
    int addBook(Books books);
    int deleteBookById(@Param("bookId") int id);
    int updateBook(Books books);
    Books queryBookById(@Param("bookId")int id);
    List<Books> queryAllBook();
    Books queryBookByName(@Param("bookName")String bookName);


}

编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;com.wb.dao.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.wb.dao.BookMapper">
    <insert id="addBook">
        insert into ssmbuild.books (bookName,bookCounts,detail)
        values (#{bookName},#{bookCounts},#{detail});
    </insert>
    <delete id="deleteBookById" parameterType="int">
        delete from ssmbuild.books where bookID = #{bookId};
    </delete>
    <update id="updateBook" parameterType="Books">
        update ssmbuild.books set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
        where bookID = #{bookID};
    </update>
    <select id="queryBookById" resultType="Books">
        select * from ssmbuild.books
        where bookID = #{bookId}
    </select>
    <select id="queryAllBook" resultType="Books">
        select * from ssmbuild.books
    </select>
    <select id="queryBookByName" resultType="Books">
        select * from ssmbuild.books where bookName = #{bookName}
    </select>
</mapper>

编写Service层的接口com.wb.servive.BookService

package com.wb.service;

import com.wb.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookService {
    int addBook(Books books);
    int deleteBookById(int id);
    int updateBook(Books books);
    Books queryBookById(int id);
    List<Books> queryAllBook();
    Books queryBookByName(String bookName);
}

编写Service层的实现类com.wb.servive.BookServiceImpl

package com.wb.service;
import com.wb.dao.BookMapper;
import com.wb.pojo.Books;
import java.util.List;

public class BookServiceImpl implements BookService {
    private BookMapper bookMapper;
    public void setBookMapper(BookMapper bookMapper){
        this.bookMapper = bookMapper;
    }
    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }
    public int deleteBookById(int id) {
        return bookMapper.deleteBookById(id);
    }
    public int updateBook(Books books) {
        return bookMapper.updateBook(books);
    }
    public Books queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }
    public List<Books> queryAllBook() {
        return bookMapper.queryAllBook();
    }
    public Books queryBookByName(String bookName) {
        return bookMapper.queryBookByName(bookName);
    }
}

接下来配置spring层,刚才在ApplicationContext.xml里面已经引入了下面的文件。
我们去编写Spring整合Mybatis的相关的配置文件;resources文件夹下面spring-dao.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">

    <context:property-placeholder location="classpath:database.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <property name="autoCommitOnClose" value="false"/>
        <property name=[技术篇(java)] 教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis

SSM框架整合---完成对图书数据表的增删改查

SSM学习成果总结,图书管理系统进行三层整合

SSM(整合JavaWeb)添加图书

04 图书管理系统(SSM+LayUi)

ssm整合 书籍管理mybatis层