Version: Next

整合Mybatis

环境依赖

  • IDEA
  • Mysql 5.7.19
  • Tomcat 9
  • Maven 3.6

数据库环境

闯进啊一个存书数据库表

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

环境搭建

  • 新建Maven项目

  • 添加WEB支持

  • 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>
    <!-- lombok -->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.8</version>
    <scope>provided</scope>
    </dependency>
    </dependencies>
  • Maven静态资源导出问题

    <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>
  • 建包

    • pojo
    • dao(mapper)
    • service
    • controller
  • 建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>
      </configuration>
    • 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

  • 数据库配置文件database.properties

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

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

Dao

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> queryAllBooks();
}
  • 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.bsx.dao.BookMapper">
    <insert id="addBook" parameterType="com.bsx.pojo.Books">
    insert into books (bookName, bookCounts, detail) values
    (#{bookName}, #{bookCounts}, #{detail});
    </insert>
    <delete id="deleteBookById">
    delete from books where bookID = #{bookID};
    </delete>
    <update id="updateBook" parameterType="com.bsx.pojo.Books">
    update books set bookCounts = #{bookCounts}, bookName = #{bookName}, detail =
    #{detail} where bookID = #{bookID};
    </update>
    <select id="queryBookById" resultType="com.bsx.pojo.Books">
    select * from books where bookID = #{bookID};
    </select>
    <select id="queryAllBooks" resultType="com.bsx.pojo.Books">
    select * from books
    </select>
    </mapper>
  • 将mapper.xml绑定到mybatis配置文件中

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

Service

  • BookService

    public interface BookService {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(@Param("bookID") int id);
    //修改一本书
    int updateBook(Books books);
    //查询一本书
    Books queryBookById(@Param("bookID") int id);
    //查询所有书
    List<Books> queryAllBooks();
    }
  • BookServiceImpl

    • 组合一个BookMapper对象
    • 写一个构造方法或者set方法供Spring实现依赖注入
@Service("bookService")
public class BookServiceImpl implements BookService {
//业务层调用Dao,组合一个BookMapper
//也可以用构造器注入,设置一个构造方法就行
@Autowired
private BookMapper bookMapper;
//用来Spring Set注入
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> queryAllBooks() {
return bookMapper.queryAllBooks();
}
}