第一步
创建一个maven项目,在pom.xml中加入
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
第二步 创建数据库添加表,同时创建相应的实体(字段名和属性名一致)
添加表数据
创建实体类
package com.itheima.entity;
import java.sql.PreparedStatement;
/**
* @Author: GenGen
* @Description: 6Ge
* @DateTime: 2023/2/23 9:25
**/
public class Db1User {
private int ID;
private String userName;
private String passWrod;
public Db1User(int ID, String userName, String passWrod) {
this.ID = ID;
this.userName = userName;
this.passWrod = passWrod;
}
public Db1User() {
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWrod() {
return passWrod;
}
public void setPassWrod(String passWrod) {
this.passWrod = passWrod;
}
@Override
public String toString() {
return "Db1User{" +
"ID=" + ID +
", userName='" + userName + '\'' +
", passWrod='" + passWrod + '\'' +
'}';
}
}
第三步 创建MyBatis的核心文件,文件名一般都是mybatis-comfig.xml
<?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>
<!--通过properties标签加载外部properties文件-->
<!-- <properties resource="jdbc.properties"></properties>-->
<!-- 环境配置 -->
<!-- 加载类路径下的属性文件 -->
<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://localhost:3306/db1"/>
<!--mysql8的url-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false"/>-->
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- mapper文件路径配置 -->
<mappers>
<mapper
resource="Mapper/UserMapper.xml"/>
</mappers>
</configuration>
第四步配置UserMapper.xml
<?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="UserMapper">
<select id="findAll" resultType="com.itheima.entity.Db1User">
select * from db1User
</select>
</mapper>
第五步使用mybatis
package com.itheima;
import com.itheima.entity.Db1User;
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 org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
/**
* @Author: GenGen
* @Description: 6Ge
* @DateTime: 2023/2/23 9:35
**/
public class userTest {
@Test
public void userTest(){
//读取文件名
String rr = "mybatis-config.xml";
//创建IO流
Reader reader = null;
try {
//读取mybatis-config.xml文件内容到reader对象中
reader = Resources.getResourceAsReader(rr);
}catch (IOException e){
e.printStackTrace();
}
//初始化mybatis数据库,创建SqlSessionFactory类的实例
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
//创建SqlSession实例
SqlSession sqlSession = sqlMapper.openSession();
//传入参数查询,返回杰哥哥
List<Db1User> users = sqlSession.selectList("findAll");
//输出结果
System.out.print(users.toString());
//关闭session
sqlSession.close();
}
}