MyBatis入门_下
发布日期:2022-02-10 11:36:55 浏览次数:48 分类:技术文章

本文共 7684 字,大约阅读时间需要 25 分钟。

MyBatis总结参考:

MyBatis 注解开发

常用注解介绍
  • 我们除了可以使用映射配置文件来操作以外,还可以使用注解形式来操作
  • 常用注解
    • @Select(“查询的 SQL 语句”):执行查询操作注解
    • @Insert(“新增的 SQL 语句”):执行新增操作注解
    • @Update(“修改的 SQL 语句”):执行修改操作注解
    • @Delete(“删除的 SQL 语句”):执行删除操作注解
注解实现CURD操作
  • 创建接口和方法

    package com.xxxxxxx.mapper;import com.xxxxxxx.bean.Student;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;public interface StudentMapper {
    //查询全部 @Select("SELECT * FROM student") public abstract List
    selectAll(); //新增操作 @Insert("INSERT INTO student VALUES (#{id},#{name},#{age})") public abstract Integer insert(Student stu); //修改操作 @Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}") public abstract Integer update(Student stu); //删除操作 @Delete("DELETE FROM student WHERE id=#{id}") public abstract Integer delete(Integer id);}
  • 在核心配置文件中配置映射关系

  • 编写测试类

    package com.xxxxxxx.test;import com.xxxxxxx.bean.Student;import com.xxxxxxx.mapper.StudentMapper;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.*;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(); } }
  • 注解单表操作小结

    • 注解可以简化开发操作,省略映射配置文件的编写(结合实际需求使用)。

    • 配置映射关系

      在这里插入图片描述

MyBatis 注解实现多表操作

一对一
/*准备工作*//*省略核心配置文件和映射关系的配置,映射关系的配置入上图*//*实体类Card,省略其他重复代码*/public class Card {
private Integer id; //主键id private String number; //身份证号 private Person p; } //所属人的对象/*实体类Person,省略其他重复代码*/public class Person {
private Integer id; //主键id private String name; //人的姓名 private Integer age; } //人的年龄

CardMapper接口

package com.xxxxxxx.one_to_one;import com.xxxxxxx.bean.Card;import com.xxxxxxx.bean.Person;import org.apache.ibatis.annotations.One;import org.apache.ibatis.annotations.Result;import org.apache.ibatis.annotations.Results;import org.apache.ibatis.annotations.Select;import java.util.List;public interface CardMapper {
//查询全部 @Select("SELECT * FROM card") @Results({
@Result(column = "id",property = "id"), @Result(column = "number",property = "number"), @Result( property = "p", // 被包含对象的变量名 javaType = Person.class, // 被包含对象的实际数据类型 column = "pid", // 根据查询出的card表中的pid字段来查询person表 /* one、@One 一对一固定写法 select属性:指定调用哪个接口中的哪个方法 */ one = @One(select = "com.itheima.one_to_one.PersonMapper.selectById") ) }) public abstract List
selectAll();}

PersonMapper接口

package com.xxxxxxx.one_to_one;import com.xxxxxxx.bean.Person;import org.apache.ibatis.annotations.Select;public interface PersonMapper {
//根据id查询 @Select("SELECT * FROM person WHERE id=#{id}") public abstract Person selectById(Integer id);}
  • @Results:封装映射关系的父注解。
    • Result[] value():定义了 Result 数组
  • @Result:封装映射关系的子注解。
    • column 属性:查询出的表中字段名称
    • property 属性:实体对象中的属性名称
    • javaType 属性:被包含对象的数据类型
    • one 属性:一对一查询固定属性
  • @One:一对一查询的注解。
    • select 属性:指定调用某个接口中的方法

测试列类

package com.xxxxxxx.one_to_one;import com.xxxxxxx.bean.Card;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.获取CardMapper接口的实现类对象 CardMapper mapper = sqlSession.getMapper(CardMapper.class); //5.调用实现类对象中的方法,接收结果 List
list = mapper.selectAll(); //6.简单处理结果 for (Card card : list) {
System.out.println(card); } //7.释放资源 sqlSession.close(); is.close(); }}
一对多

准备工作和一对一一样的配置

ClassesMapper

package com.xxxxxxx.one_to_many;import com.xxxxxxx.bean.Classes;import org.apache.ibatis.annotations.Many;import org.apache.ibatis.annotations.Result;import org.apache.ibatis.annotations.Results;import org.apache.ibatis.annotations.Select;import java.util.List;public interface ClassesMapper {
//查询全部 @Select("SELECT * FROM classes") @Results({
@Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result( property = "students", // 被包含对象的变量名 javaType = List.class, // 被包含对象的实际数据类型 column = "id", // 根据查询出的classes表的id字段来查询student表 /* many、@Many 一对多查询的固定写法 select属性:指定调用哪个接口中的哪个查询方法 */ many = @Many(select = "com.itheima.one_to_many.StudentMapper.selectByCid") ) }) public abstract List
selectAll();}

StudentMapper接口

package com.xxxxxxx.one_to_many;import com.xxxxxxx.bean.Student;import org.apache.ibatis.annotations.Select;import java.util.List;public interface StudentMapper {
//根据cid查询student表 @Select("SELECT * FROM student WHERE cid=#{cid}") public abstract List
selectByCid(Integer cid);}
  • 环境准备

  • @Results:封装映射关系的父注解。

  • Result[] value():

    • 定义了 Result 数组
  • @Result:封装映射关系的子注解。

    • column 属性:查询出的表中字段名称
    • property 属性:实体对象中的属性名称
    • javaType 属性:被包含对象的数据类型
    • many 属性:一对多查询固定属性
  • @Many:一对多查询的注解。

    • select 属性:指定调用某个接口中的方法

测试类

public class Test01 {
@Test public void selectAll() throws Exception{
// 步骤和一对一大致是一样的,这是获取的实现类对象不一样,调用的方法也不一样 //4.获取ClassesMapper接口的实现类对象 ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class); //5.调用实现类对象中的方法,接收结果 List
list = mapper.selectAll(); }}
多对多

和一对多大致是一样的

MyBatis 构建 SQL 语句

  • 我们之前通过注解开发时,相关 SQL 语句都是自己直接拼写的。一些关键字写起来比较麻烦、而且容易出错。

  • MyBatis 给我们提供了 org.apache.ibatis.jdbc.SQL 功能类,专门用于构建 SQL 语句。

    方法名 说明
    SELECT(String…column) 根据字段拼接查询语句
    FROM(String…table) 根据表名拼接语句
    WHERE(String…condition) 根据条件拼接语句
    INSERT_INTO(String table) 根据表名拼接新增语句
    VALUES(String column,String values) 根据字段和值拼接插入数据语句
    UPDATE(String table) 根据表名拼接修改语句
    DELETE_FROM(String table) 根据表名拼接删除语句

    例如

    package com.itheima.sql;import com.itheima.bean.Student;import org.apache.ibatis.jdbc.SQL;public class ReturnSql {
    //定义方法,返回查询的sql语句 public String getSelectAll() {
    return new SQL() {
    {
    SELECT("*"); FROM("student"); } }.toString(); } //省略其他方法,大都一致}

转载地址:https://blog.csdn.net/drug1910951117/article/details/106888022 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:关于spring配置文件中不能使用&的问题
下一篇:JavaScript快速入门-基础

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年03月31日 08时49分40秒