MySQL-Mybatis(使用xml配置)


使用Mybatis连接MySQL数据库

使用XML配置

1.建立maven工程,导入mybatis坐标

导入maven坐标和数据库连接的pom坐标

    
        org.mybatis
        mybatis
        3.4.6
    
    
        mysql
        mysql-connector-java
        8.0.23
    

2.建立主配置文件

在resource文件夹下建立mybatis配置文件mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>


    
    
    
            
                    
                    
                            
                            
                            
                            
                    
            
    

在resource文件夹下建立jdbc.properties配置文件保存数据库配置

driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/zzu?serverTimeZone=UTC&characterEncoding=utf-8
user = root
password = 123456

3.建立实体类、工具类、Dao接口

3.1建立实体类

对应数据库表中的字段,进行实体类pojo/userinfo的建立

package pojo;
public class userinfo {
    private Integer id;
    private String college;
    private String name;
    private Integer picId;

    public userinfo(Integer id, String college, String name, Integer picId) {
        this.id = id;
        this.college = college;
        this.name = name;
        this.picId = picId;
    }

    public userinfo() {
    }

    @Override
    public String toString() {
        return "userinfo{" +
                "id=" + id +
                ", college='" + college + '\'' +
                ", name='" + name + '\'' +
                ", picId=" + picId +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPicId() {
        return picId;
    }

    public void setPicId(Integer picId) {
        this.picId = picId;
    }
}

连接工具类utils/MybatisUtils的创建

package util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtils {
    private static SqlSession sqlSession = null;
    public static SqlSession getSqlSession(){
        try{
            if(sqlSession == null){
                sqlSession = new SqlSessionFactoryBuilder().build(MybatisUtils.class.getClassLoader().
                        getResourceAsStream("mybatis-config.xml")).openSession();
            }
        }catch(Exception e){
                System.out.println("获取session出错");
                e.printStackTrace();
            }
        return sqlSession;
    }

    public static void close(){
        if(sqlSession != null){
            sqlSession.close();
        }
    }
}

Dao接口Dao/userDao的创建

package Dao;

import pojo.userinfo;

import java.util.List;

public interface UserDao {
	// 查找所有
    public List findAll();
}

4.Dao接口映射Mapper的创建

在resource文件下建立与Dao接口相同的目录结构

创建userDaoMapper.xml文件,对Dao接口进行配置

<?xml version="1.0" encoding="UTF-8" ?>


    

namespace指定的是要绑定的Dao接口,写全限定类名

当进行查询操作时,使用