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. Linux系统中的硬件问题如何排查?(4)

    Linux系统中的硬件问题如何排查?(4) 2013-03-27 10:32 核子可乐译 51CTO.com 字号:T | T 在Linux系统中,对于硬件故障问题的排查可能是计算机管理领域最棘手的工 ...

  2. C++ Arrays, std::array, std::vector 总结

    原文来自: https://shendrick.net/Coding%20Tips/2015/03/15/cpparrayvsvector.html @Seth Hendrick Original a ...

  3. 网络吞吐量(network)

    题目 分析 过一遍spfa,把从点1到其他每一个点的最短路求出来, 接着递归把所有最短路径上的路径保留,其他的删掉. 对于保留的路径作为网络流的边,流量为无穷大,对于每个点拆点两个点之间的流量为吞吐量 ...

  4. 18.二叉树的镜像(python)

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. class Solution: # 返回镜像树的根节点 def Mirror(self, root): # write code here if ...

  5. 6423. 【NOIP2019模拟11.11】画

    题目描述 Description Input Output Sample Input 3 2 3 3 6 5 1 2 1 3 Sample Output 15 Data Constraint 题解 迫 ...

  6. CSS布局之flexbox

    参考链接: https://www.cnblogs.com/qingchunshiguang/p/8011103.html 练习代码 <!DOCTYPE html> <html la ...

  7. JavaScript的几种循环使用方式及性能解析

    循环的类型 一:for var arr = [1, 2, 3, 4, 5, 6]; for (var i = 0, len = arr.length; i < len; i++) { conso ...

  8. 原生JS获取li中的内容

  9. [POJ]P3126 Prime Path[BFS]

    [POJ]P3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35230   Accepted: ...

  10. 【bzoj4562】[Haoi2016]食物链

    *题目描述: 如图所示为某生态系统的食物网示意图,据图回答第1小题 现在给你n个物种和m条能量流动关系,求其中的食物链条数. 物种的名称为从1到n编号 M条能量流动关系形如 a1 b1 a2 b2 a ...