1. mybatis环境搭建与增删改查

开发环境

  1. IDE: Eclipse

  2. 数据库: Mysql 5.7

  3. 数据库连接工具:DBeaver

步骤

  1. 新建maven工程,命名为learnMybaits

  2. 在pom.xml中引入mybatis、 mysql驱动和Junit的依赖

    pom.xml

    
        4.0.0
        com.auuid
        mybatis
        0.0.1-SNAPSHOT
    
        
            
            
                org.mybatis
                mybatis
                3.4.6
            
            
            
                mysql
                mysql-connector-java
                8.0.11
            
            
            
                junit
                junit
                4.11
                test
            
        
    
    
  3. 新建一个Employee类,新建一张employee表

    Employee类

    package com.auuid.bean;
    
    public class Employee {
    
      private Integer id;
      private String name;
      private Integer 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 Integer getAge() {
        return age;
      }
      public void setAge(Integer age) {
        this.age = age;
      }
    }
    
    

    employee表

    CREATE TABLE employee (
      id INT PRIMARY KEY AUTO_INCREMENT,
      name VARCHAR(255),
      age SMALLINT
    )
    
  4. 新建EmployeeMapper接口

    package com.auuid.dao;
    
    import com.auuid.bean.Employee;
    
    public interface EmployeeMapper {
      public Employee getById(Integer id);
      public void save(Employee employee);
      public void update(Employee employee);
      public void deleteById(Integer id);
    }
    
  5. 新建配置文件EmployeeMapper.xml和mybatis-config.xml

    EmployeeMapper.xml

    
    
    
    
    
       
       
       
       
           insert into employee(name, age) values(#{name}, #{age})
       
       
           update employee set name=#{name}, age=#{age} where id=#{id}
       
       
           delete from employee where id=#{id}
       
    
    

    mybatis-config.xml

    
    
    
       
           
               
               
                   
                   
                   
                   
               
           
       
       
           
       
    
    
  6. 新建测试类EmployeeTestCase

    package com.auuid;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Random;
    import java.util.UUID;
    
    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 org.junit.Test;
    
    import com.auuid.bean.Employee;
    import com.auuid.dao.EmployeeMapper;
    
    public class EmployeeTestCase {
      
      @Test
      public void save() throws IOException {
        SqlSession openSession = null;
        
        try {
          openSession = openSession();
          EmployeeMapper employeeMapper = openSession.getMapper(EmployeeMapper.class); //生产接口代理对象
          employeeMapper.save(newEmployee());
          openSession.commit();
        } finally {
          openSession.close();
        }
      }
      
      private SqlSession openSession() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); //读取数据源
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //构建sessionFactory
        SqlSession openSession = sqlSessionFactory.openSession(); //构建session
        return openSession;
      }
      
      private Employee newEmployee() {
        Employee employee = new Employee();
        employee.setName(UUID.randomUUID().toString());
        employee.setAge(new Random().nextInt(100));
        return employee;
      }
    }
    

你可能感兴趣的:(1. mybatis环境搭建与增删改查)