mybatis 学习记录 (一)

一、 Mybatis HelloWorld

1. 使用Maven创建一个项目,项目目录如下

mybatis 学习记录 (一)_第1张图片

2. 在maven 的pom.xml引入依赖

主要包括mybatis,mysql,junit的依赖,然后就可进行开发了。



    4.0.0

    org.example
    test03
    1.0-SNAPSHOT

    
        8
        8
    
    
        
            org.mybatis
            mybatis
            3.5.7
        
        
            mysql
            mysql-connector-java
            8.0.22
        
        
            junit
            junit
            4.13.2
            test
        
    

3. 编写mybatis配置文件 mybatis-config.xml文件

主要是用来配置数据源 和 配置相关mapper*.xml文件




    
        
            
            
                
                
                
                
            
        
    

    
        
    

4.创建用户表,编写User类

mybatis 学习记录 (一)_第2张图片

package com.jt;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

5.编写UserMapper类

package com.jt;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

public interface  UserMapper {
   public User selectUser(int id);
   public User  selectUserByIdAndName(@Param("id") Integer id,@Param("username") String username);
   public User  selectUserByIdAndNameUseMap(Map map);

   public Integer insertUser(User user);
   public boolean updateUser(User user);
   public Integer deleteUserById(int id);
}

6.编写UserMapper.xml文件




    
    
    
    
        insert into test01 (username,password) values(#{username},#{password})
    
    
        update test01 set username=#{username} where id=#{id}
    
    
        delete from test01 where id=#{id}
    

7.编写测试类

import com.jt.User;
import com.jt.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

public class HelloTest {
    @Test
    public void selectTest() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserMapper userMapper = session.getMapper(UserMapper.class);
            User user = userMapper.selectUser(1);
            System.out.println(user);
        }finally {
            sqlSessionFactory.openSession();
        }

    }
    @Test
    public void selectByIdAndNameTest() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserMapper userMapper = session.getMapper(UserMapper.class);
            User user = userMapper.selectUserByIdAndName(5,"3");
            System.out.println(user);
        }finally {
            sqlSessionFactory.openSession();
        }

    }
    @Test
    public void selectByIdAndNameUseMapTest() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserMapper userMapper = session.getMapper(UserMapper.class);
            Map map = new LinkedHashMap();
            map.put("id",5);
            map.put("username","3");

            User user = userMapper.selectUserByIdAndNameUseMap(map);
            System.out.println(user);
        }finally {
            sqlSessionFactory.openSession();
        }

    }

    @Test
    public void insertTest() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserMapper userMapper = session.getMapper(UserMapper.class);
            User user01 = new User();
            user01.setUsername("4");
            user01.setPassword("4");
            userMapper.insertUser(user01);
            System.out.println(user01.getId());

        }finally {
            sqlSessionFactory.openSession();
        }
    }
    @Test
    public void updateTest() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserMapper userMapper = session.getMapper(UserMapper.class);
            User user01 = new User();
            user01.setId(2);
            user01.setUsername("修改1");

            boolean res = userMapper.updateUser(user01);
            System.out.println(res);

        }finally {
            sqlSessionFactory.openSession();
        }
    }
    @Test
    public void deleteTest() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserMapper userMapper = session.getMapper(UserMapper.class);


           int res =  userMapper.deleteUserById(1);
            System.out.println(res);

        }finally {
            sqlSessionFactory.openSession();
        }
    }
}

你可能感兴趣的