mybatis的使用步骤
-
1.引入依赖
<!-- Mybatis依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.5</version></dependency> <!-- Mysql连接依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency>
-
配置mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/pengyanbin?serverTimezone=UTC"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><mappers><mapper resource="UserDaoXml.xml"/></mappers> </configuration>
-
定义接口
package com.pyb.dao; import com.pyb.pojo.Users; import java.util.List; /*** @version 1.0* @Author 彭彦彬* @Date 2025/1/2 16:13* @注释*/ public interface UserDao {List<Users>users(); }
-
编写数据库表文件
create table users (user_id int auto_incrementprimary key,username varchar(255) not null,password_hash varchar(255) not null,email varchar(255) not null,created_at datetime default CURRENT_TIMESTAMP null,updated_at datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP,constraint emailunique (email),constraint usernameunique (username) );
-
定义实体类映射实体类
package com.pyb.pojo; import java.time.LocalDateTime; /*** @version 1.0* @Author 彭彦彬* @Date 2025/1/2 16:02* @注释*/ public class Users {private int userId;private String userName;private String passwordHash;private String email;private String createAt;private String updateAt;public Users() {}public int getUserId() {return userId;} public void setUserId(int userId) {this.userId = userId;} public String getUserName() {return userName;} public void setUserName(String userName) {this.userName = userName;} public String getPasswordHash() {return passwordHash;} public void setPasswordHash(String passwordHash) {this.passwordHash = passwordHash;} public String getEmail() {return email;} public void setEmail(String email) {this.email = email;} public Users(int userId, String userName, String passwordHash, String email, String createAt, String updateAt) {this.userId = userId;this.userName = userName;this.passwordHash = passwordHash;this.email = email;this.createAt = createAt;this.updateAt = updateAt;} public String getCreateAt() {return createAt;} public void setCreateAt(String createAt) {this.createAt = createAt;} public String getUpdateAt() {return updateAt;} public void setUpdateAt(String updateAt) {this.updateAt = updateAt;} @Overridepublic String toString() {return "Users{" +"userId=" + userId +", userName='" + userName + '\'' +", passwordHash='" + passwordHash + '\'' +", email='" + email + '\'' +", createAt=" + createAt +", updateAt=" + updateAt +'}';} }
-
编写实现类
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.pyb.dao.UserDao"><select id="users" resultType="com.pyb.pojo.Users">select * from users</select> </mapper>
-
测试连接
package com.pyb.test; import com.pyb.dao.UserDao; import com.pyb.pojo.Users; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.util.List; /*** @version 1.0* @Author 彭彦彬* @Date 2025/1/2 16:14* @注释*/ public class TestMybatisConnection {public static void main(String[] args) {//定义mybatis配置文件的路径String resource = "mybatis-config.xml";InputStream inputStream = null;try {inputStream = Resources.getResourceAsStream(resource);} catch (IOException e) {throw new RuntimeException(e);}SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//获取UserDao实现类对象UserDao userDao = sqlSession.getMapper(UserDao.class);//调用方法测试List<Users> userList = userDao.users();for (Users users : userList) {System.out.println(users);}//释放资源sqlSession.close();} }