1、常用的注解。

2、@insert、@delete、@update、@select完成常见的CRUD操作。

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.lfy.bean.User; public interface UserMapper { @Insert("INSERT INTO TB_USER(name,sex,age) VALUES(#{name},#{sex},#{age})")
@Options(useGeneratedKeys=true,keyProperty="id")
// @SelectKey(before=false,keyProperty="id",resultType=Integer.class,
// statement="SELECT LAST_INSERT_ID() AS id")
int saveUser(User user); //@Param指定参数的名称,如果没有,则按顺序对应语句中的参数
@Delete("DELETE FROM TB_USER WHERE id = #{id}")
int removeUser(@Param("id") Integer id); @Update("UPDATE TB_USER SET name = #{name},sex = #{sex},age = #{age} WHERE id = #{id}")
void modifyUser(User user); //如果属性列的名称一致,可以省略@Result
@Select("SELECT * FROM TB_USER WHERE id = #{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="sex",property="sex"),
@Result(column="age",property="age")
})
User selectUserById(@Param("id") Integer id); @Select("SELECT * FROM TB_USER ")
List<User> selectAllUser(); }
public class InsertTest {

    public static void main(String[] args) {
// 定义SqlSession变量
SqlSession sqlSession = null;
try {
// 创建SqlSession实例
sqlSession = FKSqlSessionFactory.getSqlSession(); // 创建UserMapper实例
UserMapper um = sqlSession.getMapper(UserMapper.class);
// 创建User对象并设置属性
User user = new User();
user.setName("test");
user.setSex("男");
user.setAge(18);
// 插入数据
um.saveUser(user);
// 查看插入数据生成的主键
System.out.println("插入数据生成的主键id为:" + user.getId()); // 提交事务
sqlSession.commit();
} catch (Exception e) {
// 回滚事务
sqlSession.rollback();
e.printStackTrace();
}finally {
// 关闭SqlSession
if(sqlSession != null)
sqlSession.close();
}
} }

2、1对1。

public interface CardMapper {

    @Select("SELECT * FROM TB_CARD WHERE ID = #{id} ")
Card selectCardById(Integer id); }
public interface PersonMapper {

    //TB_PERSON表中有个字段card_id
@Select("SELECT * FROM TB_PERSON WHERE ID = #{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="sex",property="sex"),
@Result(column="age",property="age"),
@Result(column="card_id",property="card",
one=@One(
select="com.lfy.mapping.CardMapper.selectCardById",
fetchType=FetchType.EAGER))
})
Person selectPersonById(Integer id); }

one属性表示是一对一关联关系,@One注解的select属性表示需要关联执行的SQL语句,fetchType表示查询的类型是立即加载还是懒加载。

3、1对多

public interface StudentMapper {

    // 根据班级id查询班级所有学生
@Select("SELECT * FROM TB_STUDENT WHERE CLAZZ_ID = #{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="sex",property="sex"),
@Result(column="age",property="age")
})
List<Student> selectByClazzId(Integer clazz_id);
}
public interface ClazzMapper {

    // 根据id查询班级信息
@Select("SELECT * FROM TB_CLAZZ WHERE ID = #{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="code",property="code"),
@Result(column="name",property="name"),
@Result(column="id",property="students",
many=@Many(
select="com.lfy.mapping.StudentMapper.selectByClazzId",
fetchType=FetchType.LAZY))
})
Clazz selectById(Integer id);
}

column="id"表示会将id作为查询条件,传递到查询班级下所有学生的查询中。many属性表示是一对多关联关系

4、多对多

订单只会属于某一用户,订单对用户是1对1关系;订单里面会有很多商品,一个订单对商品是1对多的关系。所以查询订单信息利用上面两种关联关系组合查询。

public interface OrderMapper {

    @Select("SELECT * FROM TB_ORDER WHERE ID = #{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="code",property="code"),
@Result(column="total",property="total"),
@Result(column="user_id",property="user",
one=@One(select="com.lfy.mapping.UserMapper.selectById",
fetchType=FetchType.EAGER)),
@Result(column="id",property="articles",
many=@Many(select="com.lfy.mapping.ArticleMapper.selectByOrderId",
fetchType=FetchType.LAZY))
})
Order selectById(Integer id); }

5、动态SQL

比较繁琐,不够直观简便,暂放弃整理。

