不古CeliaFaye
不古CeliaFaye
4月前 · 2 人阅读

Mybtis通过注解实现CRUD操作的教程

mybatis 通过注解实现CRUD 操作 只需要一个配置文件Configuration.xml 名字自己随意起 只是在读取的时候名字一致就可以,因为是通过注解所以不需要映射文件

1.0 环境准备 同上一篇

2.0 目录结构

这里写图片描述

3.0 编码

3.1 配置文件内容 Configuation.xml





    
        
        
            
        
            mysql.jdbc.Driver">
        
            
          
        
        
            
            
        
    

    
    
        
    

这里注意 mapper下的地址为对应类的完全限定名不是映射文件了

3.2创建student表 如下:

这里写图片描述

3.3 创建数据库模型 Student.java


package com.ccut.domain;

public class Student {
    private int id;
    private String name;
    private String sex;
    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;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

}

3.4 创建接口类 CRUD.java 这里也是用注解的地方


package com.ccut.DAO;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ccut.domain.Student;


public interface CRUD {

    @Select("select * from student where id = #{id}")
    public Student selectDATA(int id);

    @Insert("insert into student(id,name,sex) values(#{id},#{name},#{sex})")
    public void insertDATA(Student student);

    @Update("update student set name = #{name}, sex = #{sex} where id = #{id}")
    public void updataDATA(Student student);

    @Delete("delete from student where id = #{id}")
    public void deleteDATA(int id);

    @Select("select * from student")
    public List selectAll();
}

3.5 构建Mybatis工具类 Util.java 用来读取配置文件同时构建sessionfactory 返回sqlsession


package com.ccut.MybatisUtil;

import java.io.IOException;
import java.io.Reader;

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

public class Util {
    private static Reader reader;
    private static SqlSessionFactory factory;
    private static String cfg = "Configuration.xml";
    private static void setSqlSessionFactory(){
        try {
            //读取配置文件
            reader = Resources.getResourceAsReader(cfg);
            factory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("找不到文件!");
            e.printStackTrace();
        }
    }

    public static SqlSession getSession(boolean yes){
        setSqlSessionFactory();
        return factory.openSession(yes);
    }
}

这里注意 factory.openSession(yes); 中的 yes是布尔类型的 true 代表是自动提交,sql进行增删改的时候必须提交否则数据库不更新数据

3.6 由于调用接口中增删改查的时候还需要用到 session.getMapper(Student.calss);并且关闭会话,所以我们在业务层将上述操作封装,在有需要的时候直接调用即可 封装实现类CRUDimp.java


package com.ccut.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.ccut.DAO.CRUD;
import com.ccut.MybatisUtil.Util;
import com.ccut.domain.Student;

public class CRUDimp {
    public void insert(Student student){
        SqlSession session = Util.getSession(true);
        CRUD crud = session.getMapper(CRUD.class);
        crud.insertDATA(student);
        session.close();
    }

    public void delete(int id){
        SqlSession session = Util.getSession(true);
        CRUD crud = session.getMapper(CRUD.class);
        crud.deleteDATA(id);
        session.close();
    }

    public void update(Student student){
        SqlSession session = Util.getSession(true);
        CRUD crud = session.getMapper(CRUD.class);
        crud.updataDATA(student);
        session.close();
    }
     public void selectOne(int id){
            SqlSession session = Util.getSession(true);
            CRUD crud = session.getMapper(CRUD.class);
            Student student = crud.selectDATA(id);
            session.close();        
            System.out.println("id = "+student.getId()+" name = "+student.getName()+" sex = "+student.getSex());

     }

     public void selectAll(){
         SqlSession session = Util.getSession(true);
            CRUD crud = session.getMapper(CRUD.class);
            List list = crud.selectAll();
            session.close();        
            for(Student student:list){
                System.out.println("id = "+student.getId()+" name = "+student.getName()+" sex = "+student.getSex());
            }
     }


}

注意这里并没有实现接口CRUD 只是对其进行了二次封装 方便直接调用

3.7 编写测试类 Test.java


package test;

import com.ccut.domain.Student;
import com.ccut.service.CRUDimp;

public class Test {
    public static void main(String[] args) {
        CRUDimp cruDimp = new CRUDimp();
        Student student = new Student();
//      student.setId(1);
//      student.setName("徐艳当");
//      student.setSex("女");
        student.setId(2);
        student.setName("徐艳东");
        student.setSex("女");
    //  cruDimp.insert(student);
    //  cruDimp.update(student);
        cruDimp.selectOne(1);
        System.out.println("----------------------------------");
        cruDimp.selectAll();    
        cruDimp.delete(1);
        System.out.println("删除后");
        cruDimp.selectAll();    


    }
}

经测试 测试均通过 结果如下:

插入:

这里写图片描述

接着插入:

这里写图片描述

修改:

这里写图片描述

单个查询,全部查询 和 删除后全部查询:

这里写图片描述

至此OK 其他内容以后再说!

收藏 0
student public id session impo
评论 ( 0 )