MyBatis01
MyBatis(原名为iBatis)是一个开源的持久化框架,用于将Java对象映射到关系数据库中。它通过XML或注解的方式配置SQL语句和结果映射,提供了灵活和强大的数据库访问功能。MyBatis提供了SQL的完全控制权限,同时也提供了许多便利的特性,使得数据库操作更加简单和高效。它被广泛应用于Java web开发和企业级应用中,是一个非常受欢迎的框架之一。
1.入门
1.创建一个maven工程 pom文件中导包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
2.创建一个resources文件夹 并make
创建一个 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>
<!-- 环境们 其实就是配置数据源信息等环境 default="development" 表明当前使用的是哪一个环境-->
<environments default="development">
<!-- 其中的一个环境-->
<environment id="development">
<!-- mybatis自带的事务操作 type="JDBC" jdbc是别名 代表mybatis写好的一个事务类-->
<transactionManager type="JDBC"/>
<!-- 数据源 type="POOLED" 也是一个别名 是mybatis自带的数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
3.在文件夹下创建一个mapper文件夹并创建StudentMapper.xml文件
<?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.aaa.mapper.StudentMapper">
</mapper>
2.使用mybatis实现增删改查
StudentMapper.xml
<?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.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();
}
}
3.sql语句传参CRUD
创建学生实体类
@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
<?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.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();
}
}