1.创建数据表(库名为:mayikt)
CREATE TABLE `mayikt_user` (`id` int NOT NULL AUTO_INCREMENT,`username` varchar(20) DEFAULT NULL,`userpwd` varchar(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb3
2.创建maven项目
没下载安装或者不会创建的小伙伴可以看这里:maven的下载安装与配置环境变量!!!(全网最详细)_明天更新的博客-CSDN博客
创建完成后配置pom.xml文件:
<dependencies><!-- /org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.13</version></dependency><!-- com.mysql/mysql-connector-j --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.1.0</version></dependency></dependencies>
3.在main文件夹下的resources文件夹下创建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/mayikt?serverTimezone=GMT%2B8"/><property name="username" value="root"/><property name="password" value=""/></dataSource></environment></environments><mappers><mapper resource="mapper/userMapper.xml"/></mappers>
</configuration>
4.定义mapper ----编写我们mybatis 相关 sql语句 每个表 对应一个mapper
<?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="userMapper"><select id="getByUsers" resultType="com.entity.UserEntity">select * from mayikt_user</select>
</mapper>
5.定义java对象--需要注意下 类中的 成员属性与数据库表中字段 映射 默认 类中的 成员属性数据库表中字段名称对应的
/** Copyright (c) 2020, 2023, All rights reserved.**/
package com.entity;/*** <p>Project: mybatis-dome - UserEntity</p>* <p>Powered by scl On 2023-09-08 10:18:39</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
public class UserEntity {private Integer id;private String username;private String userpwd;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 String getUserpwd() {return userpwd;}public void setUserpwd(String userpwd) {this.userpwd = userpwd;}@Overridepublic String toString() {return "UserEntity{" +"id=" + id +", username='" + username + '\'' +", userpwd='" + userpwd + '\'' +'}';}
}
6.测试,使用 mybatis api开始执行该 sql语句即可 得到结果
/** Copyright (c) 2020, 2023, All rights reserved.**/
package com.test;import com.entity.UserEntity;
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;/*** <p>Project: mybatis-dome - Test01</p>* <p>Powered by scl On 2023-09-08 10:29:37</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
public class Test01 {public static void main(String[] args) throws IOException {// 1.读取加载mybatis-config.xmlString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 2.获取到获取到SqlSession sqlSession = sqlSessionFactory.openSession();// 3.根据 mapper id=getByUsers 执行该s ql 语句 通过 sql语句得到我们的对象 ormList<UserEntity> userEntitys = sqlSession.selectList("getByUsers", UserEntity.class);System.out.println(userEntitys);sqlSession.close();}
}