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

Spring和mybatis整合笔记

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

Spring和mybatis整合笔记

Spring-mybatis整合

注:本次笔记是在b站上观看的动力节点王妈妈的视频后自己总结的笔记,如果哪里有错误,还请各位大佬指出,谢谢,互相学习呀

1.目录结构

2.pom文件

这里是pom文件中的所需依赖和插件


        UTF-8
        1.8
        1.8
    

    
        
        
            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
        
    

    
        
        
            
                src/main/java
                
                    ***.xml
                
                false
            
        

        
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.10.1
                
                    1.8
                    1.8
                
            
        
    
3.编写dao

定义接口,实现插入数据库数据和查询数据库两个方法

package com.bjpowernode.dao;

import com.bjpowernode.domain.Student;

import java.util.List;

public interface StudentDao {
    //插入数据的方法(这里我们插入的是一个学生对象)
    int insertStudent(Student student);
    //查询数据库里面的学生信息
    List selectStudents();
}
4.编写 Dao 接口 Mapper 映射文件 StudentDao.xml

Mybatis框架抛开了Dao的实现类,直接映射到xml文件中的SQL语句,直接对数据库进行操作,这种对Dao的实现方式称为Mapper的动态代理方式




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

5.编写domain中的实体类Student

这个Student实体类其实就是一个JavaBean,这里建议编写实体类里面的属性名时最好和数据库中字段名保持一致

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

}
6.定义Service层和接口StudentService

service层是做具体的业务的

package com.bjpowernode.service;

import com.bjpowernode.domain.Student;

import java.util.List;

public interface StudentService {

    int addStudent(Student student);
    List queryStudents();
}
7.定义接口的实现类StudentServiceImpl

因为我们把对象都交给spring了,所以这里引入的studentDao对象要有set方法,使用set注入,交给Spring的ioc容器管理

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;
    }
}
8.Mybatis.xml主配置文件

之前我们在写Mybatis.xml配置文件时,需要写dataSource(数据源),在这里不需要了,因为交给spring管理了,数据源写在applicationContext.xml(spring的主配置文件)里面就可以了




    
    
        
    

    
    
        
        
    

9.编写spring的主配置文件applicationContext.xml

这个文件需要指明properties的位置,DataSource数据源,创建sqlSessionFactory和MapperScannerConfigurer对象

说明:

​ 1.properties:文件里面是关于连接数据库的信息,如url,username,password等,可以在spring主配置文件中利用${}来获取数据库连接信 息,更加灵活

​ 2.DataSource数据源:声明数据源,这里使用阿里巴巴的druid,这个数据库连接池可以不需要数据库连接的驱动,提供强大的监控和扩展功 能

​ 3.sqlSessionFactory:这里获取sqlSessionFactory是因为要利用它里面的openSession方法获取sqlSession

​ 4.MapperScannerConfigurer:使用SqlSession的getMapper(StudentDao.class) ,在内部调用getMapper()生成每个dao接口的代理对 象.




    
    
    
    
    
        
        
        
        
        
        
    
    
    
    
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
    

10.编写jdbc.properties

​ 这样写的原因是解耦合,更加灵活,便于以后的维护

jdbc.url=jdbc:mysql://localhost:3306/数据库名称
jdbc.username=root
jdbc.password=密码
jdbc.maxActive=最大连接数
11.编写测试文件
package com.bjpowernode;

import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.service.StudentService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class MyTest {
	
    //获取ioc容器里面所有的对象
    @Test
    public void test01(){
        String config = "applicationContext.xml";
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        String[] names = ctx.getBeanDefinitionNames();
        for (String na:names){
            System.out.println("容器中对象名称:"+na+"|"+ctx.getBean(na));
        }
    }
    //利用dao插入数据
    @Test
    public void testDaoInsert(){
        String config = "applicationContext.xml";
        ClassPathXmlApplicationContext 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);

    }
	
    //利用service插入数据
    @Test
    public void testServiceInsert(){
        String config = "applicationContext.xml";
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentService service = (StudentService) ctx.getBean("studentService");
        Student student = new Student();
        student.setId(1014);
        student.setName("小明");
        student.setEmail("xiaoming@qq.com");
        student.setAge(20);
        int nums = service.addStudent(student);
        System.out.println("nums="+nums);
    }
	
    //利用service查询数据
    @Test
    public void testServiceSelect(){
        String config = "applicationContext.xml";
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        //获取spring容器中的dao对象
        StudentService service = (StudentService) ctx.getBean("studentService");
        List students = service.queryStudents();
        for (Student stu:students){
            System.out.println("stu=="+stu);
        }

    }
}


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

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

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