配置mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--1.设置-->
  7. <settings>
  8. <!--配置mybatis自动开启驼峰映射-->
  9. <setting name="mapUnderscoreToCamelCase" value="true"/>
  10. <!--1.设置mybatis自带日志,也可以使用第三方日志-->
  11. <setting name="logImpl" value="STDOUT_LOGGING"/>
  12. </settings>
  13. <!--2.为po包下的实体类设置别名-->
  14. <typeAliases>
  15. <package name="com.wjh.po"/>
  16. </typeAliases>
  17. <!--3.注册xxxMapper.xml-->
  18. <mappers>
  19. <mapper class="com.wjh.dao.BookMapper"/>
  20. </mappers>
  21. </configuration>

由于spring也可以配置mybatis,所以这里只配置了三种配置,而且第3种配置也可以配置在spring中的,如果在spring-dao.xml中配置(还是推荐在spring-dao中配置):

 <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--连接数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--关联Mybatis配置文件,可以理解为引入mybatis-config.xml-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--注册映射器,即xxxMapper.xml,这一步无需再到mybatis-config.xml中注册 
        xxxMapper.xml-->
        <property name="mapperLocations" value="classpath:com/wjh/dao/*.xml"/>
    </bean>

注意:如果在spring-dao中配置了,就不能在到mybatis-config里面配置了

配置spring-dao.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 1.关联数据库文件 -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!-- 2.数据源配置(c3p0) -->
    <!--数据库连接池
        dbcp 半自动化操作 不能自动连接
        c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBatis全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
       <!--注册映射器,即xxxMapper.xml,这一步无需再到mybatis-config.xml中注册 
        xxxMapper.xml-->
        <property name="mapperLocations" value="classpath:com/wjh/dao/*.xml"/>
    </bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.wjh.dao"/>
    </bean>

</beans>

第4点和第21章中的MyBatis-Spring一节有点点不同,在那一节是注册了有SqlSessionTemplate的dao接口实现类,但是这里可以使用另外一种更加方便地配置,我们直接通过spring扫描Dao接口包,动态实现Dao接口注入到spring容器中,就不需要再手动写dao接口地实现类了,更加方便了,但是要知道SqlSessionTemplate的作用。这里的连接池使用的第三方连接池c3p0,不同的连接池会有不一样的配置,第2点需要根据需求配置。

配置database.propertis

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=wujiahao269139

ssm是要连接数据库的名称,该文件需要在spring-dao.xml文件中引入(上述文件已经引入):

 <!-- 1.关联数据库文件 -->
    <context:property-placeholder location="classpath:database.properties"/>

编写xxxMapper.xml文件和dao接口

编写接口:

package com.wjh.dao;

import com.wjh.po.Book;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author wjh
 * @date 2021/7/21 16:58
 * @Package com.wjh.dao
 */
public interface BookMapper {
    /**
     * 添加一本新书
     *
     * @param book 新书对象
     * @return 返回数据库数据发生改变的行数
     */
    int addBook(Book book);

    /**
     * 通过id删除一本书
     *
     * @param id 被删除的书的id
     * @return 返回数据库数据发生改变的行数
     */
    int deleteBookById(@Param("id") int id);

    /**
     * 修改一本书的信息
     *
     * @param book 携带新信息的书
     * @return 返回数据库数据发生改变的行数
     */
    int updateBook(Book book);

    /**
     * 通过书id查询书籍
     *
     * @param id 书的id
     * @return 返回被查询到的书籍
     */
    Book getBookById(@Param("id") int id);

    /**
     * 查询全部书籍
     *
     * @return 返回List<Book>集合
     */
    List<Book> listAllBooks();
}

接着编写对应的BookMapper.xml,如果在spring-dao.xml中已经配置了
“<property name=”mapperLocations” value=”classpath:com/wjh/dao/
.xml”/>”
那么可以无需再去mybatis-config.xml中注册了。

其中Book为po类对象:

package com.wjh.po;

/**
 * @author wjh
 * @date 2021/7/21 16:56
 * @Package com.wjh.po
 */
public class Book {
    private Integer id;
    private String bookName;
    private Integer bookCounts;
    private String detail;

    public Book() {
    }

    public Book(Integer id, String bookName, Integer bookCounts, String detail) {
        this.id = id;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.detail = detail;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBookName() {
        return bookName;
    }

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

    public Integer getBookCounts() {
        return bookCounts;
    }

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

    public String getDetail() {
        return detail;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", bookName='" + bookName + '\'' +
                ", bookCounts=" + bookCounts +
                ", detail='" + detail + '\'' +
                '}';
    }
}