Mybatis 是什么?
本篇介绍如何在 IDEA 中使用 Mybatis 开发 Java 项目,并且使用 Maven 来管理和集成依赖库。
Maven 的优点在于可以方便的管理各种依赖包,我们可以使用 Maven 来管理 Mybatis 和 MySQL 的相关库的依赖。
新建的 Maven 项目目录关系入下:
最后,Maven 目录如下:
新建一个 Maven 工程后,在 pom.xml 中配置 mybatis 和 mysql 依赖库。
<?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>net.libaoquan.learnssm.c3</groupId>
<artifactId>learnmybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis.version>3.4.5</mybatis.version>
<mysql.version>8.0.11</mysql.version>
</properties>
<dependencies><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</project>
在 resources 文件夹下新建 mybatis-config.xml 文件,填入 mybatis 配置信息。
其中:
<?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>
<typeAliases><!-- 别名 -->
<typeAlias alias="worker" type="net.libaoquan.learnssm.c3.learnmybatis.pojo.Worker"/>
</typeAliases >
<!-- 数据库环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="mapper/workerMapper.xml"/>
</mappers>
</configuration>
创建数据库并填入数据。
create database ssm;
create table worker (
id int primary key auto_increment,
name varchar(255),
note varchar(255)
);
alter table worker default character set utf8;
alter table worker change name name varchar(255) character set utf8;
alter table worker change note note varchar(255) character set utf8;
insert into worker (name, note) values (‘章三‘, ‘工程师‘);
insert into worker (name, note) values (‘王如‘, ‘工程师‘);
insert into worker (name, note) values (‘李斯‘, ‘工程师‘);
insert into worker (name, note) values (‘赵翼‘, ‘设计师‘);
insert into worker (name, note) values (‘孙起‘, ‘设计师‘);
一个 POJO 类,简单来说就是与数据库中一张表相关联的一个 Java 类。
在这里,新建一个 POJO 类 Worker,与数据表 worker 相关联,可以使自动填充填写 setter 和 getter 方法。
package net.libaoquan.learnssm.c3.learnmybatis.pojo;
public class Worker {
private Integer id;
private String name;
private String note;
public Worker() {
this.id = 0;
this.name = "";
this.note = "";
}
public Worker(Integer id, String name, String note) {
this.id = id;
this.name = name;
this.note = note;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public String toString() {
return "Worker{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", note=‘" + note + ‘\‘‘ +
‘}‘;
}
}
一个 xml 映射器用来实现如何处理与之关联的 POJO 类的数据逻辑,一般在接口中实现处理逻辑,具体的实现由 xml 文件中的 sql 语句实现。
使用 workerMapper.xml 以及 WorkerMapper 接口实现 xml 映射器。
首先,新建一个接口 workerMapper,其中方法 getWorker 是一个 Worker 类的处理逻辑。
package net.libaoquan.learnssm.c3.learnmybatis.mapper;
import net.libaoquan.learnssm.c3.learnmybatis.pojo.Worker;
public interface WorkerMapper {
public Worker getWorker(int id);
}
接着,在 resources 目录下建立 mapper 目录,创建 workerMapper.xml。
其中:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//rnybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.libaoquan.learnssm.c3.learnmybatis.mapper.WorkerMapper">
<select id="getWorker" parameterType="int" resultType="worker">
select id, name, note from worker where id= #{id}
</select>
</mapper>
新建 test 类,测试配置是否正确。
使用 2 种 SQL 发送器,分别是用 SqlSession 直接发送,和通过 SqlSession获取 Mapper接口再发送。
一般推荐使用第二种。
package net.libaoquan.learnssm.c3.learnmybatis;
import net.libaoquan.learnssm.c3.learnmybatis.mapper.WorkerMapper;
import net.libaoquan.learnssm.c3.learnmybatis.pojo.Worker;
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;
public class test {
public static void main(String[] args){
SqlSessionFactory SqlSessionFactory = null;
String resource = "mybatis-config.xml";
InputStream inputStream;
SqlSession sqlSession = null;
try {
inputStream = Resources.getResourceAsStream (resource);
SqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream);
sqlSession = SqlSessionFactory.openSession();
// 用 SqlSession 直接发送
Worker t = (Worker)sqlSession.selectOne("net.libaoquan.learnssm.c3.learnmybatis.mapper.WorkerMapper.getWorker", 1);
System.out.println(t.toString());
// 通过 SqlSession获取 Mapper接口再发送
WorkerMapper tm = sqlSession.getMapper(WorkerMapper.class);
Worker t2 = tm.getWorker(2);
System.out.println(t2.toString());
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
sqlSession.rollback();
}
finally {
if (sqlSession != null){
sqlSession.close();
}
}
}
}
Java 笔记——在 IDEA 中使用 Maven 配置和使用 mybatis
原文:https://www.cnblogs.com/libaoquan/p/9383470.html