maven项目下Java 简单引入 Mtbatis

在pom.xml文件中配置mybatis坐标和数据库驱动:


            org.mybatis
            mybatis
            3.3.0
        
        
            mysql
            mysql-connector-java
            8.0.21
        

在resources资源目录下新建两个文件和一个文件夹:db.properties、mybtais-config.xml和mapper文件夹
db.properties代码如下:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/croom?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai  
jdbc.username=root
jdbc.password=root

mybtais-config.xml代码如下:




     
    
         
    
    
         
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

在src/main/java/entity文件下创建名为:User.java代码如下:

package entity;

public class User {
    private Long id;
    private String username;
    private String password;
    private String avater;
    private Integer role;
    private Integer department;
    private String xinqin;
    public User(){}
    public User(String username , String password, String avatar,Integer department,Integer role,String xinqin){
        this.username = username;
        this.password = password;
        this.avater = avatar;
        this.department = department;
        this.role = role;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long 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 getAvatar() {
        return avater;
    }

    public void setAvatar(String avatar) {
        this.avater = avatar;
    }

    public Integer getDepartment() {
        return department;
    }

    public void setDepartment(Integer department) {
        this.department = department;
    }

    public Integer getRole() {
        return role;
    }

    public void setRole(Integer role) {
        this.role = role;
    }

    public String getXinqin(){
        return xinqin;
    }

    public void setXinqin(String xinqin){
        this.xinqin=xinqin;
    }
}

在src/main/java/mapper文件下创建名为:UsersMapper.java代码如下:

package mapper;

import entity.User;

import java.util.List;

public interface UsersMapper {
    /*
    * 查询全部用户
    * */
    List selectAll();

    /*
    * 用户登录
    * */
    User checkLogin(String username);

    /*
    * 添加用户
    * */
    Integer insertUser(User user);

    /*
    * 用户名查找
    * */
    List checkUsername(String username);

    /*
     * 用户名查找
     * */
    User loginUserName(String username);

    /*
    * 通过序号查找用户
    * */
    User findByUserID(Long id);

    /*
    * 用户修改
    * */
    Integer updateByUserId(User user);

    /*
    * 删除用户
    * */
    Integer deleteUser(Long id);

}

在src/main/java/serviceImp文件下创建名为:ImpUserMapper.java代码如下:

package server;

import entity.Friend;
import entity.User;
import mapper.UsersMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import util.MybatisUtil;


import java.util.List;

public class ImpUserMapper{

    public static SqlSession sqlSession = MybatisUtil.getSqlSession();

    public static UsersMapper userMapper = sqlSession.getMapper(UsersMapper.class);

    public static List SelectAll(){
        List lists = userMapper.selectAll();
        return lists;
    }

    public static User CheckLogin(String username){
        User user = userMapper.checkLogin(username);
        return user;
    }

    public static void InsertUser(User user){
        userMapper.insertUser(user);
        sqlSession.commit();
    }

    public  static void DeleteUser(Long id){
        userMapper.deleteUser(id);
        sqlSession.commit();
    }

    public static Boolean checkUsername(String name){
        List lists = userMapper.checkUsername(name);
        if (lists == null || lists.size() ==0 ) return false;
        else return true;
    }

    public static List checkUserdepart(Integer department){
        return userMapper.checkUserdepart(department);
    }

    public static User loginUserName(String username){
        return userMapper.loginUserName(username);
    }

    public static void updateByUserId(User user){
        userMapper.updateByUserId(user);
        sqlSession.commit();
    }
}

在src/main/java/resources/mapper文件下创建名为:UsersMapper.xml代码如下:




    
    

    
    
    
           insert into users(username,password,avater,department,role,xinqin ) value (#{username},#{password},#{avater},#{department},#{role},#{xinqin})
    
    

    
        UPDATE users
        
            username=#{username},
            password=#{password},
            department=#{department},
            role=#{role},
            xinqin=#{xinqin},
        
        WHERE id=#{id}
    

    
            delete from users where id = #{id}
    



通过测试就可以调通。

你可能感兴趣的:(maven项目下Java 简单引入 Mtbatis)