栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring框架入门(四)---Spring框架和MyBatis的整合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring框架入门(四)---Spring框架和MyBatis的整合

目录

第四章、Spring集成MyBatis

4.1 介绍

4.2 整合步骤

4.3 数据库准备

4.4 maven依赖pom.xml文件

4.5 定义实体类

4.6 定义Dao接口及相应的mapper映射文件

4.7 定义Service接口和实现类

4.8 定义MyBatis主配置文件

4.9 Spring配置文件

4.10 测试类代码


第四章、Spring集成MyBatis

4.1 介绍

   利用ioc技术将spring和mybatis整合在一起。

   为什么ioc能把spring和mybatis集成在一起,像一个框架一样使用?

        因为ioc可以创建对象,可以把mybatis框架中的对象交给spring统一创建,开发人员从spring

      中获取对象。这样开发人员就不用同时面对两个或多个框架了,就面对一个spring。

   mybatis使用步骤:

        1.定义dao接口,StudentDao

        2.定义mapper文件StudentDao.xml

        3.定义mybatis的主配置文件mybatis.xml

        4.创建dao的代理对象,StudentDao dao = SqlSession.getMapper(StudentDao.class);

   要使用dao对象,需要使用getMapper()方法,怎么能使用getMapper()方法,需要哪些条件?

        1.获取SqlSession对象, 需要使用SqlSessionFactory的openSession()方法。

        2.创建SqlSessionFactory对象。 通过读取mybatis的主配置文件,能创建SqlSessionFactory

          对象。

        3.需要SqlSessionFactory对象,使用Factory能获取SqlSession,有了SqlSession就能有

          dao,目的就是获取dao对象。Factory创建需要读取主配置文件。

   需要让spring创建以下对象:

        1.独立的连接池类的对象, 使用阿里的druid连接池

        2.SqlSessionFactory对象

        3.创建出dao对象

4.2 整合步骤

   1.新建maven项目

   2.在maven的pom.xml文件中加入依赖:spring依赖、mybatis依赖、mysql依赖、spring的事务的依赖、mybatis和spring集成的依赖(mybatis官方提供的,用来在spring项目中创建mybatis的SqlSessionFactory、Dao对象的)

   3.创建实体类

   4.创建dao接口和对应的mapper文件

   5.创建mybatis主配置文件

   6.创建Service接口及其实现类,属性是对应的dao对象

   7.创建spring的配置文件:声明mybatis的对象交给spring创建(数据源Datasource、SqlSessionFactory、Dao对象、Service对象)

   8.创建测试类,获取service对象,通过service对象调用dao完成对数据库的访问操作

4.3 数据库准备

   创建了数据库springdb,新建表Student。

4.4 maven依赖pom.xml文件

    
    
        junit
        junit
        4.11
        test
    
    
    
        org.springframework
        spring-context
        5.2.5.RELEASE
    
    
    
        org.springframework
        spring-tx
        5.2.5.RELEASE
    
    
    
        org.springframework
        spring-jdbc
        5.2.5.RELEASE
    
    
    
        org.mybatis
        mybatis
        3.5.1
    
    
    
        org.mybatis
        mybatis-spring
        1.3.1
    
    
    
        mysql
        mysql-connector-java
        5.1.9
    
    
    
        com.alibaba
        druid
        1.1.12
    

4.5 定义实体类
package com.bjpowernode.domain;

public class Student {
    //属性名和列名一样
    private Integer id;
    private String name;
    private String email;
    private Integer age;

    public Student() {
    }

    public Student(Integer id, String name, String email, Integer age) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

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

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

4.6 定义Dao接口及相应的mapper映射文件
package com.bjpowernode.dao;

import com.bjpowernode.domain.Student;

import java.util.List;

public interface StudentDao {

    int insertStudent(Student student);
    List selectStudents();
}



    
         insert into student values (#{id},#{name},#{email},#{age})
    
    
    

4.7 定义Service接口和实现类
package com.bjpowernode.service;

import com.bjpowernode.domain.Student;

import java.util.List;

public interface StudentService {
    int addStudent(Student student);
    List queryStudents();
}
package com.bjpowernode.service.impl;

import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;

import java.util.List;

public class StudentServiceImpl implements StudentService {

    //引用类型
    private StudentDao studentDao;

    //使用set注入赋值
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public int addStudent(Student student) {
        int nums = studentDao.insertStudent(student);
        return nums;
    }

    @Override
    public List queryStudents() {
        List students = studentDao.selectStudents();
        return students;
    }
}

4.8 定义MyBatis主配置文件

   这里有两点需要注意:

        ①主配置文件中不再需要数据源的配置了。因为数据源要交给Spring容器来管理了。

        ②这里对mapper映射文件的注册,使用标签,即只需给出mapper映射文件所在

          的包即可。因为mapper的名称与Dao接口名相同,可以使用这种简单注册方式。这种方式的

          好处是,若有多个映射文件,这里的配置也是不用改变的。当然,也可使用原来的

          标签方式。





    
        
        
    

    
    
        
        
    

    
    
        
        
    

4.9 Spring配置文件
jdbc.properties文件:
jdbc.url=jdbc:mysql://localhost:3306/springdb
jdbc.username=root
jdbc.passwd=123456
jdbc.max=20



    
    

    
    
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
    

4.10 测试类代码
public class MyTest {
    @Test
    public void test01(){
        String config = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        String[] names = ac.getBeanDefinitionNames();
        for(String name : names){
            System.out.println("容器中对象的名称:" + name + "|" + ac.getBean(name));
        }
    }

    @Test
    public void testDaoInsert(){
        String config = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentDao dao = (StudentDao) ac.getBean("studentDao");
        Student student = new Student(1009,"科比","kobe@163.com",42);
        int nums = dao.insertStudent(student);
        System.out.println("nuns = " + nums);
    }

    @Test
    public void testServiceInsert(){
        String config = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的service对象
        StudentService studentService = (StudentService) ac.getBean("studentService");
        Student student = new Student(1010,"xxy","xxy@163.com",24);
        int nums = studentService.addStudent(student);
        //spring和mybatis整合在一起使用,事务是自动提交的,无需执行SqlSession.commit();
        System.out.println("nuns = " + nums);
    }

    @Test
    public void testServiceSelect(){
        String config = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的service对象
        StudentService studentService = (StudentService) ac.getBean("studentService");
        List students = studentService.queryStudents();
        for (Student student : students){
            System.out.println("student = " + student);
        }
    }
}

PS:根据动力节点课程整理,如有侵权,联系删除。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396817.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号