1.在Manven架构中创建resources目录,并在pom中配置引入
图1.1 创建resources资源目录
引入pom
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
<include>mapperxmls/*.xml</include>
</includes>
</resource>
</resources>
</build>
2.引入MyBatis框架(POM文件) org.mybatis mybatis 3.4.6
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
3.在resources下新建编辑db.properties文件,用于编写数据库相关信息的
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/bmi?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
db.username=root
db.password=123456
4.创建MyBatis配置文件(mybatis.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>
<!-- 加载类路径下的属性文件 -->
<properties resource="db.properties"/>
<!-- 设置一个默认的连接环境信息 -->
<environments default="mysql_developer">
<!-- 连接环境信息,取一个任意唯一的名字 -->
<environment id="mysql_developer">
<!-- mybatis使用jdbc事务管理方式 -->
<transactionManager type="jdbc"/>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="pooled">
<!-- 配置与数据库交互的4个必要属性 -->
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapperxmls/UsersMapper.xml"/>
</mappers>
</configuration>
5.在resources目录下创建mapperxmls目录,在目录下创建映射配置文件(~Mapper.xml)
image.png
<?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.fuful.mappers.UsersMapper">
<insert id="addUsers" parameterType="com.fuful.domian.Users" useGeneratedKeys="true" keyProperty="uid" keyColumn="uid">
insert into Users(uname, upwd) values (#{uname,jdbcType=VARCHAR},#{upwd,jdbcType=VARCHAR});
</insert>
</mapper>
注意:里面的信息一定要对应正确!!!
6.创建Mapper接口,用户封装数据操作方法
在java...目录下新建domain目录
新建user实例方法
package com.fuful.domian;
import javax.xml.crypto.Data;
public class Users {
private int uid;
private String uname;
private String upwd;
private Data addtime;
public Users() {
}
public Users(String uname, String upwd) {
this.uname = uname;
this.upwd = upwd;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public Data getAddtime() {
return addtime;
}
public void setAddtime(Data addtime) {
this.addtime = addtime;
}
}
在java...目录下新建mapper目录
新建usermapper接口
package com.fuful.mappers;
import com.fuful.domian.Users;
public interface UsersMapper {
int addUsers(Users users);
}
7.将映射配置文件与Mapper接口对接(spacename属性)(配置Mapper.xml文件)
8.将映射配置文件,添加到MyBatis.xml中(标签下)
9.根据数据操作方法,配置映射配置文件(,....)
10.利用MyBatis的api(aSqlSession)操作数据库
新建utils目录
public class MybatisUtil {
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
private static SqlSessionFactory sqlSessionFactory;
/**
* 加载位于resources/mybatis.xml配置文件
*/
static {
try {
Reader reader = Resources.getResourceAsReader("mybatis.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 禁止外界通过new方法创建
*/
private MybatisUtil() {
}
/**
* 获取SqlSession
*/
public static SqlSession getSqlSession() {
//从当前线程中获取SqlSession对象
SqlSession sqlSession = threadLocal.get();
//如果SqlSession对象为空
if (sqlSession == null) {
//在SqlSessionFactory非空的情况下,获取SqlSession对象
sqlSession = sqlSessionFactory.openSession();
//将SqlSession对象与当前线程绑定在一起
threadLocal.set(sqlSession);
}
//返回SqlSession对象
return sqlSession;
}
/**
* 关闭SqlSession与当前线程分开
*/
public static void closeSqlSession() {
//从当前线程中获取SqlSession对象
SqlSession sqlSession = threadLocal.get();
//如果SqlSession对象非空
if (sqlSession != null) {
//关闭SqlSession对象
sqlSession.close();
//分开当前线程与SqlSession对象的关系,目的是让GC尽早回收
threadLocal.remove();
}
}
}
app的main方法调用:
public static void main( String[] args )
{
SqlSession sqlSession = MybatisUtil.getSqlSession();
UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
usersMapper.addUsers(new Users("hhh","hhh123"));
//非查询查询操作必须手动提交
sqlSession.commit();
MybatisUtil.closeSqlSession();
}
11.连接Database查看运行结果
image.png
image.png