SpringMVC学习07之SSM整合(二)

回顾

我的环境

环境:

  • IDEA
  • MySQL 5.7.34
  • Tomcat 8
  • Maven 3.8.3

要求:
需要熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识;

Mybatis层编写

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

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

2、IDEA关联数据库

3、编写MyBatis的核心配置文件

<?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.shan.pojo"/>
    </typeAliases>

    <mappers>
        <mapper resource="com/shan/dao/BookMapper.xml"/>
    </mappers>

</configuration>

4、编写数据库对应的实体类 com.shan.pojo.Books

使用lombok插件!

package com.shan.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

}

5、编写Dao层的 Mapper接口!

package com.shan.dao;

import com.shan.pojo.Books;
import java.util.List;

public interface BookMapper {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBook(int bookID);
    //更新一本书
    int updateBook(Books books);
    //通过id查询一本书
    Books queryBookById(int bookID);
    //查询所有书
    List<Books> queryAllBook();
    //通过书籍名称查询书籍
    List<Books> queryBookByName(String bookName);

}

6、编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;

<?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.shan.dao.BookMapper">
    <insert id="addBook" parameterType="books">
        insert into ssmbuild.books (bookID, bookName, bookCounts, detail) VALUES (#{bookID}, #{bookName}, #{bookCounts}, #{detail})
    </insert>
    <delete id="deleteBook" 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>

7、编写Service层的接口和实现类

接口:

package com.shan.service;

import com.shan.pojo.Books;

import java.util.List;
//BookService:底下需要去实现,调用dao层
public interface BookService {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBook(int bookID);
    //更新一本书
    int updateBook(Books books);
    //通过id查询一本书
    Books queryBookById(int bookID);
    //查询所有书
    List<Books> queryAllBook();
    //通过书籍名称查询书籍
    List<Books> queryBookByName(String bookName);

}

实现类:

package com.shan.service;

import com.shan.dao.BookMapper;
import com.shan.pojo.Books;

import java.util.List;

public class BookServiceImpl implements BookService{
    //调用dao层的操作,设置一个set接口,方便Spring管理
    private BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }
    @Override
    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }

    @Override
    public int deleteBook(int bookID) {
        return bookMapper.deleteBook(bookID);
    }

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

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

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

    @Override
    public List<Books> queryBookByName(String bookName) {
        return bookMapper.queryBookByName(bookName);
    }
}

到此Mybatis层编写完成!

作者有话说

博客创作不易,希望看到这里的读者动动你的小手点个赞,如果喜欢的小伙伴可以一键三连,作者大大在这里给大家谢谢了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值