Mybatis连接MySQL8数据库及注意事项

MySQL8对比MySQL5改变很大,所以连接方式也发生了变化。作者连接方式如下:
1.导入maven坐标:
注意作者使用的是8.0.17版本的jar包!!!!!(如果使用5.1.XX的jar包,连接的参数有一些变动,后面会有说到)


<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.17version>
dependency>

2.写mybatis配置文件mybatis.config
注意driver使用的是com.mysql.cj.jdbc.Driver(一定要有cj!!!!!!)url使用的是jdbc:mysql://127.0.0.1:3306/ssm?serverTimezone=UTC必须加入时区(serverTimezome)!!!否则可能报错
(PS:如果使用5.1.XX的jar包,driver仍然选择com.mysql.jdbc.Driver,但是url需要改成:jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false)



<configuration>
    
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC">transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/ssm?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            dataSource>
        environment>
    environments>

    
    <mappers>
        
        <package name="com.zenghui.dao"/>
    mappers>
configuration>

3.写测试类:

//前两个类是作者自己写的
import com.zenghui.dao.AccountDao;
import com.zenghui.domain.Account;
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.List;

public class TestMybatis {
    @Test
    public void run1() throws Exception {
        //加载mybatis配置文件,写自己mybatis配置文件的名字
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //创建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession对象
        SqlSession session = factory.openSession();
        //获取代理对象
        AccountDao dao = session.getMapper(AccountDao.class);
        //
        List<Account> all = dao.findAll();
        for (Account account:all) {
            System.out.println(account);
        }
        session.close();
        in.close();
    }
}

4.运行结果:
Mybatis连接MySQL8数据库及注意事项_第1张图片
后记:
import com.zenghui.dao.AccountDao;
import com.zenghui.domain.Account;作者的两个类如下

package com.zenghui.dao;

import com.zenghui.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 账户dao接口
 */
public interface AccountDao {
    //查询所有用户
    @Select("select * from account")
    public List<Account> findAll();
    //保存账户信息
    @Insert("insert into account (name,money) values(#{name},#{money})")
    public void saveAccount(Account account);
}


package com.zenghui.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Double money;

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    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 Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }
}

你可能感兴趣的:(Mybatis连接MySQL8数据库及注意事项)