一:新建项目 初始项目结构目录
二:导入依赖(导入坐标)
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
有可能还需要设置 静态资源过滤问题
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
三:在resources中新建File —> 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核心配置文件-->
<configuration>
<!--引入外部配置文件 注意标签位置是有顺序的 放错位置会报错-->
<properties resource="db.properties"/>
<!--配置日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- <setting name="logImpl" value="LOG4J"/>-->
</settings>
<!--可以给实体类起别名-->
<typeAliases>
<!-- <typeAlias type="com.csnz.pojo.User" alias="User"/>-->
<package name="com.csnz.pojo"/>
</typeAliases>
<!--环境配置-->
<environments default="development">
<environment id="development">
<!--事务管理器-->
<transactionManager type="JDBC"/>
<!--数据源-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--绑定Mapper-->
<mappers>
<!-- 错误示范 使用通配符 mybatis中没有这个功能 Spring才有
<mapper resource="com/csnz/dao/*.xml"/> -->
<!-- 方式一 -->
<!-- <package name="com.csnz.dao"/>-->
<!-- 方式二 -->
<!-- <mapper resource="com/csnz/dao/StudentMapper.xml"/>-->
<!-- <mapper resource="com/csnz/dao/TeacherMapper.xml"/>-->
<!-- 方式三 -->
<mapper class="com.csnz.dao.UserMapper"/>
</mappers>
</configuration>
四:编写 连接数据库的 外部配置文件db.properties
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username = 你的用户名,默认是root
password = 你的密码
五:java包下建立相关的包、实体类、dao、工具类等等
工具类MyBatisUtils.java
package com.csnz.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 {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
}
实体类
package com.csnz.pojo;
public class User {
private int id;
private String name;
private String pwd;
}
dao
UserMapper.java
package com.csnz.dao;
public interface UserMapper {
}
UserMapper.xml
注意每一个Mapper要有对应的Mapper.xml文件
并绑定命名空间namespace
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.csnz.dao.UserMapper">
</mapper>
最后再 在核心配置文件中绑定当前Mapper
六:测试
最好在test下 按照相同的文件路径建包 进行测试
七:整体项目目录结构
|