Environment: IDEA MySQL
1. Build maven project, import coordinates: edit pom.xml, and create mybatis constraint.
stay https://mybatis.org/mybatis-3/zh/getting-started.html View the latest version of mybatis and the beginning of the constraint.
<!-- x.x.x by mybaits Version number --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency>
Using in mysql
mysql> show variables like "%version%";
Check your MySQL version.
<?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.lymMybits</groupId> <artifactId>Mybits_first</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> <!-- Location MyBatis --> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> <!-- Location MySQL --> </dependency> </dependencies> </project>
2. Create entity class and dao interface.
Entity class User:
package com.Mybaits_test.domain; import java.io.Serializable; /** * @author North Cang *User Entity class */ public class User implements Serializable { private Integer ID; private String name; private String password; private String sex; private String work; //Same as column name in table public Integer getID() { return ID; } public void setID(Integer ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getWork() { return work; } public void setWork(String work) { this.work = work; } @Override public String toString() { return "User{" + "ID=" + ID + ", name='" + name + '\'' + ", password='" + password + '\'' + ", sex='" + sex + '\'' + ", work='" + work + '\'' + '}'; } }
DAO interface:
package com.Mybaits_test.Dao; import com.Mybaits_test.domain.User; import java.util.List; /** * @author North Cang * dao Persistence layer interface */ public interface IF_UserDao { List<User> findAll(); }
3. Create the main configuration file of Mybatis: SQLMapConifg.xml, and create the mapping configuration file IF_UesDao.xml.
Note the constraints of Conifg and Mapper:
Constraints for Conifg:
<?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">
Mapper constraints:
<?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">
Main configuration file: create SQLMapConifg.xml under resource, and establish the constraint of Conifg.
<?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"> <!-- Master profile --> <configuration> <!-- Environmental configuration --> <environments default="mysql"> <!-- mysql Environmental configuration --> <environment id="mysql"> <!-- Configure the type of things --> <transactionManager type="JDBC"></transactionManager> <!-- Configure connection pool --> <dataSource type="POOLED"> <!-- Configure connection information to connect to the database --> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test.jdbc?serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- Specifies the location of the mapping profile, which is each DAO Class independent profile --> <mappers> <mapper resource="com/Mybaits_test/dao/IF_UesDao.xml"/> </mappers> </configuration>
Create and create the COM / mybaits · test / Dao folder under resource, map the configuration file if · uesdao.xml, and create the Mapper constraint.
<?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.Mybaits_test.Dao.IF_UserDao"> <select id="findAll"> select * from user </select> </mapper>