您当前的位置: 首页 >  Java

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java学习路线-60:spring 整合 mybatis

彭世瑜 发布时间:2020-06-22 09:10:27 ,浏览量:3

整合示例

1、依赖

pom.xml




    org.mybatis
    mybatis
    3.5.4




    org.mybatis
    mybatis-spring
    2.0.4




    org.springframework
    spring-jdbc
    5.2.6.RELEASE

2、配置

(1)beans.xml





    
    
        
        
        
        
    

    
    
        
        
    

    
        
    

    
        
    


(2)mybatis-config.xml






    
        
        
    

    
        
    

    
        
    


(3)StudentMapper.xml





    
        select * from students
    

3、实体对象类

package com.pengshiyu.mybatis.entity;

public class Student {
    private int id;
    private String name;
    private Teacher teacher;

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    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;
    }

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

4、dao

(1)定义接口

package com.pengshiyu.mybatis.dao;

import com.pengshiyu.mybatis.entity.Student;

import java.util.List;

public interface StudentDao {

    public List selectAllStudent();

}

(2)实现接口

package com.pengshiyu.mybatis.dao.impl;

import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private SqlSessionTemplate sqlSession;

    @Override
    public List selectAllStudent() {
        return sqlSession.selectList(
                "com.pengshiyu.mybatis.entity.StudentMapper.selectAllStudent");
    }

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
}

5、测试

package com.pengshiyu.mybatis.test;

import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        StudentDao studentDao = (StudentDao)context.getBean("StudentDao");
        List students = studentDao.selectAllStudent();

        for(Student student: students){
            System.out.println(student);
        }
    }
}

实现类继承 SqlSessionDaoSupport

实现类

package com.pengshiyu.mybatis.dao.impl;

import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import org.mybatis.spring.support.SqlSessionDaoSupport;

public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao {
    @Override
    public Student getById(int id) {
        return getSqlSession().selectOne("com.pengshiyu.mybatis.entity.StudentMapper.getById", id);
    }
}

配置修改





    
    
        
        
        
        
    

    
    
        
        
    

    
    
    
    

    
        
        
        
    



注解方式

1、DAOMapper

package com.pengshiyu.mybatis.dao;

import com.pengshiyu.mybatis.entity.Student;
import org.apache.ibatis.annotations.Select;

public interface StudentMapper {
    @Select("select * from students where id = #{id}")
    public Student getById(int id);

}

2、Service

(1)接口

package com.pengshiyu.mybatis.service;

import com.pengshiyu.mybatis.entity.Student;

public interface StudentService {
    public Student getById(int id);
}

(2)实现

package com.pengshiyu.mybatis.service.impl;

import com.pengshiyu.mybatis.dao.StudentMapper;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.service.StudentService;

public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper;

    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    public Student getById(int id) {
        return studentMapper.getById(id);
    }
}

3、配置 beans.xml





    
    
        
        
        
        
    

    
    
        
        
    

    
        
        
    

    
        
    



4、测试

package com.pengshiyu.mybatis.test;

import com.pengshiyu.mybatis.dao.StudentMapper;
import com.pengshiyu.mybatis.entity.Student;
import com.pengshiyu.mybatis.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Demo {
    public static void main(String[] args) throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        StudentService studentService = (StudentService) context.getBean("studentService");

        Student student = studentService.getById(1);
        System.out.println(student);
    }
}

指定配置文件目录

beans.xml



    

    
    
    


关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0913s