一、Maven 环境配置
1、配置 pom.xml
1.1、依赖的 jar包
<dependencies> <!-- mysql8 驱动,与本地mysql版本需要一致 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!-- junit,测试工具 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
1.2、防止资源导出失败
<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>
2、在resources下编写 Mybatis核心配置文件
命名为mybatis-config.xml,作用是连接数据库
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//OTD Config 3.0//EN" "http://mybatis.org/schema/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"/> <property name="username" value="root"/> <property name="password" value="******"/> </dataSource> </environment> </environments> <!-- 绑定映射配置文件 --> <mappers> <mapper resource="com/study/dao/UserMapper.xml"/> </mappers> </configuration>
二、搭建结构
1、编写mybatis工具类(utils)
作用是获取SqlSession对象,相当于JDBC中的 Statement对象
package com.study.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 { //1.获取sqlSessionFactory对象 String resource = "mybatis-config.xml";//maven项目下路径这么写 InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //SqlSessionFactory 顾名思义,我们可以通过工厂创建SqlSession对象 //SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
2、编写实体类(pojo)
实现所有 get和set 方法并重写 toString方法
package com.study.pojo; public class User { //全部按着数据库来,名字一一对应 private int id; private String name; private String pwd; public User(){ } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } 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 getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
3、Mybatis 的实现(dao层)
以添加用户为例
3.1、编写接口(UserMapper.java)
接口的实现在映射文件(UserMapper.xml)中实现
public interface UserMapper { int addUser(User user); }
3.2、通过映射绑定并实现接口(UserMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//OTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.study.dao.UserMapper"> <insert id="addUser" parameterType="com.study.pojo.User"> insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd}); </insert> </mapper>
三、测试
1、搭建数据库
create table user ( id int(20) not null, name varchar(30)default null, pwd varchar(30) default null, primary key(id) ) engine=innodb default charset=utf8;
2、编写测试类
注意:增删改需要提交事务 , 查询语句不需要
Mybatis 操作数据库和 JDBC 比较相似但 Mybatis更加简单便捷!
@Test public void updateUser(){ //1.获取数据库操作对象 SqlSession sqlSession = MybatisUtils.getSqlSession(); //2.获取接口 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //3.执行SQL语句 User user = new User(1,"燕双鹰","123456"); mapper.addUser(user); //4.提交事务 sqlSession.commit(); //5.关闭资源 sqlSession.close(); }
3、运行结果