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. robot framework学习笔记之一 资源文件(Resource)和外部资源(External Resources)

    一.资源文件(Resource) 测试套件主要是存放测试案例,资源文件主要是用来存放用户关键字. 添加资源    在目录型的Project/Test Suite下单击鼠标右键,选择『New Resou ...

  2. rpm 卸载

    卸载:python-urlgrabber-3.9.1-9.el6.noarch rpm -e python-urlgrabber-3.9.1-9.el6.noarch

  3. java使用Redis6--sentinel单点故障主从自动切换

    Redis SentinelSentinel(哨兵)是用于监控redis集群中Master状态的工具,其已经被集成在redis2.4+的版本中 一.Sentinel作用:1):Master状态检测 2 ...

  4. drools入门示例

    Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效.随着互联网金融的兴 ...

  5. leetcode-78-子集(用bfs解决)

    题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1] ...

  6. QuantLib 金融计算——基本组件之 Calendar 类

    目录 QuantLib 金融计算--基本组件之 Calendar 类 Calendar 对象的构造 一些常用的成员函数 自定义假期列表 工作日修正 如果未做特别说明,文中的程序都是 Python3 代 ...

  7. elment 中 el-table 进行校验

    脑洞大开:什么是展示数据最好的方式呢,表格,写得又快,又清晰,又明显,那么就积累一些工作中表格经常使用到的东西. 第一步:效果图: 第二步:举个例子: <template> <div ...

  8. easy-mock本地部署成功,访问报错:EADDRNOTAVAIL 0.0.0.0:7300 解决方案

    easy-mock本地部署成功后,迫不及待的想看看是否能正常访问,执行命令 npm run dev 启动项目,访问 127.0.0.1:7300 ,结果郁闷的是报错:EADDRNOTAVAIL 0.0 ...

  9. Scrapyd-Client的安装

    1. pip安装 这里推荐使用pip安装,相关命令如下: pip3 install scrapyd-client 2.验证安装 安装成功后会有一个可用命令,叫作scrapyd-deploy,即部署命令 ...

  10. js实现瀑布流布局

    window.onload = function () { var d1 = new Waterfall(); d1.init();};//构造函数function Waterfall() { thi ...