百木园-与人分享,
就是让自己快乐。

SpringMVC(5)-ssm整合实现增删改查-mybatis层

mybatis层编写完毕后的项目目录

1.右键SpringMVC2项目-》new-》Modual-》选择maven项目(我的项目名为Study09_ssm),输入模块名,点击Finish

2.第二部的操作就是将idea的基本运行环境搞定,包括:添加web支持,配置tomcat,配置project structure

  2.1添加web支持:右键项目名为Study09_ssm,选择Add Framework Support,勾选Web Application,点击ok

  2.2配置tomcat(注意:如果第一步没有配置的话,tomcat里边不会出现这个项目的)

  2.3配置project structure,选择idea菜单栏File->project structure->Artifact->找到自己的这个项目点击->在WEB-INF下新建一个lib文件夹(默认是没有的),然后点击加号,选择所有的依赖,点击ok

3.新建一个数据库命名为ssm_crud,新建一个查询窗口,执行如下的sql代码

USE `ssm_crud`;
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
  `bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT \'书id\',
  `bookName` VARCHAR(100) NOT NULL COMMENT \'书名\',
  `bookCounts` 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,\'从进门到进牢\');

4.让idea连接上MySql数据库,具体步骤如下:找到ida右侧的database选项,点击加号-》data source-》mysql,打开data source and driver窗口,也就是第二张图,根据步骤做完即可,完成后会出现第四张图的样子。

5.建立项目目录结构,如下图所示:Java文件夹下的四个包,web模块不用创建

6.resources文件夹下创建下面的三个文件

7.database.properties文件为数据库连接串,mybatis-config.xml文件为mybatis配置,这里只写别名和mappers即可,其他的让spring来做,至此mybatis层的resources文件夹下的内容搞定,applicationContext.xml是用来整合各层的配置文件的,这个最后配置。

jdbc.driver=com.mysql.jdbc.Driver
#mysql8.0+的话,要加一个时区设置&serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssm_crud?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=CST
jdbc.username=root
jdbc.password=123456
<?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.xiaoma1.pojo\"/>
    </typeAliases>

    <mappers>
        <mapper class=\"com.xiaoma1.dao.BookMapper\"></mapper>
    </mappers>
</configuration>

8.下面来做Java文件夹下的东西,包括:实体类,dao层的mapper接口和mapper.xml配置文件,service层的接口和实现类(我们这里主要实现对书籍的增删改查)

   8.1编写实体类:属性名和数据库字段名一一对应,这里的get、set方法、有参无参构造通过三个注解来实现,分别是@Data、@NoArgsConstructor、@AllArgsConstructor

package com.xiaoma1.pojo;

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

/**
 * Author:xiaoma
 * FileName:Books
 * Data:2022/5/3 10:21
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}  

  8.2编写dao层的mapper接口和mapper对应的xml文件,xml文件中编写sql语句实现和数据库的交互,下面为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\">
<!--绑定接口BookMapper-->
<mapper namespace=\"com.xiaoma1.dao.BookMapper\">
    <!--//增加一本书-->
    <insert id=\"addBook\" parameterType=\"Books\">
        insert into books (bookName,bookCounts,detail)
        values (#{bookName},#{bookCounts},#{detail});
    </insert>

    <!--/删除一本书-->
    <delete id=\"deleteBookById\" parameterType=\"int\">
        delete
        from books
        where bookID=#{id};
    </delete>

    <!--//更新一本书-->
    <update id=\"updateBook\" parameterType=\"Books\">
        update books
        set bookName = #{bookName},bookCounts = #{bookCounts},detail=#{detail}
        where bookID = #{bookID};
    </update>

    <!--//查询一本书-->
    <select id=\"queryBookById\" resultType=\"Books\" parameterType=\"int\">
        select *
        from books
        where bookID=#{id};
    </select>

    <!--//查询全部的书-->
    <select id=\"queryAllBook\" resultType=\"Books\">
        select * from books;
    </select>

</mapper>
package com.xiaoma1.dao;

import com.xiaoma1.pojo.Books;

import java.util.List;

/**
 * Author:xiaoma
 * FileName:BookMapper
 * Data:2022/5/3 10:23
 */
public interface BookMapper {
    //增加一本书
    int addBook(Books books);

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

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

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

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

  8.3编写service层的接口和实现类,这里service\'层的接口和dao层的mapper接口是一样的

package com.xiaoma1.service;

import com.xiaoma1.pojo.Books;

import java.util.List;

/**
 * Author:xiaoma
 * FileName:BookService
 * Data:2022/5/3 10:45
 */
public interface BookService {
    //增加一本书
    int addBook(Books books);

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

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

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

    //查询全部的书
    List<Books> queryAllBook();
}
package com.xiaoma1.service;

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

import java.util.List;

/**
 * Author:xiaoma
 * FileName:BookServiceImpl
 * Data:2022/5/3 10:45
 */

public class BookServiceImpl implements BookService{
    //业务层调用dao层,组合dao
    public 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();
    }
}

至此mybatis层实现完毕!!!!!!!!!

 


来源:https://www.cnblogs.com/XiaoMaGuai/p/16218031.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » SpringMVC(5)-ssm整合实现增删改查-mybatis层

相关推荐

  • 暂无文章