Mybatis和Spring的整合

Mybatis:

作用:连接数据库,获取数据库中的信息

步骤:

0、引入依赖,生成全局配置文件

1、创建一个Student的pojo类

2、创建方法接口StudentDao

3、创建和接口对应的mapper.xml文件

4、在资源文件夹中的全局配置文件中配置映射

5、测试运行

目录结构:

 

Spring: 

作用:管理对象,实现低耦合

步骤:

0、引入依赖,生成全局配置文件

1、创建一个基本类

2、创建一个配置文件管理需要的对象

3、测试

目录结构:

 

Mybatis和Spring 的整合主要就是将Mybatis中的会话的一系列对象使用容器进行管理,从而取消Mybatis中的全局配置文件,降低耦合。

整合步骤:

1、引入依赖 将Mybatis和Spring的依赖都引入进去,还用到了连接池,还有整合时需要的依赖。

 <dependencies>


        <!-- c3p0的依赖 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <!--Mybatis和Spring整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.propety}</version>
        </dependency>


        <!--Spring的AOP jar包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.propety}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>


        <!--spring核心依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.propety}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.propety}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.propety}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.propety}</version>
        </dependency>


        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--Mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.che</groupId>
            <artifactId>MybatisDemo</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>


    </dependencies>

2、Student类

package com.che.pojo;

public class Student {
    private int id;
    private String name;
    private int age;
    private String sex;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

3、创建获取学生信息的方法接口 StudentMapper

import com.che.pojo.Student;

public interface StudentMapper {
    public Student selectStudentById(int id);
}

4、创建对应的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="com.che.Mapper.StudentMapper">

    <!--
    id:statementID,在同一个命名空间下具有唯一性,一般是对应接口中方法
    parameterType:表示参数类型
    resultType:指定返回类型

    #{XXX}:表示占位符  ?
    -->
    <select id="selectStudentById" parameterType="Integer" resultType="com.che.pojo.Student">
        select * from student where id = #{id}
    </select>


</mapper>

 5、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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/che"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--获取会话工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--映射关系-->
        <property  name="mapperLocations" value="classpath:Mapper/*.xml"/>
    </bean>

   

    </bean>
</beans>

6、测试

import com.che.Mapper.StudentMapper;
import com.che.pojo.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SMTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        SqlSessionFactory  sqlSessionFactory = (SqlSessionFactory)context.getBean("sqlSessionFactory");
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectStudentById(1);
        System.out.println(student);
    }
}

 7、运行结果

 给出目录结构:

还可以直接用容器管理studentMapper

在spring.xml中添加:

 <!--通过代理对象获取SQLSession对象-->
    <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--注入会话工厂-->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <!--注入接口-->
        <property name="mapperInterface" value="com.che.Mapper.StudentMapper"/>

 就可以在测试类中使用是直接获取studentMapper对象


import com.che.Mapper.StudentMapper;
import com.che.pojo.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SMTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//        SqlSessionFactory  sqlSessionFactory = (SqlSessionFactory)context.getBean("sqlSessionFactory");
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        StudentMapper.xml mapper = sqlSession.getMapper(StudentMapper.xml.class);
//        Student student = mapper.selectStudentById(1);
        StudentMapper studentMapper = (StudentMapper)context.getBean("studentMapper");
        Student student = studentMapper.selectStudentById(1);
        System.out.println(student);
    }
}

同样可以拿到结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值