小白日记:spring整合mybatis全过程

整合mybatis

  1. 导入Jar包
    • junit
    • mybatis
    • spring
    • 数据库
    • AOP织入
    • mybatis-spring

文件结构

在这里插入图片描述

pom.xml

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
    </dependencies>

</project>
  1. spring配置mybatis文件
<?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:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="db.properties"/>
    <!-- bean definitions here -->

    <!--使用spring的数据源替换mybatis的数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--创建一个SqlSessionFactory 并注入dataSource-->
    <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/moro/mapper/*.xml"/>
    </bean>
    <!--注入SQLSessionTemplate-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--没有set方法,只能使用构造器-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
    <!--给AccountMapper中的template注入-->
    <bean id="accountMapper" class="com.moro.mapper.impl.AccountMapperImpl">
        <property name="session" ref="sqlSession"/>
    </bean>
</beans>

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>

    <!--设定实体类别名,这样设置后pojo包下的所有实体类的别名默认为首字母小写的类名,也可以用@Alias("别名")去指定每个实体类的别名-->
    <typeAliases>
        <package name="com.moro.pojo"/>
    </typeAliases>

</configuration>

mapper映射文件

<?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.moro.mapper.AccountMapper">

    <select id="findAll" resultType="account">
       select * from account;
   </select>

</mapper>

接口文件


import java.util.List;

/**
 * @author Moro
 * @createTime 2020/5/25 -16:39
 * @projectName Moro_Demo_Spring-mybatis
 */
public interface AccountMapper {
    List<Account> findAll();
}

接口实现类文件

package com.moro.mapper.impl;


public class AccountMapperImpl implements AccountMapper {


    private SqlSessionTemplate session = null;

    public void setSession(SqlSessionTemplate session) {
        this.session = session;
    }

    //所有操作都使用SQLsession来执行
    public List<Account> findAll() {
        AccountMapper mapper = session.getMapper(AccountMapper.class);
        return mapper.findAll();
    }
}

测试代码

package com.moro.test;
import java.util.List;

public class Mytest {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("springconfiguration.xml");
        AccountMapper accountMapper = context.getBean("accountMapper",AccountMapper.class);
        List<Account> all = accountMapper.findAll();
        for (Account account : all) {
            System.out.println(account);
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值