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 ListselectAll();}
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.调用实现类对象中的方法,接收结果 Listlist = 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 ListselectAll();}
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 ListselectByCid(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.调用实现类对象中的方法,接收结果 Listlist = 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年03月31日 08时49分40秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
VS中定义的DEBUG常量
2019-04-27
C#中warning预处理命令的使用
2019-04-27
C#中error预处理命令的使用
2019-04-27
MATLAB中限值模块的使用
2019-04-27
树莓派显示黑边问题解决
2019-04-27
树莓派的硬件信息了解与思考
2019-04-27
树莓派安装截图工具
2019-04-27
树莓派上创建个人用户
2019-04-27
树莓派CPU超频配置
2019-04-27
树莓派终端背景色调节
2019-04-27
树莓派U盘挂载位置
2019-04-27
对比Ubuntu与Win10的资源占用
2019-04-27
树莓派硬件启动失败log记录查看方法
2019-04-27
树莓派使用心得
2019-04-27
树莓派实现无显示器远程登录
2019-04-27
Samba服务实现树莓派与Windows之间的文件共享
2019-04-27
wiringpi安装编译问题解决
2019-04-27
Windows上创建Emacs配置文件
2019-04-27
编写并运行第一个Lisp程序
2019-04-27
VS code中godoc命令不可用问题解决
2019-04-27