zoukankan      html  css  js  c++  java
  • 使用Spring整合Mybatis

    1. 导入maven依赖

        <!--依赖-->
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.2</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.1.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.4</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.2</version>
            </dependency>
        </dependencies>
        <!--配置忽略文件-->
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>

    2. 创建spring配置文件

    <?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">

    3. 配置数据源

        <!--配置数据源-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
        </bean>

    4. 使用spring创建SqlSessionFactory对象

        <!--配置sqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!--绑定Mybatis配置文件-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:com/yd/mapper/*.xml"/>
        </bean>

    5. 利用spring使用SqlSessionTemplate创建sqlSession对象

        <!--使用sqlSessionTemplate创建sqlSession的bean-->
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>

    6. 编写接口实现类

    package com.yd.mapper;
    
    import com.yd.pojo.User;
    import org.apache.ibatis.session.SqlSession;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.util.List;
    
    public class UserMapperImpl implements UserMapper {
        private SqlSession sqlSession;
    
        public void setSqlSession(SqlSession sqlSession) {
            this.sqlSession = sqlSession;
        }
    
        public List<User> selectUser() {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }

    7. 使用Spring创建userMapper对象

        <bean id="userMapper" class="com.yd.mapper.UserMapperImpl">
            <property name="sqlSession" ref="sqlSession"/>
        </bean>

    8. 测试

    import com.yd.mapper.UserMapper;
    import com.yd.mapper.UserMapperImpl;
    import com.yd.pojo.User;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.util.List;
    
    public class MyTest {
        @Test
        public void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-mapper.xml");
            UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
            List<User> userList = userMapper.selectUser();
            for (User user : userList) {
                System.out.println(user);
            }
        }
    }
  • 相关阅读:
    【JavaP6大纲】SpringCould篇:什么是微服务
    【JavaP6大纲】SpringCould篇: Spring Boot 和 Spring Cloud,谈谈你对它们的理解?
    【JavaP6大纲】分布式会话篇:集群部署时的分布式 Session 如何实现?
    【JavaP6大纲】SpringCould篇:Spring Cloud 和 Dubbo 有哪些区别?
    【JavaP6大纲】SpringCould篇:服务发现组件 Eureka 的主要调用过程?Eureka 和 Zookeeper 都可以提供服务注册与发现的功能,它们有什么区别?
    【JavaP6大纲】SpringCould篇:熔断框架如何做技术选型?选用 Sentinel 还是 Hystrix?
    【JavaP6大纲】SpringCould篇:如何限流?在工作中是怎么做的?说一下具体的实现?
    【JavaP6大纲】SpringCould篇:常用组件底层实现
    【JavaP6大纲】MySQL篇:SQL的整个解析、执行过程原理、SQL行转列?
    如何将百度搜索嵌入到站点中!
  • 原文地址:https://www.cnblogs.com/chao666/p/12879121.html
Copyright © 2011-2022 走看看