Mybatis
Mybatis最简单的使用

目录结构

数据库
Sql语句
| |
| create table student |
| ( |
| id bigint not null |
| primary key, |
| password varchar(20) null, |
| class_number int null |
| ); |
数据

Maven坐标
| <dependencies> |
| |
| <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; |
| |
| |
| |
| |
| |
| |
| @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; |
| |
| |
| |
| |
| |
| |
| 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 resource="config.properties"/> |
| |
| <settings> |
| <setting name="mapUnderscoreToCamelCase" value="true"/> |
| </settings> |
| |
| <typeAliases> |
| <package name="cn.pickle.entity"/> |
| </typeAliases> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <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> |
测试结果

· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术