Mybatis environment construction
- New Project
- Create a Maven project and import related dependencies (pom.xml)
- Problems that may occur when importing dependencies
- New database and table
- New entity class User
- Create the persistence layer interface of the entity class
- New interface mapping file
- Write the main configuration file of Mybatis
- to sum up:
New Project
First create a new project,
select Maven, and then continue to the next step.
As for the knowledge of Maven, if you don’t understand, you can look for information. If I want to publish, I can only post it after I wash the SSM series.
After the creation is complete:
Create a Maven project and import related dependencies (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>MyBatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 导入依赖 -->
<dependencies>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
<scope>runtime</scope>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Problems that may occur when importing dependencies
You can go and read my article because I also made a mistake. Portal
New database and table
-- 创建数据库
CREATE DATABASE IF NOT EXISTS db_mybatis CHARACTER SET utf8;
-- 创建数据表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` datetime default NULL COMMENT '生日',
`sex` char(1) default NULL COMMENT '性别',
`address` varchar(256) default NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 添加记录
insert into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (41,'老王','2018-02-27 17:47:08','男','广东广州'),(42,'张三','2018-03-02 15:09:37','女','广东深圳'),(43,'李四','2018-03-04 11:34:34','女','广东佛山'),(45,'小二','2018-03-04 12:04:06','男','广东茂名'),(46,'老王','2018-03-07 17:37:26','男','广东珠海'),(48,'王五','2018-03-08 11:44:00','女','广东惠州');
New entity class User
Note: the entity class attribute name is consistent with the database table field name
package com.Domain;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import java.io.Serializable;
import java.util.Date;
/**
* @author zhang
*/
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
Create the persistence layer interface of the entity class
package com.Dao ;
import com.Doamin.User;
import java.util.List;
/**
* @author zhang
* 用户的持久层接口
*/
public interface UserDao {
//查询所有操作
List<User> findAll();
}
New interface mapping file
Create a new UserDao.xml file under resource:
<?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">
<mapper namespace="com.Dao.UserDao">
<!-- 配置查询所有 -->
<select id="findAll">
select * from user
</select>
</mapper>
The id attribute above is not arbitrarily taken, it must be consistent with the method name of your corresponding interface
Write the main configuration file of Mybatis
Also create a new SqlMapConfig.xml file under resource:
<?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">
<!-- 配置数据库的基本信息 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="7107883"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每个Dao独立配置的文件 -->
<mappers>
<mapper resource="com/Dao/UserDao.xml"/>
</mappers>
</configuration>
The final structure diagram is as follows:
to sum up:
The environment of MyBatis is mainly divided into four steps:
- Create a Maven project and import dependencies
- Create entity class and Dao interface
- Create the main configuration file of MyBatis: SqlMapConfig.xml
- Create a mapping configuration file: UserDao.xml
Precautions:
-
In Mybatis, the operation interface name and mapping file of the persistence layer are also called Mapper, so UserMapper and UserDao are the same
-
The location of the MyBatis mapping configuration file must be the same as the package structure of the Dao interface. (Pictured)
-
The value of the namespace attribute of the mapper tag of the mapping configuration file must be the fully qualified class name of the DAO interface
-
The operation configuration of the mapping configuration file (select), the value of the id attribute must be the method name of the Dao interface
What are the benefits of paying attention to these?
When we follow these precautions above, we don’t need to write Dao implementation classes during development.
After the environment is set up, enter the entry case and custom mybatis framework