码迷,mamicode.com
首页 > 其他好文 > 详细

ssm整合 书籍管理mybatis层

时间:2020-11-02 10:28:02      阅读:20      评论:0      收藏:0      [点我收藏+]

标签:smb   not   jin   his   ring   books   tde   放弃   lazy   

学完了ssm框架之后针对图书管理的几个小功能进行针对于ssm框架的整合

首先数据库是最为关键的,同时也是一个项目的基础

use `ssmbuild`;

create table `books`(
`bookID` int(10) not null auto_increment comment 书id,
`bookName` varchar(100) not null comment 书名,
`bookCounts` int(10) not null comment 书数量,
`detail` varchar(200) not null comment描述,
primary key(`bookID`)
)engine=innodb charset=utf8

insert into `books`(`bookID`,`bookName`,`bookCounts`,`detail`)values
(1,Java,1,从入门到放弃),
(2,Mysql,10,从删库到跑路),
(3,Linux,5,从进门到进牢);

我们创建一个数据库,然后创建一张books的表,之后给他插入几行数据

现在开始整合Mybatis

创建一个maven工程:并且导入依赖,还有为防止maven出现资源泄露的问题也加上相关的组件

<!--依赖-->
    <dependencies>
     <!--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 -->
        <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>
        <!--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.2</version>
        </dependency>
        <!--Spring-->
        <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>

    </dependencies>
<!--资源导出问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

之后开始搭建项目的结构

技术图片

我们先开始创建dao层的接口:BookMapper,并添加几个简单地增删改查的小功能

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();
}

之后马上开始对功能进行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.jin.dao.BookMapper">
    <insert id="addBook" parameterType="Books">
        insert into ssmbuild.books(bookID, bookName, bookCounts, detail)
        values (#{bookID},#{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" parameterType="int" resultType="Books">
        select * from ssmbuild.books where bookID=#{bookId}
    </select>

    <select id="queryAllBook" resultType="Books">
        select * from ssmbuild.books
    </select>
</mapper>

写完以后我们把这个接口注册到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>
<!--配置数据源spring去做-->

    <typeAliases>
        <package name="com.jin.pojo"/>
    </typeAliases>

    <mappers>
        <mapper class="com.jin.dao.BookMapper"/>
    </mappers>


</configuration>

补充一下,实体类忘创建了.......

public class Books {
    private int bookID;
    private  String bookName;
    private int bookCounts;
    private String detail;

    public Books() {
    }

    public Books(int bookID, String bookName, int bookCounts, String detail) {
        this.bookID = bookID;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.detail = detail;
    }

    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 + ‘\‘‘ +
                ‘}‘;
    }
}

之后开始编写service层(业务层)这个借口里的代码跟dao层里面的代码几乎是一样的因为service层只需要调用dao层就好

public interface BookService {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById( int id);
    //更新一本书
    int updateBook(Books books);
    //查询一本书
    Books queryBookById( int id);
    //查询全部的书
    List<Books> queryAllBook();

之后是bookService的实现类,要引入dao层只需要引入dao层接口的引用就好

public class BookServiceImpl implements BookService {

    //service调用dao层
    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();
    }
}

这样Mybatis的整合就好了,等待着spring的接管还有springMVC的视图转发就好

 

ssm整合 书籍管理mybatis层

标签:smb   not   jin   his   ring   books   tde   放弃   lazy   

原文地址:https://www.cnblogs.com/joyboy007/p/13837011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!