MyBatis(原名为iBatis)是一个开源的持久化框架,用于将Java对象映射到关系数据库中。它通过XML或注解的方式配置SQL语句和结果映射,提供了灵活和强大的数据库访问功能。MyBatis提供了SQL的完全控制权限,同时也提供了许多便利的特性,使得数据库操作更加简单和高效。它被广泛应用于Java web开发和企业级应用中,是一个非常受欢迎的框架之一。
1.创建一个maven工程 pom文件中导包
org.mybatis
mybatis
x.x.x
mysql
mysql-connector-java
8.0.33
org.projectlombok
lombok
1.18.28
2.创建一个resources文件夹 并make
创建一个 mybatis-config.xml文件
3.在文件夹下创建一个mapper文件夹并创建StudentMapper.xml文件
StudentMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.mapper.StudentMapper">
<insert id="saveStudent">
insert into student
values (null, '新数据', 18, '郑州', 1)
insert>
<delete id="delStudent">
delete
from student
where id = 2
delete>
<update id="updateStudent">
update student
set sname = '张三'
where id = 1
update>
<select id="listStudent" resultType="com.aaa.dto.Student">
select *
from student
select>
mapper>
JavaTest
package com.aaa.test;
import com.aaa.dto.Student;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @Author Grg
* @Date 2023/9/12 9:01
* @PackageName:com.aaa.test
* @ClassName: JavaTest
* @Description: 又是码代码的一天
* @Version plus max 宇宙无敌终极版本
*/
public class JavaTest {
@Test
public void test() throws IOException {
//读取配置文件
String resource = "mybatis-config.xml";
// 获取资源输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂获取SqlSession 相当于拿到一个连接对象 进行数据库操作
SqlSession sqlSession = sqlSessionFactory.openSession();
int i = sqlSession.insert("com.aaa.mapper.StudentMapper.saveStudent");
System.out.println(i);
//mybatis自带的实务操作
sqlSession.commit();
sqlSession.close();
}
@Test
public void test1() throws IOException {
//读取配置文件
String resource = "mybatis-config.xml";
// 获取资源输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂获取SqlSession 相当于拿到一个连接对象 进行数据库操作
SqlSession sqlSession = sqlSessionFactory.openSession();
int i = sqlSession.delete("com.aaa.mapper.StudentMapper.delStudent");
System.out.println(i);
//mybatis自带的实务操作
sqlSession.commit();
sqlSession.close();
}
@Test
public void test2() throws IOException {
//读取配置文件
String resource = "mybatis-config.xml";
// 获取资源输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂获取SqlSession 相当于拿到一个连接对象 进行数据库操作
SqlSession sqlSession = sqlSessionFactory.openSession();
int i = sqlSession.update("com.aaa.mapper.StudentMapper.updateStudent");
System.out.println(i);
//mybatis自带的实务操作
sqlSession.commit();
sqlSession.close();
}
@Test
public void test3() throws IOException {
//读取配置文件
String resource = "mybatis-config.xml";
// 获取资源输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂获取SqlSession 相当于拿到一个连接对象 进行数据库操作
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Student> data = sqlSession.selectList("com.aaa.mapper.StudentMapper.listStudent");
System.out.println(data);
//mybatis自带的实务操作
sqlSession.commit();
sqlSession.close();
}
}
创建学生实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
private Integer id;
private String sname;
private String sage;
private String saddress;
private Integer sgid;
}
StudentMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.mapper.StudentMapper">
<insert id="saveStudent" parameterType="com.aaa.entity.Student">
insert into student
values (null, #{name}, #{age}, #{address}, 1)
insert>
<delete id="delStudent" parameterType="java.lang.Integer">
delete
from student
where id = #{id}
delete>
<update id="updateStudent" parameterType="com.aaa.entity.Student">
update student
set name=#{name},
age=#{age},
address=#{address},
gid=#{gid}
where id = #{id}
update>
<select id="getOne" parameterType="java.lang.Integer" resultType="com.aaa.entity.Student">
select *
from student
where id = #{id}
select>
<select id="listStudent" resultType="com.aaa.entity.Student">
select *
from student
select>
mapper>
JavaTest
package com.aaa.test;
import com.aaa.entity.Student;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @Author Grg
* @Date 2023/9/12 9:01
* @PackageName:com.aaa.test
* @ClassName: JavaTest
* @Description: 又是码代码的一天
* @Version plus max 宇宙无敌终极版本
*/
public class JavaTest {
@Test
public void test() throws IOException {
Student student = new Student(null, "haha", 18, "dddd", 1);
//读取配置文件
String resource = "mybatis-config.xml";
// 获取资源输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂获取SqlSession 相当于拿到一个连接对象 进行数据库操作
SqlSession sqlSession = sqlSessionFactory.openSession();
int i = sqlSession.insert("com.aaa.mapper.StudentMapper.saveStudent",student);
System.out.println(i);
//mybatis自带的实务操作
sqlSession.commit();
sqlSession.close();
}
@Test
public void test1() throws IOException {
//读取配置文件
String resource = "mybatis-config.xml";
// 获取资源输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂获取SqlSession 相当于拿到一个连接对象 进行数据库操作
SqlSession sqlSession = sqlSessionFactory.openSession();
int i = sqlSession.delete("com.aaa.mapper.StudentMapper.delStudent",3);
System.out.println(i);
//mybatis自带的实务操作
sqlSession.commit();
sqlSession.close();
}
@Test
public void test2() throws IOException {
Student student = new Student(5, "qweqwrqwreq", 19, "beijing", 3);
//读取配置文件
String resource = "mybatis-config.xml";
// 获取资源输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂获取SqlSession 相当于拿到一个连接对象 进行数据库操作
SqlSession sqlSession = sqlSessionFactory.openSession();
int i = sqlSession.update("com.aaa.mapper.StudentMapper.updateStudent",student);
System.out.println(i);
//mybatis自带的实务操作
sqlSession.commit();
sqlSession.close();
}
@Test
public void test3() throws IOException {
//读取配置文件
String resource = "mybatis-config.xml";
// 获取资源输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 通过工厂获取SqlSession 相当于拿到一个连接对象 进行数据库操作
SqlSession sqlSession = sqlSessionFactory.openSession();
// Student s = sqlSession.selectOne("com.aaa.mapper.StudentMapper.getOne",1);
// System.out.println(s);
List<Student> data = sqlSession.selectList("com.aaa.mapper.StudentMapper.listStudent");
System.out.println(data);
//mybatis自带的实务操作
sqlSession.commit();
sqlSession.close();
}
}