IDEAL+maven搭建mybatis例子

IDEL+maven搭建mybatis例子

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

项目结构

1560559-20190326123318276-1937399476.png

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

1560559-20190326123236034-1186009549.png

1560559-20190326123251650-294929706.png

运行结果

1560559-20190326123302947-941832848.png

需要注意的地方
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

转载于:https://www.cnblogs.com/sm1128/p/10599371.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值