Mybatis
Mybatis最简单的使用
目录结构
数据库
Sql语句
-- auto-generated definition
create table student
(
id bigint not null
primary key,
password varchar(20) null,
class_number int null
);
数据
Maven坐标
<dependencies>
<!-- Mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<!-- 数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 工具类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
实体类(Entity)
package cn.pickle.entity;
import lombok.Data;
/**
* @author Pickle
* @version V1.0
* @date13:53 2022/12/14
*/
@Data
public class Student {
private Long id;
private String password;
private Integer classNumber;
}
测试类
package cn.pickle;
import cn.pickle.entity.Student;
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.Reader;
/**
* @author Pickle
* @version V1.0
* @date 2022/12/15 10:06
*/
public class Test {
public static void main(String[] args){
try {
String resources = "mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resources);
final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
try(final SqlSession sqlSession = sqlSessionFactory.openSession()){
final Student student = sqlSession.selectOne("selectStudent", 1);
System.out.println("Student ID: " + student.getId());
System.out.println("Student Password: " + student.getPassword());
System.out.println("Student ClassNumber: " + student.getClassNumber());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
配置文件
config.properties
jdbc.password=root
jdbc.username=root
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_demo
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- properties-->
<properties resource="config.properties"/>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="cn.pickle.entity"/>
</typeAliases>
<!-- typeHandler-->
<!-- objectFactory-->
<!-- <plugins>-->
<!-- <plugin interceptor=""></plugin>-->
<!-- </plugins>-->
<environments default="develop">
<environment id="develop">
<transactionManager type="JDBC">
<!-- 跳过关闭连接自动提交 -->
<property name="skipSetAutoCommitOnClose" value="true"/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="StudentMapper.xml"/>
</mappers>
</configuration>
StudentMapper.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="cn.pickle.entity.Student">
<select id="selectStudent" resultType="student">
Select * from mybatis_demo.student where id = #{id};
</select>
</mapper>