<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.xzixi.framework</groupId>
<artifactId>mybatis</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
声明:配置文件的名字叫什么都可以
<?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://127.0.0.1:3306/test?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--映射文件-->
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
CREATE TABLE IF NOT EXISTS student
(
sid CHAR(8) PRIMARY KEY,
sname VARCHAR(4) NOT NULL,
sage TINYINT NOT NULL,
sphone CHAR(11) NOT NULL UNIQUE
)
实体类的字段必须和表中字段一致,因为mybatis底层基于反射
package com.raoqi.pojo;
/**
* @author rqq
* @create 2022/7/3
*/
public class Student {
private String sid;
private String sname;
private Byte sage;
private String sphone;
@Override
public String toString() {
return "Student{" +
"sid='" + sid + '\'' +
", sname='" + sname + '\'' +
", sage=" + sage +
", sphone='" + sphone + '\'' +
'}';
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Byte getSage() {
return sage;
}
public void setSage(Byte sage) {
this.sage = sage;
}
public String getSphone() {
return sphone;
}
public void setSphone(String sphone) {
this.sphone = sphone;
}
}
####5.创建Dao层接口(也叫mapper层)
package com.raoqi.mapper;
/**
* @author rqq
* @create 2022/7/3
*/
public interface StudentMapper {
/**
* 增删改统一返回int代表影响行数
*/
int addStudent();
}
####6.创建接口的映射配置文件,建议配置文件名字和接口名字保持一致
<?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层接口">
<select id="这个接口中的方法">
具体的sql语句
</select>
</mapper>
<!--映射文件-->
<mappers>
<mapper resource="mappers/StudentMapper.xml"/>
</mappers>
public class MyBatisTest {
@Test
public void test() throws IOException {
//获取核心配置文件得输入流
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取sqlsession构建者对象
SqlSessionFactoryBuilder buider = new SqlSessionFactoryBuilder();
//使用文件输入流构建sqlsession工厂
SqlSessionFactory factory = buider.build(is);
//使用工厂获取会话对象
SqlSession sqlSession = factory.openSession();//默认不自动提交事务,传入true可以自动提交
//使用接口字节码对象获取实例 它内部会动态代理创建mapper得子类
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
mapper.addStudent();
//提交事务
sqlSession.commit();
}
}
1.读取核心配置文件mybatis.config.xml
2.找到配置文件中指定的映射文件
3.根据映射文件的namespace找到dao层接口
4.根据映射文件中的配置的标签id找到对应的方法并且执行里面的sql语句