spring整合mybatis

spring整合mybatis

0.导入pom

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  <!--    mysql数据库驱动程序-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.23</version>
    </dependency>
  <!--    数据库连接池-->
    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>4.0.3</version>
    </dependency>
  <!--    mybatis 依赖-->
	<dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.6</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.6</version>
    </dependency>

    <!--    输出sql语句-->
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.13.3</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.8.0-beta0</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.8.0-beta0</version>
    </dependency>

    <!--    spring 依赖  Controller、Model等-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.3</version>
    </dependency>
    <!-- spring-web注解包 RequestMapping ...-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.5</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version> 5.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>

  </dependencies>

1.创建数据库配置文件(db.properties)

datasource.driverClassName=com.mysql.cj.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/pdd?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8
datasource.username=root
datasource.password=123456
#配置数据相关   使用HikariCP
datasource.type=com.zaxxer.hikari.HikariDataSource
datasource.hikari.minimum-idle=5
datasource.hikari.maximum-pool-size=15
datasource.hikari.auto-commit=true
datasource.hikari.idle-timeout=30000
datasource.hikari.pool-name=DatebookHikariCP
datasource.hikari.max-lifetime=1800000
datasource.hikari.connection-timeout=30000
datasource.hikari.connection-test-query=SELECT 1

2.创建spring.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"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       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 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!--    spring 导入 其他配置 可以删除-->
    <import resource="classpath:springBean/*"/>
    <!--指定数据源配置文件-->
    <context:property-placeholder location="classpath:db.properties" ignore-unresolvable="true"/>
    <!--    配置数据源(数据库连接池)-->
    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
        <property name="driverClassName" value="${datasource.driverClassName}"/>
        <property name="jdbcUrl" value="${datasource.url}"/>
        <property name="username" value="${datasource.username}"/>
        <property name="password" value="${datasource.password}"/>
        <property name="connectionTimeout" value="${datasource.hikari.connection-timeout}"/>
        <property name="idleTimeout" value="${datasource.hikari.idle-timeout}"/>
        <property name="maxLifetime" value="${datasource.hikari.max-lifetime}"/>
        <property name="maximumPoolSize" value="${datasource.hikari.maximum-pool-size}"/>
        <property name="minimumIdle" value="${datasource.hikari.minimum-idle}"/>
        <property name="connectionTestQuery" value="${datasource.hikari.connection-test-query}"/>
        <property name="autoCommit" value="true"/>
    </bean>

    <!--定义sqlSessionFactory对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <!--将数据源对象注入到当前这个对象-->
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="configLocation" value="classpath:mybatis.xml"/>
    </bean>
<!--创建连接对象-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory"/>
    </bean>

    <!--定义扫描mapper接口的配置对象-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定sqlSessionFactory bean对象的名字-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--指定mapper接口所在的包名-->
        <property name="basePackage" value="cdu.lzy.Dao"/>
    </bean>
</beans>

3.配置mybatis文件(打印sql语句)(mybatis.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>
    <settings>
        <!-- 打印查询语句 下面扫描的包的文件 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <typeAliases>
        <package name="cdu.lzy.Dao"/>
    </typeAliases>
</configuration>

4.配置mybatis 映射文件(BlogMapper.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cdu.lzy.Dao.BlogMapper">
    <select id="selectAll" resultType="cdu.lzy.entity.Blog">
        select * from blog
    </select>
</mapper>

5.配置mybatis接口 (BlogMapper)

package cdu.lzy.Dao;
import cdu.lzy.entity.Blog;
import java.util.List;
public interface BlogMapper {
    /**
     * 查询所有的博客信息
     */
    List<Blog> selectAll();
}

6.目录结构及测试方法

spring整合mybatis

testSpring.java


cdu.lzy.Test1;
import cdu.lzy.Dao.BlogMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testSpring {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        SqlSession sqlSession = (SqlSession) applicationContext.getBean("sqlSession");
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        System.out.println(mapper.selectAll());
    }

}
上一篇:动态SQL


下一篇:Java持久层框架Mybatis入门