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

spring-集成mybatis

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

spring-集成mybatis

spring-集成mybatis
  • 1.mybatis使用步骤
  • 2.spring和mybatis的集成
    • 2.1.新建maven项目
    • 2.2.加入maven的依赖
    • 2.3.创建实体类
    • 2.4.创建dao接口和mapper文件
    • 2.5.创建mybatis主配置文件
    • 2.6.创建Service接口和实现类,属性是dao。
    • 2.7.创建spring的配置文件:声明mybatis的对象交给spring创建
    • 2.8.创建测试类,获取Service对象,通过service调用dao完成数据库的访问

把mybatis框架和spring集成在一起,像一个框架一样使用。
用的技术是ioc。
为什么用ioc:能把mybatis和spring集成在一起,像一个框架,是因为ioc能创建对象。可以把mybatis框架的中的对象交给spring统一创建,开发人员从spring中获取对象。开发人员就不用同时面对两个或多个框架了,就面对一个spring。

1.mybatis使用步骤
  • 1)定义dao接口,StudentDao
  • 2)定义mapper文件,StudentDao.xml
  • 3)定义mybatis的主配置文件 mybatis.xml
  • 4)创建dao的代理对象
    StudentDao dao = SqlSession.getMapper(StudentDao.class);
    List students = dao.selectStudents();
    要使用dao对象,需要使用getMapper()方法,怎么能使用getMapper()方法,需要哪些条件?
    1.获取SqlSession对象,需要使用SqlSessionFactory的openSession()方法。
    2.创建SqlSessionFactory对象。通过读取mybatis的主配置文件,能创建SqlSessionFactory对象。需要SqlSessionFactory对象,使用Factory能获取SqlSession,有了SqlSession就能有dao,目的就是获取dao对象。
    Factory创建需要读取主配置文件。
    我们会使用独立的连接池类替换mybatis默认自带的,把连接池类也交给spring创建。

通过以上的说明,我们需要让spring创建以下对象
1.独立的连接池类的对象,使用阿里的druid连接池
2.SqlSessionFactory对象
3.创建出dao对象
需要学习的就是上面三个对象的创建语法,使用xml的bean标签。

2.spring和mybatis的集成 2.1.新建maven项目

创建一个名为ch07-spring-mybatis的模块项目。

2.2.加入maven的依赖
  • 1)spring依赖
 
   org.springframework
   spring-context
   5.2.5.RELEASE
 
  • 2)mybatis依赖
 
   org.mybatis
   mybatis
   3.5.1
 
  • 3)mysql驱动
   
     mysql
     mysql-connector-java
     5.1.9
   
   
   
     com.alibaba
     druid
     1.1.12
   
  • 4)spring的事物的依赖
  
    org.springframework
    spring-tx
    5.2.5.RELEASE
  
  
    org.springframework
    spring-jdbc
    5.2.5.RELEASE
  
  • 5)mybatis和spring集成的依赖:mybatis官方提供的,用来在spring项目中创建mybatis
    的SqlSessionFactory,dao对象的。
  
    org.mybatis
    mybatis-spring
    1.3.1
  
2.3.创建实体类
package com.putao.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 +
                '}';
    }
}

2.4.创建dao接口和mapper文件
package com.putao.dao;

import com.putao.domain.Student;

import java.util.List;

public interface StudentDao {

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




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

    

2.5.创建mybatis主配置文件




    
        
        
    

    

        
    
    
    
    
        
    

2.6.创建Service接口和实现类,属性是dao。
package com.putao.service;

import com.putao.domain.Student;

import java.util.List;

public interface StudentService {

    int addStudent(Student student);
    List queryStudents();
}
package com.putao.service.impl;

import com.putao.dao.StudentDao;
import com.putao.domain.Student;
import com.putao.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;
    }
}
2.7.创建spring的配置文件:声明mybatis的对象交给spring创建
  • 1)数据源DataSource
    声明数据源DataSource,作用是连接数据库的
  


       
      
      
      
  
  • 2)SqlSessionFactory
    声明的是mybatis中所提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory的
  

      
      
      
  

jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/springdb?characterEncoding=utf-8
jdbc.username=root
jdbc.passwd=root
jdbc.max=30
  • 3)Dao对象
    创建dao对象,使用SqlSession的getMapper(StudentDao.class)
    MapperScannerConfigurer:在内部调用getMapper()生成每个到接口的代理对象
 

     
 
     
 
  • 4)声明自定义的service
 
     
 
2.8.创建测试类,获取Service对象,通过service调用dao完成数据库的访问
package com.putao;

import com.putao.dao.StudentDao;
import com.putao.domain.Student;
import com.putao.service.StudentService;
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 test01(){

        String config="applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        String names[] = ctx.getBeanDefinitionNames();
        for (String na : names) {
            System.out.println("容器中对象的名称:"+na+"|"+ctx.getBean(na));
        }
    }

    @Test
    public void testDaoInsert(){

        String config="applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentDao dao = (StudentDao) ctx.getBean("studentDao");
        Student student = new Student();
        student.setId(1013);
        student.setName("周峰");
        student.setEmail("zhoufeng@qq.com");
        student.setAge(20);
        int nums = dao.insertStudent(student);
        System.out.println("nums:"+nums);
    }

    @Test
    public void testServiceInsert(){

        String config="applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentService service = (StudentService) ctx.getBean("studentService");
        Student student = new Student();
        student.setId(1015);
        student.setName("李胜利");
        student.setEmail("zhoufeng@qq.com");
        student.setAge(26);
        int nums = service.addStudent(student);
        //spring和mybatis整合在一起使用,事物是自动提交的。无需执行SqlSession.commit();
        System.out.println("nums:"+nums);
    }

    @Test
    public void testServiceSelect(){

        String config="applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentService service = (StudentService) ctx.getBean("studentService");

        List students = service.queryStudents();
        for (Student stu : students) {
            System.out.println(stu);
        }
    }
}

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

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

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