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之简单注解的更多相关文章

  1. MyBatis 使用简单的 XML或注解用于配置和原始映射

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .My ...

  2. MyBatis(1)-简单入门

    简介 什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.My ...

  3. 单独使用MyBatis的简单示例

    单独使用MyBatis的简单示例:mybaties-config.xml:MyBatis配置文件 <?xml version="1.0" encoding="UTF ...

  4. Mybatis基于接口注解配置SQL映射器(二)

    Mybatis之增强型注解 MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码.但是注解对动态SQL的支持一直差强人意,即使My ...

  5. Mybatis基于接口注解配置SQL映射器(一)

    上文已经讲解了基于XML配置的SQL映射器,在XML配置的基础上MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码. Mybat ...

  6. Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!

    前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库 ...

  7. Mybatis的简单增删改查

    刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...

  8. Mybatis的@Options注解

    mybatis的@Options注解能够设置缓存时间,能够为对象生成自增的key 第一个使用场景: 有一个表 CREATE TABLE instance ( instance_id BIGINT UN ...

  9. mybatis 使用@Select 注解,因为字符编码不一致导致mybatis 报错

    使用 mybatis 的@Select 注解, @Select({ "<script>select " + ALL_COLUMNS + " from &quo ...

随机推荐

  1. jenkins+gitlab+robot framework 中遇到的坑

    问题一:拉Git源代码时提示无权限 原来之前用的ssh密钥一直都是自己的用户生成的.其实在Jenkins系统使用的都是Jenkins这个系统帐号的. 解决方法: 切换到jenkins这个帐号下生成个新 ...

  2. [转] 打开 CMD 时自动执行命令

    [转] 打开 CMD 时自动执行命令 问题描述 在Windows中打开一个command-prompt时,我正在寻找一种方法来执行一些控制台命令,特别是设置一些命令别名. 例如,当打开command- ...

  3. 2016级算法第六次上机-G.ModricWang likes geometry

    1116 ModricWang likes geometry 思路 难题,非常考察几何知识,放在这里作为计算几何场次的最难的题. 原题地址 原版题解 代码

  4. jquery json实现面向对象 百度十二星座

    效果: 源码: index.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  5. eclipse首次使用的基本设置

    最近,一些刚开始学习Java的朋友使用eclipse遇到了一些编码导致的问题向我询问,那就总结一下首次安装eclipse后我们大体应该设置哪些基本东西吧~大神们呐就不用看啦. 一.修改工作空间默认编码 ...

  6. Mysql update from

    UPDATE tab1   a INNER JOIN tab_game_version as b ON a.id=b.id SET a.advert_data=0 where a.advert_dat ...

  7. poj3207 Ikki's Story IV - Panda's Trick 2-SAT

    题目传送门 题意:在一个圆上顺时针安放着n个点,给出m条线段连接端点,要求线段不相交,线段可以在圆内也可以在圆外,问是否可以. 思路:假设一条线段,放在圆外是A,放在园内是A',那么两条线段如果必须一 ...

  8. C#方法重载和方法重写的区别

    一.重载的条件: 1.必须在同一个类中: 2.方法名必须相同: 3.参数列表不能相同. 二.重写的条件: 1. 在不同的类中2. 发生方法重写的两个方法返回值,方法名,参数列表必须完全一致(必须具有相 ...

  9. PHPStorm操作小技巧

    1.围绕选中字符输入引号或者括号 2.设置服务器部署 3.隐藏Project快捷键 Shift + Esc 4.IDE内窗口切换 Ctrl + TAB 5.关闭当前项目 File -> Clos ...

  10. 【Kafka】Kafka集群搭建

    一.准备工作 服务器:最好是多台,大于等于2 已经搭建好的zookeeper集群 下载软件kafka_2.11-0.10.0.1.tgz 二.创建目录 #创建目录 cd /opt/ mkdir kaf ...