IDEAL+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 "/>


mysql-connector-java-8.0.11.jar
@Test
	public void test() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.cj.jdbc.Driver");
		Connection connection = DriverManager.getConnection(""
				+ "jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true"
				+ "", "root", "root");
	}
引用地址的时候 直接copy reference
posted @ 2019-03-26 12:34  等不到时光等天亮  阅读(399)  评论(0编辑  收藏  举报