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

Mybatis(二):怎样使用Mybatis

程序员文章站 2024-01-01 10:08:10
一、创建项目(本文以Idea基于Maven构建的项目为例) New——>Project 笔者这里是选择自己本地的Maven及配置 最后点击Finish即可 二、在配置文件中添加依赖包 pom.xml配置文件中添加Mybatis、JDBC驱动、log4j日志管理的包依赖 完整代码如下:

一、创建项目(本文以idea基于maven构建的项目为例)

  • new——>project

Mybatis(二):怎样使用Mybatis

 

 

 

Mybatis(二):怎样使用Mybatis

 

 

 

Mybatis(二):怎样使用Mybatis

 

 

 

  • 笔者这里是选择自己本地的maven及配置

Mybatis(二):怎样使用Mybatis

 

 

 

  • 最后点击finish即可

Mybatis(二):怎样使用Mybatis

 

 

 

二、在配置文件中添加依赖包

  • pom.xml配置文件中添加mybatis、jdbc驱动、log4j日志管理的包依赖
  • 完整代码如下:
<?xml version="1.0" encoding="utf-8"?>

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>

  <groupid>com.zhurouwangzi</groupid>
  <artifactid>mybatisdemo</artifactid>
  <version>1.0-snapshot</version>

  <name>mybatisdemo</name>
  <!-- fixme change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <!--mybatis版本-->
    <mybatis.version>3.2.7</mybatis.version>
    <!--log4j版本-->
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
  </properties>

  <dependencies>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- mybatis核心包 -->
    <dependency>
      <groupid>org.mybatis</groupid>
      <artifactid>mybatis</artifactid>
      <version>${mybatis.version}</version>
    </dependency>
    <!-- mysql驱动包 -->
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.29</version>
    </dependency>

    <!-- 日志文件管理包 -->
    <!-- log start -->
    <dependency>
      <groupid>log4j</groupid>
      <artifactid>log4j</artifactid>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupid>org.slf4j</groupid>
      <artifactid>slf4j-api</artifactid>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupid>org.slf4j</groupid>
      <artifactid>slf4j-log4j12</artifactid>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupid>org.junit.jupiter</groupid>
      <artifactid>junit-jupiter-api</artifactid>
      <version>release</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginmanagement><!-- lock down plugins versions to avoid using maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_lifecycle -->
        <plugin>
          <artifactid>maven-clean-plugin</artifactid>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactid>maven-resources-plugin</artifactid>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactid>maven-compiler-plugin</artifactid>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactid>maven-surefire-plugin</artifactid>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactid>maven-jar-plugin</artifactid>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactid>maven-install-plugin</artifactid>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactid>maven-deploy-plugin</artifactid>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_lifecycle -->
        <plugin>
          <artifactid>maven-site-plugin</artifactid>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactid>maven-project-info-reports-plugin</artifactid>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginmanagement>
  </build>
</project>
  • 等依赖包下载完了之后我们在项目这里可以看到:

Mybatis(二):怎样使用Mybatis

 

 

 

  •  在src下添加一个resource文件夹,然后鼠标右键mark directory as resources root

Mybatis(二):怎样使用Mybatis

 

 

  •  在src/resource文件夹下创建sqlmapconfig.xml作为mybatis的全局配置文件
  • 在src/resource然后再创建一个log4j.properties文件用来配置日志文件的配置
  • 在src/resource再创建一个sqlmap文件夹用来存放mybatis的映射文件
  • 创建好的项目结构如下:

Mybatis(二):怎样使用Mybatis

 

 

 


三、设置mybatis相关配置文件

  • log4j.properties配置文件的内容如下:
# global logging configuration
log4j.rootlogger=debug, stdout
# console output...
log4j.appender.stdout=org.apache.log4j.consoleappender
log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%5p [%t] - %m%n
  • sqlmapconfig.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>
    <!-- 和spring整合后 environments配置将废除-->
    <environments default="development">
        <environment id="development">
            <!-- 使用jdbc事务管理-->
            <transactionmanager type="jdbc" />
            <!-- 数据库连接池-->
            <datasource type="pooled">
                <property name="driver" value="com.mysql.jdbc.driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/web_test3?characterencoding=utf-8" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </datasource>
        </environment>
    </environments>

</configuration>

 

四、创建实体类

Mybatis(二):怎样使用Mybatis

 

 

 

package com.zhurouwangzi.entity;

public class userbaseinfo {
    private int id;
    private string username;

    @override
    public string tostring(){
        return "user [id=" + id + ", username=" + username+ "]";
    }

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    public string getusername() {
        return username;
    }

    public void setusername(string username) {
        this.username = username;
    }
}
  • 在src/resource/sqlmap文件夹下添加user.xml作为userbaseinfo的映射文件

Mybatis(二):怎样使用Mybatis

 

 

 

<?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是命名空间,作用sql语句的隔离,后面还有重要作用
#{}作用就是占位符,相当于jdbc的“?”
parametertype:查询的参数类型
resulttype:查询结果的数据类型,如果时候pojo应该给全路径。
-->
<mapper namespace="test">
    <select id="getuserbyid" parametertype="int" resulttype="com.zhurouwangzi.entity.userbaseinfo">
        select * from user_baseinfo where id = #{id}
    </select>
</mapper>
  • 在sqlmapconfig.xml配置文件中添加<mappers>配置:

Mybatis(二):怎样使用Mybatis

 

五、用测试方法区测试

  • 代码如下:
package com.zhurouwangzi.service;

import com.zhurouwangzi.entity.userbaseinfo;
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.jupiter.api.test;

import java.io.inputstream;

public class mybatistest {

    @test
    public void test() throws exception{
        //1.创建一个sqlsessionfactorybuilder对象
        sqlsessionfactorybuilder sqlsessionfactorybuilder = new sqlsessionfactorybuilder();
        //2.加载配置文件
        inputstream inputstream = resources.getresourceasstream("sqlmapconfig.xml");
        //3.创建sqlsessionfactory工厂对象
        sqlsessionfactory sqlsessionfactory = sqlsessionfactorybuilder.build(inputstream);
        //4.获取sqlsession对象
        sqlsession sqlsession = sqlsessionfactory.opensession();
        //5.执行sql语句
        userbaseinfo userbaseinfo = sqlsession.selectone("getuserbyid", 16);
        system.out.println(userbaseinfo);
        //关闭资源
        sqlsession.close();
    }
}

运行结果如下:

Mybatis(二):怎样使用Mybatis

 

上一篇:

下一篇: