Spring整合第三方框架-MyBatis原始操作代码

建议自己写一下

实体类,用于封装数据库数据

package com.example.pojo;

import java.util.Date;

public class Emp {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Integer gender;
    private String image;
    private Integer job;
    private Date entrydate;
    private Date create_time;
    private Date update_time;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Integer getJob() {
        return job;
    }

    public void setJob(Integer job) {
        this.job = job;
    }

    public Date getEntrydate() {
        return entrydate;
    }

    public void setEntrydate(Date entrydate) {
        this.entrydate = entrydate;
    }

    public Date getCreate_time() {
        return create_time;
    }

    public void setCreate_time(Date create_time) {
        this.create_time = create_time;
    }

    public Date getUpdate_time() {
        return update_time;
    }

    public void setUpdate_time(Date update_time) {
        this.update_time = update_time;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", gender=" + gender +
                ", image='" + image + '\'' +
                ", job=" + job +
                ", entrydate=" + entrydate +
                ", create_time=" + create_time +
                ", update_time=" + update_time +
                '}';
    }
}

Mapper接口类,定义对于数据库的操作

package com.example.Mapper;

import com.example.pojo.Emp;

import java.util.List;

public interface EmpMapper {
    List findAll();
}

Mapper接口类对应的xml配置文件,用于实行Mapper接口类中对于数据库的操作




    

MyBatis配置文件,定义和配置MyBatis框架的各种参数和属性,以及映射文件的路径和数据库连接信息。




    
        
            
            
                
                
                
                
            
        
    
    
    
        
    

测试类代码,实现MyBatis功能

package com.example.Test;

import com.example.Mapper.EmpMapper;
import com.example.pojo.Emp;
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.InputStream;
import java.util.List;

public class MyBatisTest {
    public static void main(String[] args) throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        List all = mapper.findAll();
        for (Emp emp : all) {
            System.out.println(emp);
        }


    }
}

运行结果如下

Spring整合第三方框架-MyBatis原始操作代码_第1张图片

你可能感兴趣的:(Spring,5,spring,mybatis,oracle)