1.搭建前准备事项
1.IntelliJ IDEA 2021.2
2.Maven 3.8.2
3.Mysql 8.0.16
2.创建mybatis数据库和user表
CREATE DATABASE mybatis;
CREATE TABLE `USER`(
id INT NOT NULL PRIMARY key,
name VARCHAR(30) DEFAULT NULL,
psw VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO USER(id,name,psw) VALUES
(1,"波哥","123456"),
(2,"张三","123456"),
(3,"李四","123456")
3.创建Maven项目
1.删除项目中的src目录
2.导入依赖,将依赖放入到项目根目录下的pom.xml文件中,以下导入三个依赖,分别是mybatis,mysql驱动,junit。这里推荐一个可以快速查找依赖的网址https://mvnrepository.com/artifact/org.mybatis/mybatis/3.5.2
<!--导入依赖-->
<dependencies>
<!--mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--mysql驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!--junit-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
3.创建一个Maven模块(Module),并在src\main\resources路径下创建一个xml文件,导入数据库连接配置,这里要注意的是:因为我用的mysql是8.0.16,所以driver是
com.mysql.cj.jdbc.Driver
如果是mysql8.0一下版本,要用
com.mysql.jdbc.Driver
<?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="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/mybatis?
useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Tokyo"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
</configuration>
4.写一个工具类MybatisUtils,用于获取SqlSession,SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
package Utils;
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 MybatisUtils {
public static InputStream inputStream;
public static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
5.创建User实体类
package Pojo;
public class User {
private int id;
private String name;
private String psw;
public User() {
}
public User(int id, String name, String psw) {
this.id = id;
this.name = name;
this.psw = psw;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", name='" + name + '\'' +
", psw='" + psw + '\'' +
'}';
}
}
6.创建UserDao接口和UserMapper.xml配置文件
package Dao;
import Pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUser();
}
<?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="Dao.UserDao">
<select id="getUser" resultType="Pojo.User">
select * from user
</select>
</mapper>
7.在mybatis-config.xml中添加mappers
<mappers>
<mapper resource="Dao/UserMapper.xml"/>
</mappers>
8.因为maven不会导出自定义的xml文件,所以我们得在两个pom.xml里追加以下配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
9.最后测试一下,成功获取数据库User数据
import Dao.UserDao;
import Pojo.User;
import Utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class test {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> user = mapper.getUser();
for (Pojo.User user1 : user) {
System.out.println(user1.toString());
}
sqlSession.close();
}
}