Mybatis快速入门

Mybatis快速入门相关配置信息

Mybatis详细信息

文章目录

  • Mybatis快速入门相关配置信息
    • 一、导入相关依赖
    • 二、添加相关logback的配置文件
    • 三、编写Mybaits核心配置文件
    • 四、编写实体类的映射文件UserMapper.xml
    • 五、编写测试类

整体项目框架
Mybatis快速入门_第1张图片

一、导入相关依赖

<dependencies>
	
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>testscope>
    dependency>
	
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.31version>
    dependency>
	
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.5.5version>
    dependency>
	 
    
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
      <version>1.7.20version>
    dependency>
    
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>1.2.3version>
    dependency>
    
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
      <version>1.2.3version>
    dependency>

二、添加相关logback的配置文件


<configuration>
    
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%level] %blue(%d{HH:mm:ss.SSS}) %cyan([%thread]) %boldGreen(%logger{15}) - %msg %npattern>
        encoder>
    appender>

    <logger name="com.lz" level="DEBUG" additivity="false">
        <appender-ref ref="Console"/>
    logger>
    
    <root level="DEBUG">
        <appender-ref ref="Console"/>
    root>
configuration>

三、编写Mybaits核心配置文件


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
        <package name="com.lz.pojo"/>
    typeAliases>
    
<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            
            <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        dataSource>
    environment>
environments>
    <mappers>
        
        <mapper resource="UserMapper.xml"/>
    mappers>
configuration>

四、编写实体类的映射文件UserMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="test">
   <select id="selectAll" resultType="com.lz.pojo.User">
      select * from tb_user;
   select>
mapper>

五、编写测试类

package com.lz;

import com.lz.pojo.User;
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;
import java.util.List;
/*
    MyBatis快速入门
 */
public class MyBatis_Demo {
    public static void main(String[] args) throws IOException {
        //1、创建sqlSessionFactory对象
        String resource = "mybatis-config.xml"; //配置mybatis文件的路径
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        //2、获取sqlSession对象
        SqlSession sqlSession= sqlSessionFactory.openSession();
        //3.执行sql
        List<User> userList = sqlSession.selectList("test.selectAll");
        //4.释放资源
        sqlSession.close();
        System.out.println(userList);
    }
}

你可能感兴趣的:(mybatis,java,mysql)