整体就是mysql里存了一张表,然后在java程序里用mybatis把数据读出来的一个简单示例。
库 blog里有一张表 article
整个项目就是增加了这3个文件
首先是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://localhost:3306/blog"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><!--写好的sql映射文件(EmployeeMapper.xml)注册到全局配置文件(mybatis-config.xml)中--><mappers><mapper resource="ArticleMapper.xml"/></mappers>
</configuration>
然后是ArticleMapper.XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper"><select id="selectArticle" resultType="org.example.po.Article">select * from article where id = #{id}</select>
</mapper>
然后是和数据库存储模型对应的对象Article类
package org.example.po;import java.util.Date;public class Article {private Integer id;private String title;private String desc;private Integer cate;private Date createdAt;private Date updatedAt;private String content;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title == null ? null : title.trim();}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc == null ? null : desc.trim();}public Integer getCate() {return cate;}public void setCate(Integer cate) {this.cate = cate;}public Date getCreatedAt() {return createdAt;}public void setCreatedAt(Date createdAt) {this.createdAt = createdAt;}public Date getUpdatedAt() {return updatedAt;}public void setUpdatedAt(Date updatedAt) {this.updatedAt = updatedAt;}public String getContent() {return content;}public void setContent(String content) {this.content = content == null ? null : content.trim();}
}
然后就是运行类,main方法
package org.example;import com.google.gson.Gson;
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.example.po.Article;import java.io.InputStream;public class Main {public static void main(String[] args) throws Exception {String resource="mybatis-config.xml";InputStream inputStream= Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession=sqlSessionFactory.openSession();Article article=sqlSession.selectOne("selectArticle",1);Gson gson=new Gson();System.out.println("Hello world!");System.out.println(gson.toJson(article));}
}
最后把pom.XML也贴一下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>mybatis-study</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>14</maven.compiler.source><maven.compiler.target>14</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.10</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></dependency></dependencies></project>
其他问题可以参这个官方教程
mybatis – MyBatis 3 | XML 映射器