IDEL+maven搭建mybatis例子

Posted sm1128

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IDEL+maven搭建mybatis例子相关的知识,希望对你有一定的参考价值。

IDEL+maven搭建mybatis例子

参考于:https://www.cnblogs.com/wvae/p/9607684.html

项目结构

技术图片

pom.xml

<dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.3.0</version>
    </dependency>
    <!-- mysql驱动包 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
   </dependency>
    <!-- junit测试包 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
   </dependency>
</dependencies>

Employee.class

 package com.yjf.pojo;
 
 public class Employee {
    private Integer id;
     private String last_Name;
     private String email;
     private String gender;

    public Integer getId() {
        return id;
     }
 
     public void setId(Integer id) {
        this.id = id;
    }
 
     public String getLast_Name() {
         return last_Name;
     }
 
     public void setLast_Name(String last_Name) {
         this.last_Name = last_Name;
     }
 
     public String getEmail() {
         return email;
     }
 
     public void setEmail(String email) {
        this.email = email;
     }
 
     public String getGender() {
        return gender;
     }
 
     public void setGender(String gender) {
         this.gender = gender;
     } 
     @Override
     public String toString() {
        return "Employee{" +
                "id=" + id +
                 ", last_Name='" + last_Name + '\\'' +
                 ", email='" + email + '\\'' +
                 ", gender='" + gender + '\\'' +
                '}';
     }
 }

EmployeeMapper.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.yjf.pojo.Employee">
    <select id="selectEmp" resultType="com.yjf.pojo.Employee">
    select * from tbl_employee where id = #{id}
  </select>
</mapper>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <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://localhost:3306/mybatis?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true "/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/EmployeeMapper.xml"/>
    </mappers>
</configuration>

MyBatisTest

![mybatistable](F:\\软件\\typora\\md文件\\images\\mybatistable.png)import com.yjf.pojo.Employee;
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;

public class MyBatisTest {
    @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //能执行已经隐射的sql语句
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            Employee emp = openSession.selectOne("selectEmp", "1");
            System.out.println(emp);
        }finally {
            openSession.close();
        }
    }
}

mysql

表名 tbl_employee

技术图片

技术图片

运行结果

技术图片

需要注意的地方

mysql-connector 包版本过高需要用这种的driver和url  和以前的不同
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true "/>

引用地址的时候 直接copy reference

以上是关于IDEL+maven搭建mybatis例子的主要内容,如果未能解决你的问题,请参考以下文章

Maven 项目使用mybatis的环境搭建-基于xml形式实现查询所有的功能

Mybatis -- 动态Sql 环境搭建

使用idel和maven实现hadoop上数据的读取和写入

Mybatis学习01

Maven 项目使用mybatis的环境搭建-基于xml形式实现查询所有的功能

Spring+SpringMVC+MyBatis+Maven框架整合