Mybatis之简单注解
Mybatis使用注解实现主键自增长:
oracle:
@SelectKey(statement="select my_seq.nextval from dual",resultType=int.class,keyProperty="id",before=true)
statement是生成id语句,resultType是语句返回类型,keyProperty是填入id列,before是先写入对象,再写入表
mysql:
@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
@Results:结果映射
@Results注解和映射器XML配置文件元素<resultMap> 对应。
@Select("select id as pid,name,age from person")
@Results({
@Result(id=true,property="id",column="pid"),
@Result(property="name",column="name"),
@Result(property="age",column="age")
})
public List<Person> selectAllPerson_ResultMap();
resultMap可以重用,@Results不可以重用。
解决方法:
创建一个映射器Mapper配置文件,然后配置<resultMap>元素,然后使用 @ResultMap注解引用此<resultMap>
@ResultMap(【命名空间名】.【ResultMap_id】)
@One注解:
一对一关联查询
@Select("select addr_id as addrId, street, city, state, zip, country from addresses where addr_id=#{id}")
Address findAddressById(int id);
@Select("select * from students where stud_id=#{studId} ")
@Results(
{
@Result(id = true, column = "stud_id", property = "studId"),
@Result(column = "name", property = "name"),
@Result(column = "email", property = "email"),
@Result(property = "address", column = "addr_id",
one = @One(select = "com.briup.mapper.StudentMapper.findAddressById"))
})
Student selectStudentWithAddress(int studId);
@Many注解:
一对多关联查询
@Select("select addr_id as addrId, street, city, state, zip,
country from addresses where addr_id=#{id}")
Address findAddressById(int id);
@Select("select * from courses where tutor_id=#{tutorId}")
@Results(
{
@Result(id = true, column = "course_id", property = "courseId"),
@Result(column = "name", property = "name"),
@Result(column = "description", property = "description"),
@Result(column = "start_date" property = "startDate"),
@Result(column = "end_date" property = "endDate")
})
List<Course> findCoursesByTutorId(int tutorId);
@Select("SELECT tutor_id, name as tutor_name, email, addr_id
FROM tutors where tutor_id=#{tutorId}")
@Results(
{
@Result(id = true, column = "tutor_id", property = "tutorId"),
@Result(column = "tutor_name", property = "name"),
@Result(column = "email", property = "email"),
@Result(property = "address", column = "addr_id",
one = @One(select = "com.briup.mappers.Tutor Mapper.findAddressById")),
@Result(property = "courses", column = "tutor_id",
many = @Many(select = "com.briup.mappers.Tutor Mapper.findCoursesByTutorId"))
})
Tutor findTutorById(int tutorId);
@mapper注解:
把mapper这个DAO交给spring去管理
不在写mapper的映射文件
Mybatis之简单注解的更多相关文章
- MyBatis 使用简单的 XML或注解用于配置和原始映射
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .My ...
- MyBatis(1)-简单入门
简介 什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.My ...
- 单独使用MyBatis的简单示例
单独使用MyBatis的简单示例:mybaties-config.xml:MyBatis配置文件 <?xml version="1.0" encoding="UTF ...
- Mybatis基于接口注解配置SQL映射器(二)
Mybatis之增强型注解 MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码.但是注解对动态SQL的支持一直差强人意,即使My ...
- Mybatis基于接口注解配置SQL映射器(一)
上文已经讲解了基于XML配置的SQL映射器,在XML配置的基础上MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码. Mybat ...
- Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!
前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库 ...
- Mybatis的简单增删改查
刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...
- Mybatis的@Options注解
mybatis的@Options注解能够设置缓存时间,能够为对象生成自增的key 第一个使用场景: 有一个表 CREATE TABLE instance ( instance_id BIGINT UN ...
- mybatis 使用@Select 注解,因为字符编码不一致导致mybatis 报错
使用 mybatis 的@Select 注解, @Select({ "<script>select " + ALL_COLUMNS + " from &quo ...
随机推荐
- 0基础浅谈反射型xss (1)
0X1:在学习xss之前,先快速学习相关的HTML代码 1. <input>标签 文本域用法: <input type="text" /> Type的作 ...
- Instant low voltage or power off to make computer power burn down
严重则可造成硬盘直接报废! 原理:瞬间低压或者断电,滤波电容上存储的电能已经被使用,此时再瞬间供电则会重新对电容充电,而限流电阻还没有恢复到保护状态,于是会产生很大的冲击电流,从而导致了全桥元件或保险 ...
- 记一次在CentOS系统搭建python3环境
首先,查看linux系统类型和版本:参考:查看linux系统类型和版本 默认Centos7中是有python安装的,但是是2.7版本,实际上这几个文件之间是有依赖关系的.在ls 后面加个 -al参数, ...
- max,min无法使用的问题
遇到了包含algorithm头文件以后 min或max函数不能用的问题 改成__min和__max以后就可以了
- .NET Core容器化之多容器应用部署-使用Docker-Compose
原文补充: -- docker-compose.ymlversion: ' services: mvc-web: container_name: mvc.web.compose build: . re ...
- finally语句块一定会执行吗?
public class SystemExitAndFinally { public static void main(String[] args) { try{ System.out.println ...
- MySQL数据库的账户管理
账户管理 在生产环境下操作数据库时,绝对不可以使用root账户连接,而是创建特定的账户,授予这个账户特定的操作权限,然后连接进行操作,主要的操作就是数据的crud MySQL账户体系:根据账户所具有的 ...
- 工具篇-大数据组件的一些快捷Shell操作
一.Hbase 1. HBase shell窗口进入 执行hbase shell 2. HBase表的创建 # 语法:create <table>, {NAME => <fam ...
- 一个比较强大的HTTP请求类,支持文本参数和文件参数。
一个 http 请求类 ,支持文件上传,从淘宝 top sdk 里面扣出来的,蛮好用的,做个记录而已. 调用代码: Dictionary<string, string> textParas ...
- DB2 体系结构 (进程模型)
DB2 是众多关系型数据库中的一种, 关系型数据库还包括比较火的Oracle,MySQL 实例 数据库 DB2 进程模型 DB2 通过 db2start 命令启动数据库实例,即启动相应的进程和线程,并 ...