面试必备3-快速搭建mybatis

面试必备3-快速搭建mybatis,第1张

面试必备3-快速搭建mybatis 面试必备3-快速搭建mybatis 1、新建表 user,插入样本数据,创建pojo实体

sql脚本

create database `lrx`;
create table `user`{
	`id` int primary key not null,
	`username` varchar(20),
	`password` varchar(20)
};
insert into `user` (`id`,`username`,`password`) values(1,'lihua','123');

User.java

package com.pojo;


public class User {

    private Integer id;
    private String username;
    private String password;


    public Integer getId() {
        return id;
    }

    public User(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                '}';
    }
}
2、导入依赖

pom.xml



    
        javaweb-maven
        lrx
        1.0-SNAPSHOT
    
    4.0.0

    mybatis-01-buildEnvironment
    
        
        
            mysql
            mysql-connector-java
            8.0.24
        

        
        
            junit
            junit
            4.13.2
            test
        

        
        
            org.mybatis
            mybatis
            3.5.7
        

    
    
        
            
                src/main/java
                
                    ***.xml
                
                true
            
            
                src/main/resources
                
                    ***.xml
                
                true
            
        
    

3、配置文件 mybatisConfig.xml



    
        
            
            
                
                
                
                
            
        
    
    
        
    

4、新建UserMapper 接口并编写映射文件userMapper.xml

UserMapper.java

package com.mapper;

import com.pojo.User;

import java.util.List;

public interface UserMapper {
    User select(int id);
    int addUser(User user);
    int update(User user);
    int delete(int id);
}

userMapper.xml