码迷,mamicode.com
首页 > 编程语言 > 详细

Spring整合Mybatis

时间:2020-08-19 19:17:07      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:oca   div   tst   xmlns   apache   dea   class   conf   jdbc   

14、Spring整合Mybatis

官方参考文档:http://mybatis.org/spring/zh/index.html

问题:

(1)为什么要整合Mybatis?

整合之前:我们想要拿到Dao层的对象,必须手动通过SQLSessionFactory来创建

整合之后:我们想要拿到直接通过IOC容器拿到

参考:https://www.sohu.com/a/285808583_194621

1、使用SqlSessionTemplate

步骤:

14.1.1、导入依赖

所需依赖:

  • Junit:测试所需依赖包
  • Lombok:简化实体类get、set等方法
  • 引入Spring相关依赖
  • 引入mybatis依赖
  • 连接数据库的驱动
  • Spring整合mybatis所需依赖包
  • 使用c3p0数据源
<dependencies>
        <!-- 引入Junit测试类 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- 引入LomBok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <!-- 引入Spring相关依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <!-- 引入Mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- 引入连接数据库的驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
        <!-- Spring整合Mybatis的依赖包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- 使用c3p0数据源 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.1</version>
        </dependency>
    	<!-- Spring对jdbc的封装 -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
    </dependencies>

 	<!-- 特别注意:必须放过该部分资源,不然不会被编译 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

14.1.2、编写User实体类

@Data
public class User {
    private int id;
    private int score;
    private String name;
}

14.1.3、编写Dao和配置文件xml

public interface UserDao {
    List<User> selectAll();
}
<?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="com.ch.dao.UserDao">
    <select id="selectAll" resultType="user">
        select * from student;
    </select>
</mapper>

14.1.4、编写mybatis配置文件

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>
    <!-- 设置 -->

    <!-- 配置别名 -->
    <typeAliases>
        <typeAlias type="com.ch.pojo.User" alias="user"/>
        <package name="com.ch.pojo"/>
    </typeAliases>
</configuration>

14.1.5、编写jdbc.properties

mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mysqlstudy?serverTimezone=GMT&useSSL=true
mysql.username=root
mysql.password=root

14.1.6、编写spring-mybatis.xml

整合还需要一个Dao的实现类

<?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
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="jdbc.properties"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
        <property name="driverClass" value="${mysql.driver}"/>
        <property name="jdbcUrl" value="${mysql.url}"/>
        <property name="user" value="${mysql.username}"/>
        <property name="password" value="${mysql.password}"/>
    </bean>
    
    <!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!--        <property name="driverClassName" value="${mysql.driver}"/>-->
<!--        <property name="url" value="${mysql.url}"/>-->
<!--        <property name="username" value="${mysql.username}"/>-->
<!--        <property name="password" value="${mysql.password}"/>-->
<!--    </bean>-->

    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource"  ref="dataSource"/>
        <!-- 关联mybatis配置文件 -->
        <property name="configLocation" value="mybatis-config.xml"/>
        <!-- 添加映射器 -->
        <property name="mapperLocations" value="classpath:com/ch/dao/*.xml"/>
    </bean>

    <!-- 通过配置好的SQLSessionFactory配置SqlSessionTemplate -->
    <!-- 没有set方法,只能通过构造器注入 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <!-- 注入实现类 -->
    <bean id="userDaoImpl" class="com.ch.dao.impl.UserDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
    </bean>

</beans>

14.1.7、编写实现类

public class UserDaoImpl implements UserDao {

    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @Override
    public List<User> selectAll() {
        List<User> users = sqlSessionTemplate.getMapper(UserDao.class).selectAll();
        return users;
    }
}

14.1.8、测试

@Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
    UserDao userDao = context.getBean("userDaoImpl", UserDao.class);
    List<User> users = userDao.selectAll();
    for (User user : users) {
        System.out.println(user);
    }
}

常见问题

1、缺少spring-jdbc

使用jdbc数据源

Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource

使用c3p0数据源

Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ‘dataSource‘ threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:122)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1732)
	... 35 more

2、未放过java下的.xml配置文件

org.apache.ibatis.binding.BindingException: Type interface com.ch.dao.UserDao is not known to the MapperRegistry.

	at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
	at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:745)
	at org.mybatis.spring.SqlSessionTemplate.getMapper(SqlSessionTemplate.java:318)
	at com.ch.dao.impl.UserDaoImpl.selectAll(UserDaoImpl.java:19)
	at MyTest.test(MyTest.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

解决

技术图片

2、使用SqlSessionDaoSupport

方式2直接跳过SqlSessionTemplate,使用SQLSessionFactory来实现

修改的地方如下:

1、spring-mybatis.xml

技术图片

2、实现类

技术图片

Spring整合Mybatis

标签:oca   div   tst   xmlns   apache   dea   class   conf   jdbc   

原文地址:https://www.cnblogs.com/IT_CH/p/13514672.html

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