Introduction of Mybatis Environment Setting

Introduction

        Mybatis is a persistence framework that allows easier ways to handle the repository layer in Java. Without Mybatis or similar frameworks like Hibernate, we need to write every JDBC connection and SQL command ourselves. Therefore, we must understand how to use these frameworks properly. Today's tutorial briefly introduces how to set up the Mybatis environment in Java through XML.

1. Mybatis Jar and JDBC connection Jar

        The first setting up step is including the Mybatis and JDBC jar package. If you are using Maven, here is the dependencies code:

<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-typehandlers-jsr310</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
    </dependencies>

  You can use the latest version on maven.org.

2. Create Mybatis Configuration File 

        After adding dependencies in Maven, the next step is to make a configuration file for our Mybatis project. Our configuration file contains many essential sections; We will briefly introduce them. To correctly name the configuration file is vital. Here, I called it "mybatis.cfg.xml." The configuration file should be put into the resource folder.

<?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>
	<settings>
		<setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>

	<typeAliases>
		<package name="com.tencent.entity"/>
	</typeAliases>
	
   <environments default="dev">  

        <environment id="dev">  

            <transactionManager type="JDBC"></transactionManager>  

            <dataSource type="POOLED">  
				<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/XXXXXXX?characterEncoding=utf-8&amp;allowMultiQueries=true"/>
				<property name="username" value="XXXXXXXXX"/><!-- 数据库用户名 database username-->
				<property name="password" value="XXXXXXXXX"/><!-- 数据库密码 database password-->
            </dataSource>  

        </environment>  

    </environments>  
	
	<mappers>
		<package name="com.tencent.dao"/>
	</mappers>
</configuration>  

The XML code above is a typical configuration file of Mybatis. The setting tag's function is for confirming some properties of your Mybatis. For example, what kind of Logging method do you want to exercise?  

"TypeAliases" is handy when you have an insanely long package name. TypeAliases means a short term for a package.  

The "Environment" section is crucial. It determines the information in our database, including the connection URL, username, and password. Meanwhile, the environment tag confirms which driver manager we want to use. 

Finally, the "mapper" tag defines the repository layer package we want to connect.

One thing I want to clarify here is that those tags I mentioned above are not all tags in the Mybatis configuration file. Besides, there are other tags like "databaseIdProvider," "Plugin", and "ObjectFactory" etc. However, I only talked about necessary tags in this tutorial. For more information, readers can refer to mybatis.org. 

Continues

The following tutorial will implement our Mybatis framework in actual code practices! I am so excited!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值