2021-03-15

搭建mybatis环境

首先创建一个maven项目
2021-03-15_第1张图片
2021-03-15_第2张图片
2021-03-15_第3张图片
2021-03-15_第4张图片
然后我们要在pom.xml里添加配置

<?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>org.example</groupId>
  <artifactId>zyt239000</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>zyt239000 Maven Webapp</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>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.4.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>zyt239000</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_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-war-plugin</artifactId>
          <version>3.2.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>
      </plugins>
    </pluginManagement>
  </build>
</project>

接下来我们需要在src文件夹下的main.java中创建一个com.zyt.pojo包并在包文件里创建一个User.java
2021-03-15_第5张图片
其中User.java中的内容部分为`

package com.zyt.pojo;

public class User {
     
    private Integer id;
    private String username;
    private String password;

    private Dept dept;
    private String dname;

    public Dept getDept() {
     
        return dept;
    }

    public void setDept( Dept dept ) {
     
        this.dept = dept;
    }

    public Integer getId() {
     
        return id;
    }

    public void setId( Integer id ) {
     
        this.id = id;
    }

    public String getUsername() {
     
        return username;
    }

    public void setUsername( String username ) {
     
        this.username = username;
    }

    public String getPassword() {
     
        return password;
    }

    public void setPassword( String password ) {
     
        this.password = password;
    }

    public String getDname() {
     
        return dname;
    }

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

创建数据库相应的表后创建resources文件中的核心文件(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="mysql">
        <!--配置mysql的环境-->
        <environment id="mysql">
            <!--配置事务的类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/cyq?serverTimezone=Asia/Shanghai&useSSL=false" />
                <property name="username" value="root" />
                <property name="password" value="tiancaik" />
            </dataSource>
        </environment>
    </environments>
    <!-- mapping文件路径配置 -->
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>

</configuration>

zai resources中创建mpaaer文件夹,存放映射文件并在映射文件中创建UserMapper.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="mysql">
        <!--配置mysql的环境-->
        <environment id="mysql">
            <!--配置事务的类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/cyq?serverTimezone=Asia/Shanghai&useSSL=false" />
                <property name="username" value="root" />
                <property name="password" value="tiancaik" />
            </dataSource>
        </environment>
    </environments>
    <!-- mapping文件路径配置 -->
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>

</configuration>

你可能感兴趣的:(java)