mybatis配置

个人推荐最好直接看官网文档,下方赘述只可快速建立mybatis环境。
mybatis官方文档:https://mybatis.org/mybatis-3/zh/index.html

1、jdbc.properties

创建jdbc.properties文件用于连接mysql数据库

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/database?serverTimezone=GMT%2B8&characterEncoding=utf8&useUnicode=true
username=root
password=******

2、mybatis-config.xml

创建mybatis-config.xml文件

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
        
<configuration>
    <!--引入外部配置文件-->
    <properties resource="jdbc.properties"></properties>

    <settings>
        <!--标准的日志工厂-->
        <setting name="logImpl" value="STDOUT_LOGGING"></setting>
    </settings>

    <typeAliases>
        <!--<typeAlias type="com.area.pojo.User" alias="User"></typeAlias>-->
        <package name="com.area.pojo"/><!--扫描并生成别名映射(pojo类名小写))-->
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/area/dao/UserMapper.xml"/><!--映射文件位置,相对于最初包名-->
    </mappers>
</configuration>

3、Mapper.xml

创建接口后,进行注册映射文件的创建

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        
<mapper namespace="com.area.dao.UserDao">
    <select id="getAllUser" resultType="user">
        select * from `user`;
    </select>

</mapper>

4、MybatisUtil

创建SqlSessionFactory工具类,进行sqlSession的调用

package com.area.utils;

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 java.io.IOException;
import java.io.InputStream;

// SqlSessionFactory
public class MybatisUtil {

    private static SqlSessionFactory sqlSessionFactory;

    static{
        String resource = "mybatis-config.xml";//读取mybatis配置文件
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    //
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值