Spring与MyBatis整合(哇,快来看,他写的真好,简单易懂哎)

1.思路

  把MyBatis框架使用中所涉及到的核心组件配置到Spring容器中,交给Spring来创建和管理

2.准备工作

  1.在项目中加入Spring/MyBatis及整合相关的JAR文件

  2.建立开发目录结构,创建实体类

  3.创建数据访问接口

  4.配置映射文件

  5.配置MyBatis配置文件

3.案例

  步骤一:依赖(根据自身需要添加其他依赖)

<!--MyBatis和Spring的整合包 由MyBatis提供-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.0</version>
</dependency>
<!--MyBatis的核心jar文件-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.1</version>
</dependency>

  步骤二:实体类

public class Student {
    private Integer stuid;
    private String stuname;
    private Integer age;

    public Integer getStuid() {
        return stuid;
    }

    public void setStuid(Integer stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public Integer getAge() {
        return age;
    }

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

  步骤三:dao

public interface IStudentdao {
     List<Student> allStu();
}

  步骤四:相对应的.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">
<!--小配置根节点 namespace代表命名空间-->
<mapper namespace="com.SpringandMyBatis.dao.IStudentdao">
    <select id="allStu" resultType="Student">
            select * from Student
</select>
</mapper>

  步骤五:Service

public interface IStudentService {
    public List<Student> allStu();
}

  步骤六:Serviceimpl

public class IStudentServiceimpl implements IStudentService {

    private IStudentdao iStudentdao;

    public IStudentdao getiStudentdao() {
        return iStudentdao;
    }

    public void setiStudentdao(IStudentdao iStudentdao) {
        this.iStudentdao = iStudentdao;
    }

    @Override
    public List<Student> allStu() {
        return iStudentdao.allStu();
    }
}

  步骤七:jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student?useUniCode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=123

  步骤八:mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- xml文件的头文件,起到对文件的约束作用(例如:必须存在哪些节点) -->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--配置别名-->
    <typeAliases>
        <package name="com.SpringandMyBatis.entity"/>
    </typeAliases>

    <!--管理我们配置的映射文件-->
    <mappers>
        <package name="com.SpringandMyBatis.dao"/>
    </mappers>

</configuration>

  步骤九:applicationContext.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"
       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">

    <!--1.加载配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>



    <!--2.配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <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>


    <!--3配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--加载MyBatis文件-->
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>


    <!--4扫描Dao,节省代码-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.SpringandMyBatis.dao"/>
    </bean>


    <!--5.注入dao层-->
    <bean id="iStudentdao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.SpringandMyBatis.dao.IStudentdao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    </bean>


    <!--6.Service层-->
    <bean id="iStudentServiceimpl" class="com.SpringandMyBatis.Service.impl.IStudentServiceimpl">
        <property name="iStudentdao" ref="iStudentdao"/>
    </bean>
</beans>

  步骤十:测试

public class Studenttest {
    @Test
    public void stutest(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        IStudentService iStudentService = (IStudentService)ctx.getBean("iStudentServiceimpl");

        List<Student> strings = iStudentService.allStu();
        for(Student stu:strings){
            System.out.println(stu.getStuname());
        }

    }
}

 

 

 

4.使用注释方式实现

  步骤一:applicationContext.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"
       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:component-scan base-package="com.SpringandMyBatis"/>



    <!--1.加载配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>



    <!--2.配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <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>


    <!--3配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--加载MyBatis文件-->
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>


    <!--4扫描Dao,节省代码-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.SpringandMyBatis.dao"/>
    </bean>

</beans>

  步骤二:dao

@Repository
public interface IStudentdao {
     List<Student> allStu();
}

  步骤三:Serviceimpl

@Service("iStudentService")
public class IStudentServiceimpl implements IStudentService {
    @Resource
    private IStudentdao iStudentdao;

    public IStudentdao getiStudentdao() {
        return iStudentdao;
    }

    public void setiStudentdao(IStudentdao iStudentdao) {
        this.iStudentdao = iStudentdao;
    }

    @Override
    public List<Student> allStu() {
        return iStudentdao.allStu();
    }
}

  步骤四:测试

@Test
    public void stutest(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        IStudentService iStudentService = (IStudentService)ctx.getBean("iStudentService");

        List<Student> strings = iStudentService.allStu();
        for(Student stu:strings){
            System.out.println(stu.getStuname());
        }

    }

 

 

 

 

 

posted @ 2019-11-05 17:09  梅川酷子灬  阅读(246)  评论(0编辑  收藏  举报