Mybatis-学习笔记(8)常用的注解的更多相关文章

  1. mybatis学习笔记二(接口注解)

    直接上代码,全部在代码里讲解. 1.实体类 package com.home.entity; /** * 此类是:user实体类 * @author hpc * @2017年1月10日下午9:36:5 ...

  2. mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现

    项目结构  基础入门可参考:mybatis学习笔记(一)-- 简单入门(附测试Demo详细过程) 开始体验 1.新建项目,新建类MybatisUtil.java,路径:src/util/Mybatis ...

  3. 【MyBatis学习笔记】

    [MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...

  4. MyBatis:学习笔记(1)——基础知识

    MyBatis:学习笔记(1)--基础知识 引入MyBatis JDBC编程的问题及解决设想 ☐ 数据库连接使用时创建,不使用时就释放,频繁开启和关闭,造成数据库资源浪费,影响数据库性能. ☐ 使用数 ...

  5. mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)

    下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...

  6. mybatis学习笔记--常见的错误

    原文来自:<mybatis学习笔记--常见的错误> 昨天刚学了下mybatis,用的是3.2.2的版本,在使用过程中遇到了些小问题,现总结如下,会不断更新. 1.没有在configurat ...

  7. mybatis 学习笔记(三):mapper 代理开发 dao 层

    mybatis 学习笔记(三):mapper 代理开发 dao 层 优势 通过使用mapper 代理,我们可以不需要去编写具体的实现类(使用 getMapper() 方法自动生成),只需编写接口即可, ...

  8. mybatis 学习笔记(一):mybatis 初认识

    mybatis 学习笔记(一):mybatis 初认识 简介 MyBatis是一个Java持久层框架,它通过XML描述符或注解把对象与存储过程或SQL语句关联起来.mybatis 可以将 prepar ...

  9. Mybatis学习笔记导航

    Mybatis小白快速入门 简介 本人是一个Java学习者,最近才开始在博客园上分享自己的学习经验,同时帮助那些想要学习的uu们,相关学习视频在小破站的狂神说,狂神真的是我学习到现在觉得最GAN的老师 ...

  10. Mybatis学习笔记(二) 之实现数据库的增删改查

    开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...

随机推荐

  1. python类库26[sqlite]

    一 sqlite 与 python 的类型对应 二 实例 import sqlite3 def sqlite_basic():     # Connect to db     conn = sqlit ...

  2. 系统命令模块subprocess

    系统命令 可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen* --废弃 popen2.* --废弃 commands.* --废弃,3.x中被移除 ...

  3. java打分系统

    package com.ioTest; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fil ...

  4. Eclipse修改背景颜色(豆沙绿)

    操作界面默认颜色为白色.对于我们长期使用电脑编程的人来说,白色很刺激我们的眼睛,所以我经常会改变workspace的背景色,使眼睛舒服一些.设置方法如下: 1.打开window->Prefere ...

  5. Let Us Adore 让我们来敬拜祂 中文歌词

      Verse 1 诸天宣告 神的荣耀 万国万民 都将赞美 宣扬祂奇妙 The heavens declare The glory of God And all of the world Will j ...

  6. js 中使用typeof

    >typeof(null) <"object" 对null执行typeof预算,结果返回字符串'object',也就是说,可以将null认为是一个特殊的对象值,含义是“ ...

  7. Python 爬虫十六式 - 第一式:HTTP协议

    HTTP:伟大而又无闻的协议 学习一时爽,一直学习一直爽!   Hello,大家好啊,我是Connor,一个从无到有的技术小白.有的人一说什么是HTTP协议就犯愁,写东西的时候也没想过什么是HTTP协 ...

  8. BZOJ 4245: [ONTAK2015]OR-XOR 贪心 + 位运算

    Description 给定一个长度为n的序列a[1],a[2],...,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的异或和,则总费用为c[1] or c[2] or ...

  9. 【Python】学习笔记十四:循环进阶

    range() 在Python中,for循环后的in跟随一个序列的话,循环每次使用的序列元素,而不是序列的下标. 我们继续开发range的功能,以实现下标对循环的控制: s = 'abcdefghj' ...

  10. 8.并发编程--多线程通信-wait-notify-模拟Queue

    并发编程--多线程通信-wait-notify-模拟Queue 1. BlockingQueue 顾名思义,首先是一个队列,其次支持阻塞的机制:阻塞放入和获取队列中的数据. 如何实现这样一个队列: 要 ...