ssm框架整合(一)

环境搭建

1.构建数据库,创建表格中对应字段

CREATE DATABASE ssmframework;
USE ssmframework;
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,从进门到进牢)

2.创建maven项目,导入相关依赖

Junit

<!-- JUnit -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.0-M1</version>
            <scope>test</scope>
        </dependency>

数据库驱动

<!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.24</version>
        </dependency>

数据库连接池

<!-- 数据库连接池 : c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>

Spring MVC相关依赖

<!-- Servlet-jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

Mybatis、Mybatis-Spirng相关依赖

<!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>

Spring相关依赖

<!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.7</version>
        </dependency>
    </dependencies>

配置静态资源过滤

<!-- 静态资源导出 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

3.创建框架所需配置文件

  • ApplicationContext.xml(Spring配置文件)
  • mybatis-config.xml(Mybatis配置文件)
  •  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>

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>

4.建立项目基本结构

  • pojo层:com.autumn.pojo
  • mapper层:com.autumn.mapper
  • service层:com.autumn.service
  • controller层:com.autumn.controller

ssm框架整合(一)


 

 Mybatis层编写

1.配置数据库配置文件database.properties

jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username = root
jdbc.password = 123456

当Mysql版本为5时,driver改为jdbc.driver = com.mysql.cj.jdbc.Driver

2.idea关联数据库

ssm框架整合(一)

 URL注意时区问题

3.编写pojo实现类和mapper接口类

  pojo下的Books.java推荐和数据库中字段一致,以便带来不必要的麻烦

public class Books {

    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;

    public Books() {
    }

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getBookCounts() {
        return bookCounts;
    }

    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    @Override
    public String toString() {
        return "Books{" +
                "bookID=" + bookID +
                ", bookName=‘" + bookName + ‘\‘‘ +
                ", bookCounts=" + bookCounts +
                ", detail=‘" + detail + ‘\‘‘ +
                ‘}‘;
    }
}

mapper下的BooksMapper.java

package com.autumn.mapper;

import com.autumn.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookMapper {

    //增加一本书
    int addBook(Books books);

    //根据ID删除一本书
    int deleteBookById(int id);

    //更新一本书
    int updateBook(Books books);

    //根据ID查询一本书
    Books queryBookById(int id);

    //查询全部书
    List<Books> queryAllBook();
}

4.编写mapper接口对应的Sql语句

BookMapper.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.autumn.mapper.BookMapper">
    <insert id="addBook" parameterType="com.autumn.pojo.Books">
        insert into ssmframework.books(bookName, bookCounts, detail)
        values (#{bookName} , #{bookCounts} , #{detail});
    </insert>

    <delete id="deleteBookById" parameterType="int">
        delete
        from ssmframework.books
                 where bookID = #{bookID};
    </delete>
    
    <update id="updateBook" parameterType="com.autumn.pojo.Books">
        update ssmframework.books
            set bookName = #{bookName} ,bookCounts = #{bookCounts} ,detail = #{detail}
        where bookID = #{bookID};
    </update>

    <select id="queryBookById" parameterType="int" resultType="com.autumn.pojo.Books">
        select *
        from ssmframework.books where bookID = #{bookID};
    </select>

    <select id="queryAllBook" resultType="com.autumn.pojo.Books">
        select *
        from ssmframework.books;
    </select>
</mapper>

注意返回类型是一个类时,resultType写全路径

5.配置Mybatis核心配置文件

注册BookMapper

<?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>
    <mappers>
        <mapper class="com.autumn.mapper.BookMapper"/>
    </mappers>
</configuration>

6.编写service业务类

  service层调用mapper层

  service层和mapper层一样 都是先设计接口,再设计实现接口的类

public interface BookService {

    //增加一本书
    int addBook(Books books);

    //根据ID删除一本书
    int deleteBookById(int id);

    //更新一本书
    int updateBook(Books books);

    //根据ID查询一本书
    Books queryBookById(int id);

    //查询全部书
    List<Books> queryAllBook();
}

7.编写service的实现类

public class BookServiceImpl implements BookService{
    //Service层 调用 Mapper层
    private BookMapper bookMapper;

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

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

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

    @Override
    public int updateBook(Books books) {
        return bookMapper.updateBook(books);
    }

    @Override
    public Books queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }

    @Override
    public List<Books> queryAllBook() {
        return bookMapper.queryAllBook();
    }
}

 

ssm框架整合(一)

上一篇:asp.net mvc 客户端(&)中检测到有潜在危险的 Request.Path 值。


下一篇:读书笔记: 深入浅出node.js