spring整合mybatis

spring整合mybatis

    • 1.mybatis实现数据库中学生信息的查询:
    • 运行结果:
    • 代码整体布局:
    • 代码如下:
      • 数据库/表:
      • pom.xml:
      • Student:
      • StudentMapper:
      • StudentMapper.xml:
      • db.properties:
      • mybatis-config.xml:
      • Test:
    • 2.按照之前的结构,业务层搞起来。。。
    • 运行结果如下:
    • 代码整体结构:
    • 添加/修改的代码如下:
      • IStudentService:
      • IStudentService:
      • Test2:
    • 3.spring整合mybatis实现数据库中学生信息的查询:(待补充:增删改查)

1.mybatis实现数据库中学生信息的查询:

运行结果:

spring整合mybatis_第1张图片

代码整体布局:

spring整合mybatis_第2张图片

代码如下:

数据库/表:

create table tb_student
(
    student_id int primary key auto_increment,
    name varchar(20),
    sex char(1),
    birthday date
);

select * from tb_student;

insert into tb_student
(name,sex,birthday)
select '张三','男','2001-01-01' UNION
select '李四','女','2002-01-02' union
select '王五','男','2002-01-01' union
select '赵六','男','2003-01-03' union
select '孙琪','男','2002-01-01' union
select '高达','女','2005-01-01' union
select '郭靖','男','2002-11-01' union
select '黄蓉','女','2002-09-01' union
select '大武','男','2002-01-05';

spring整合mybatis_第3张图片

pom.xml:



    4.0.0

    org.example
    mybatis_0822_KTLX2
    1.0-SNAPSHOT

    
        8
        8
        5.3.14
        1.4
        3.4.6
        1.3.3
        8.0.11
    
    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            repository.org.springframework
            spring-jdbc
            ${spring.version}
        
        
        
            commons-dbcp
            commons-dbcp
            ${commons-dbcp.version}
        
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
        
            mysql
            mysql-connector-java
            ${mysql-connector-java.version}
        
        
        
            org.mybatis
            mybatis-spring
            ${mybatis-spring.version}
        
        
        
            com.github.pagehelper
            pagehelper
            5.1.4
        
        
        
            com.github.abel533
            mapper
            3.0.1
        
    



Student:

package com.entity;

import java.util.Date;

public class Student {
    private String studentId;
    private String name;
    private String sex;
    private Date birthday;

    public Student() {
    }

    public Student(String studentId, String name, String sex, Date birthday) {
        this.studentId = studentId;
        this.name = name;
        this.sex = sex;
        this.birthday = birthday;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentId='" + studentId + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}

StudentMapper:

package com.mapper;

import com.entity.Student;

import java.util.List;

public interface StudentMapper {
    List listAll();
}

StudentMapper.xml:






    



db.properties:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/70816_db?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=123456

mybatis-config.xml:




    
    
        
        
    
    
        
    

    
        
            
            
                
                
                
                
            
        
    
    
        
    



Test:

package com.test;

import com.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class Test {
    public static void main(String[] args) throws IOException {
        InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session=factory.openSession();
        StudentMapper studentMapper=session.getMapper(StudentMapper.class);
        System.out.println(studentMapper.listAll());

        session.commit();
        session.close();
    }
}

2.按照之前的结构,业务层搞起来。。。

参考:第三章 DAO模式《分层》 ② 代码https://blog.csdn.net/Liu_wen_wen/article/details/125967616
spring整合mybatis_第4张图片

运行结果如下:

spring整合mybatis_第5张图片

代码整体结构:

spring整合mybatis_第6张图片

添加/修改的代码如下:

spring整合mybatis_第7张图片

IStudentService:

package com.service;

import com.entity.Student;

import java.util.List;

public interface IStudentService {
    List listAll();
}

IStudentService:

package com.service.impl;

import com.entity.Student;
import com.mapper.StudentMapper;
import com.service.IStudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

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

public class StudentServiceImpl implements IStudentService {

    @Override
    public List listAll() {
        InputStream inputStream=null;
        try {
            inputStream= Resources.getResourceAsStream("mybatis-config.xml"); //ctrl+alt+t try+catch
            SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session=factory.openSession();
            StudentMapper studentMapper=session.getMapper(StudentMapper.class);
            //System.out.println(studentMapper.listAll());
            List studentList=studentMapper.listAll();

            session.commit();
            session.close();

            return studentList;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Test2:

package com.test;

import com.entity.Student;
import com.service.IStudentService;
import com.service.impl.StudentServiceImpl;

import java.util.List;

public class Test2 {
    public static void main(String[] args) {
        IStudentService iStudentService=new StudentServiceImpl();
        List studentList=iStudentService.listAll();
        System.out.println(studentList);
    }
}

3.spring整合mybatis实现数据库中学生信息的查询:(待补充:增删改查)

// A code block
var foo = 'bar';

// A code block
var foo = 'bar';

// A code block
var foo = 'bar';

// A code block
var foo = 'bar';

// A code block
var foo = 'bar';

// A code block
var foo = 'bar';

你可能感兴趣的:(#,Spring,mybatis,spring,java)