码迷,mamicode.com
首页 > 其他好文 > 详细

IDEL+maven搭建mybatis例子

时间:2019-03-26 13:47:23      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:junit   value   驱动   images   exception   local   where   .sql   uil   

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例子

标签:junit   value   驱动   images   exception   local   where   .sql   uil   

原文地址:https://www.cnblogs.com/sm1128/p/10599371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!