一:首先创建一个普通的maven项目。接着删掉src目录,接着在pom.xml下导入maven依赖

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<!-- <version>5.1.46</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>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
二:在该maven项目下创建子模块,该子模块会继承父模块的maven依赖,紧接着在该子模块下在resources目录下创建该模块的核心配置文件mybatis-config.xml

<?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.jdbc.Driver"/>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?useSSL=false&serverTimezone=Asia/Shanghai
&zeroDateTimeBehavior=CONVERT_TO_NULL
&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dong/dao/UserMapper.xml"></mapper>
</mappers>
</configuration>
三:在main/java目录下创建com.dong.dao/pojo/utils 三个包。
其中utils包目录下创建MybatisUtils.java ,表示为mybatis工具类

public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//使用Mybatis第一步:获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
pojo包存放实体类,例如:
public class User {
private int id;
private String name;
private String pwd;
}
dao包存放*Mapper.java 接口,这相当于javaWeb的Dao接口。
并且还存放了*Mapper.xml ,这相当于JavaWeb的Dao实现类。
例如:

package com.dong.dao;
import com.dong.pojo.User;
import java.util.List;
import java.util.Map;
/**
* @author dong
* @create 2020-02-25 10:26
*/
public interface UserMapper {
//模糊查询
List<User> getUserLike(String value);
//查询所以用户
List<User> getUserList();
//根据ID查询用户
User getUserById(int id);
//insert一个用户
int addUser(User user);
//万能map
int addUser2(Map<String,Object> map);
//修改用户
int updateUser(User user);
//删除用户
int deleteUser(int id);
}
该接口的方法名为xml实现语句的id

<?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="com.dong.dao.UserMapper">
<!--模糊查询-->
<select id="getUserLike" resultType="com.dong.pojo.User">
select * from mybatisdb.user where name like #{value}
</select>
<select id="getUserList" resultType="com.dong.pojo.User">
select * from mybatisdb.user
</select>
<select id="getUserById" parameterType="int" resultType="com.dong.pojo.User">
select * from mybatisdb.user where id = #{id}
</select>
<insert id="addUser" parameterType="com.dong.pojo.User">
insert into mybatisdb.user (id, name, pwd) values (#{id},#{name},#{pwd});
</insert>
<insert id="addUser2" parameterType="map">
insert into mybatisdb.user (id, name, pwd) values (#{userId},#{userName},#{password});
</insert>
<!--呵呵-->
<update id="updateUser" parameterType="com.dong.pojo.User">
update mybatisdb.user set name = #{name},pwd=#{pwd} where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatisdb.user where id=#{id};
</delete>
</mapper>
每个*Mapper.xml文件的namespace=绑定一个对应的mapper接口。创建完Mapper后,需要在子模块的核心配置文件mybatis-config.xml文件中注册mappers。
<mapper resource="com/dong/dao/*Mapper.xml"></mapper>
四,测试:

public class UserDaoTest {
@Test
public void test(){
//第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try{
//方式一:getMapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
//方式二:
//List<User> userList = sqlSession.selectList("com.rui.dao.UserDao.getUserList");
for (User user : userList) {
System.out.println(user);
}
}
finally {
//关闭SqlSession
sqlSession.close();
}
}
}
注意,当增删改需要提交事务
sqlSession.commit();
使用注解:
在dao目录下不用再创建*Mapper.xml,直接在*Mapper.java 接口中写入查询语句,例如:
@Select(value = "select * from user")
List<User> getUsers();
并且在核心配置文件mybatis-config.xml中绑定接口:
<!--绑定接口-->
<mappers>
<mapper class="com.dong.dao.UserMapper"/>
</mappers>
这里跟使用xml绑定接口稍微不同。此处是使用class并用'.'来分割,而在xml绑定接口是使用resource并用‘/’来分割。