本文目录
废话不多说,直接进入正题,下面将直接介绍如何使用MyBatis框架实现数据库的CRUD(数据库建表操作省略)
一、创建Maven工程
配置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>day01_eesy_01mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 调试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
二、创建Sql配置文件
配置SqlMapConfig.xml
其中Driver的value需要根据数据库jar包中的具体位置而定。
<?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>
<!-- 配置环境 -->
<environments default="mysql">
<!-- 配置mysql的环境 -->
<environment id="mysql">
<!-- 配置事务的类型 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池) -->
<dataSource type="POOLED">
<!-- 配置连接数据库的四个基本信息 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/Test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 -->
<mappers>
<mapper resource="com/Itcast/dao/InterfaceUserDao.xml"/>
</mappers>
</configuration>
三、创建实体类对象
UserDao.java
package com.Itcast.domain;
import java.io.Serializable;
import java.util.Date;
//实现Serializable接口是为了持久序列化
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
四、创建Dao接口
InterfaceUserDao.java
package com.Itcast.dao;
import com.Itcast.domain.User;
import java.util.List;
/*
用户持久层接口
*/
public interface InterfaceUserDao {
//以查询所有为例,其他操作大同小异
List<User> findAll();
}
五、配置XML配置文件
配置InterfaceUserDao.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">
<!-- namespace是dao的全限定类名,定位的是InterfaceUserDao.java这个接口-->
<mapper namespace="com.Itcast.dao.InterfaceUserDao">
<!-- 配置查询所有,通过namespace找到InterfaceUserDao.java,再通过id找到findAll()方法 ,resultType标签是该方法返回的结果类型,显然在这里返回的是User-->
<select id="findAll" resultType="com.Itcast.domain.User">
select * from user
</select>
</mapper>
六、创建Test测试类
创建MyBatisTest.java
package com.Itcast.test;
import com.Itcast.dao.InterfaceUserDao;
import com.Itcast.domain.User;
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.InputStream;
import java.util.List;
public class MyBatisTest {
public static void main(String[] args) throws Exception {
//1.读取配置文件
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(is);
//3.使用工厂生产SqlSession对象
SqlSession session = factory.openSession();
//4.使用SqlSession对象创建dao接口代理对象
InterfaceUserDao userDao = session.getMapper(InterfaceUserDao.class);
//5.使用代理对象执行方法
List<User> listAll = userDao.findAll();
for (User user : listAll) {
System.out.println(user);
}
//6.释放资源
session.close();
is.close();
}
}
运行结果:
这里有红字是因为之前配置pom.xml的时候写了日志的坐标,但是实际上并没有打印日志所需要的配置文件。
防止遗忘,先记下来。