4

mybatis学习日志(一)

 4 years ago
source link: https://blog.csdn.net/lanlantanglan/article/details/115572493
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
  • 使用的编译器-IDEA 2020

1.项目建立

新建一个maven空项目吗,点击next
在这里插入图片描述
给项目取名,确定目录位置
GroupID按照个人喜好取
例如:com.公司名.项目名
在这里插入图片描述
等待项目创建完毕
在这里插入图片描述

2.maven配置

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>com.sari.studyMybatis</groupId>
    <artifactId>study_01mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!--链接的是mysql数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
  • 其中日志log4j暂无配置教程,等待后续更新
  • 点击编译器上面出现的小图标,即可开始下载

3.mybatis配置文件

文件架构如下(这个文件夹需要自己建立,其中java下一定要建package)

  • resource:资源文件目录,也就是我们相关配置的文件区域
  • java:核心代码区

在这里插入图片描述
配置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">
<!-- mybatis的主配置文件 -->
<configuration>
    <!-- 配置环境 -->
    <environments default="mysql">
        <!-- 配置mysql文件 -->
        <environment id="mysql">
            <!-- 配置事务类型 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源连接池 -->
            <dataSource type="POOLED">
                <!-- 配置链接数据库的4个基本数据 -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis01?characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="LLTL520"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 配个DAO的映射配置文件 -->
    <mappers>
        <mapper class="com.sari.DAO.IUser"/>
    </mappers>
    <!-- TODO log4j的配置数据文件 -->
</configuration>
  • mappers配置下次说

目前仓库还未建立


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK