欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis简易项目快速搭建

程序员文章站 2022-03-26 16:38:23
一.创建maven工程,引入jar包pom.xml org.mybatis mybatis 3.5.0

一.创建maven工程,引入jar包

pom.xml

<!--MyBatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.0</version>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--oracle数据库连接-->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

二.在resource资源文件夹下导入log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Encoding" value="UTF-8"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n"/>
        </layout>
    </appender>
    <logger name="java.sql">
        <level value="debug"/>
    </logger>
    <logger name="org.apache.ibatis">
        <level value="info"/>
    </logger>
    <root>
        <level value="debug"/>
        <appender-ref ref="STDOUT"/>
    </root>
</log4j:configuration>

三.创建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>
    <!--引入log4j日志-->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
                <property name="username" value="scott"/>
                <property name="password" value="tiger"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

四.创建Dept表的pojo类以及dao层

Dept.java

package com.etc.pojo;

/**
 * @Author Gao Xiaozhuang
 * @Date 2020/11/26  12:51
 * @Version 1.0
 * @Description
 */
public class Dept {
    private Integer deptno;
    private String dname;
    private String loc;

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }
}

DeptDao.java

package com.etc.dao;

import com.etc.pojo.Dept;

/**
 * @Author Gao Xiaozhuang
 * @Date 2020/11/26  12:49
 * @Version 1.0
 * @Description
 */
public interface DeptDao {
    Dept selectById(int id);
}

五.在资源文件夹下创建对应dao的mapper文件

DeptMapper.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">
<!--namespace 对应dao位置-->
<mapper namespace="com.etc.dao.DeptDao">
<!--id对应dao中的方法  parameterType对应参数类型 resultType对应结果集返回类型-->
    <select id="selectById" parameterType="int" resultType="com.etc.pojo.Dept">
    select deptno,dname,loc from Dept where deptno = #{id}
  </select>
</mapper>

六.在mybatis-config.xml中引入DeptMapper.xml,加载映射文件

 <!--加载映射文件-->
    <mappers>
        <mapper resource="com.etc.dao.imp/EmpMapper.xml"/>
        <mapper resource="com.etc.dao.imp/DeptMapper.xml"/>
    </mappers>

七.在test中编写测试单元

    @Test
    public void DeptDemo() throws IOException {
        String resource = "mybatis-config.xml";
        //读取配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DeptDao deptDao = sqlSession.getMapper(DeptDao.class);
        Dept dept = deptDao.selectById(10);
        System.out.println(dept);
        sqlSession.close();
    }

八.运行结果

MyBatis简易项目快速搭建

 

 

 

 

本文地址:https://blog.csdn.net/gxzhaha/article/details/110219071