常用注解:
@Select(“查询的 SQL 语句”):执行查询操作注解
@Insert(“查询的 SQL 语句”):执行新增操作注解
@Update(“查询的 SQL 语句”):执行修改操作注解
@Delete(“查询的 SQL 语句”):执行删除操作注解
1.创建接口和查询方法
2.在核心配置文件中配置映射关系
3.编写测试类
例:
bean.Student:
- package Mybatis4.bean;
-
- public class Student {
- private Integer id;
- private String name;
- private Integer age;
-
- public Student() {
- }
-
- public Student(Integer id, String name, Integer age) {
- this.id = id;
- this.name = name;
- this.age = age;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
Mapper接口 :
- package Mybatis4.mapper;
-
- import Mybatis4.bean.Student;
- import org.apache.ibatis.annotations.Select;
-
- import java.util.List;
-
- public interface StudentMapper {
- //查询全部
- @Select("SELECT * FROM student")
- public abstract List
selectAll(); - }
MyBatisConfig.xml :
- <mappers>
- <package name="Mybatis4.mapper"/>
- mappers>
-
测试类:
- package Mybatis4.test;
-
- import Mybatis4.bean.Student;
- import Mybatis4.mapper.StudentMapper;
- 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.InputStream;
- import java.util.List;
-
- public class Test01 {
- @Test
- public void selectAll() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过工厂对象获取SqlSession
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- //4.获取StudentMapper接口的实现类对象
- StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
- //5.调用实现类的方法,接收结果
- List
list = mapper.selectAll(); - //6.处理结果
- for (Student student : list) {
- System.out.println(student);
- }
- //7.释放资源
- sqlSession.close();
- is.close();
- }
- }
1.创建新增方法
2.编写测试类
Mapper接口:
- //新增操作: sql的参数与之前的写法一致,从insert方法的参数中获取对应属性值
- @Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")
- public abstract Integer insert(Student stu);
测试类:
- @Test
- public void insert() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过工厂对象获取SqlSession
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- //4.获取StudentMapper接口的实现类对象
- StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
- //5.调用实现类的方法,接收结果
- Student stu = new Student(4,"赵六",26);
- Integer result = mapper.insert(stu);
- //6.处理结果
- System.out.println(result);
- //7.释放资源
- sqlSession.close();
- is.close();
- }
1.创建修改方法
2.编写测试类
Mapper接口:
- //修改操作
- @Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")
- public abstract Integer update(Student stu);
测试类:
- @Test
- public void update() throws Exception{
- InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
-
- Student stu = new Student(4,"赵六",36);
- Integer result = mapper.update(stu);
- System.out.println(result);
-
- sqlSession.close();
- is.close();
- }
1.创建删除方法
2.编写测试类
Mapper接口:
- //删除操作
- @Delete("DELETE FROM student WHERE id=#{id}")
- public abstract Integer delete(Integer id);
测试类:
- @Test
- public void delete() throws Exception{
- InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
-
- Integer result = mapper.delete(4);
- System.out.println(result);
-
- sqlSession.close();
- is.close();
- }
注:映射配置关系
- <mappers>
- <package name="接口所在包"/>
- mappers>