MyBatis学习笔记(四) 注解
1 package com.cy.mybatis.mapper; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.apache.ibatis.annotations.Delete; 7 import org.apache.ibatis.annotations.Insert; 8 import org.apache.ibatis.annotations.Options; 9 import org.apache.ibatis.annotations.Param; 10 import org.apache.ibatis.annotations.Result; 11 import org.apache.ibatis.annotations.ResultMap; 12 import org.apache.ibatis.annotations.Results; 13 import org.apache.ibatis.annotations.Select; 14 import org.apache.ibatis.annotations.Update; 15 16 import com.cy.mybatis.beans.UserBean; 17 18 public interface UserMapper { 19 // 简单的增删改查可以使用注解 20 // 注解+配置文件 21 22 /** 23 * 新增用戶 24 * @param user 25 * @return 26 * @throws Exception 27 */ 28 @Insert("insert into t_user value (null,user.username,user.password,user.account)") 29 @Options(useGeneratedKeys=true,keyProperty="user.id") 30 public int insertUser(@Param("user")UserBean user) throws Exception; 31 32 33 /** 34 * 修改用戶 35 * @param user 36 * @param id 37 * @return 38 * @throws Exception 39 */ 40 @Update(" update t_user set username=#{u.username},password=#{u.password},account=#{u.account} where id=#{id}") 41 public int updateUser (@Param("u")UserBean user,@Param("id")int id) throws Exception; 42 43 /** 44 * 刪除用戶 45 * @param id 46 * @return 47 * @throws Exception 48 */ 49 @Delete(" delete from t_user where id=#{id} ") 50 public int deleteUser(int id) throws Exception; 51 52 53 /** 54 * 根据id查询用户信息 55 * @param id 56 * @return 57 * @throws Exception 58 */ 59 60 @Select(" select * from t_user where id=#{id}") 61 @Results({ 62 63 @Result(id=true,property="id",column="id",javaType=Integer.class), 64 @Result(property="username",column="username",javaType=String.class), 65 @Result(property="password",column="password",javaType=String.class), 66 @Result(property="account",column="account",javaType=Double.class) 67 }) 68 public UserBean selectUserById(int id) throws Exception; 69 /** 70 * 查询所有的用户信息 71 * @return 72 * @throws Exception 73 */ 74 75 @Select(" select * from t_user") 76 @ResultMap("userMap") 77 public List<UserBean> selectAllUser() throws Exception; 78 79 80 /** 81 * 批量增加 82 * @param user 83 * @return 84 * @throws Exception 85 */ 86 public int batchInsertUser(@Param("users")List<UserBean> user) throws Exception; 87 88 /** 89 * 批量删除 90 * @param list 91 * @return 92 * @throws Exception 93 */ 94 public int batchDeleteUser(@Param("list")List<Integer> list) throws Exception; 95 96 97 /** 98 * 分页查询数据 99 * @param parma 100 * @return 101 * @throws Exception 102 */ 103 public List<UserBean> pagerUser(Map<String, Object> parmas) throws Exception; 104 105 /** 106 * 107 * 分页统计数据 108 * @param parma 109 * @return 110 * @throws Exception 111 */ 112 public int countUser(Map<String, Object> parmas) throws Exception; 113 114 }
UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.cy.mybatis.mapper.UserMapper"> 4 <!-- 自定义返回结果集 --> 5 <resultMap id="userMap" type="UserBean"> 6 <id property="id" column="id" javaType="java.lang.Integer"></id> 7 <result property="username" column="username" javaType="java.lang.String"></result> 8 <result property="password" column="password" javaType="java.lang.String"></result> 9 <result property="account" column="account" javaType="java.lang.Double"></result> 10 </resultMap> 11 12 <!-- 批量操作和foreach标签 --> 13 14 <insert id="batchInsertUser" parameterType="java.util.List"> 15 insert into t_user values 16 <foreach collection="users" item="users" separator=","> 17 (null,#{users.username},#{users.password},#{users.account}) 18 </foreach> 19 </insert> 20 21 22 <delete id="batchDeleteUser"> 23 delete from t_user where id in ( 24 <foreach collection="list" item="list" separator=","> 25 #{id} 26 </foreach> 27 ) 28 </delete> 29 30 <!--collection 为用于遍历的元素(必选),支持数组、List、Set --> 31 <!-- item 表示集合中每一个元素进行迭代时的别名. --> 32 <!--separator表示在每次进行迭代之间以什么符号作为分隔 符. --> 33 34 <!--#在生成SQL时,对于字符类型参数,会拼装引号 35 $在生成SQL时,不会拼装引号,可用于order by之类的参数拼装 36 --> 37 <select id="pagerUser" parameterType="java.util.Map" resultMap="userMap"> 38 select * from t_user where 1=1 39 <if test="username!=null"> 40 and username like '%${username}%' 41 </if> 42 limit ${index},${pageSize} 43 </select> 44 45 <select id="countUser" parameterType="java.util.Map" resultType="int"> 46 select count(*) from t_user where 1=1 47 <if test="username != null"> 48 and username like '%${username}%' 49 </if> 50 </select> 51 52 </mapper>
1 package com.lovo.mybatis.mapper; 2 3 import org.apache.ibatis.annotations.Insert; 4 import org.apache.ibatis.annotations.One; 5 import org.apache.ibatis.annotations.Options; 6 import org.apache.ibatis.annotations.Param; 7 import org.apache.ibatis.annotations.Result; 8 import org.apache.ibatis.annotations.ResultType; 9 import org.apache.ibatis.annotations.Results; 10 import org.apache.ibatis.annotations.Select; 11 12 import com.lovo.mybatis.beans.HusbandBean; 13 import com.lovo.mybatis.beans.WifeBean; 14 15 public interface HusbandMapper { 16 17 /** 18 * 保存丈夫 19 * @param husband 20 * @return 21 */ 22 @Insert("insert into t_husband values (null,#{h.name})") 23 @Options(useGeneratedKeys=true,keyProperty="h.id") 24 public int saveHusband(@Param("h")HusbandBean husband); 25 26 27 /** 28 * 根据ID查询丈夫资料 29 * @param id 30 * @return 31 */ 32 @Select("select * from t_husband where id=#{id}") 33 @ResultType(HusbandBean.class) 34 public HusbandBean findHusbandById(int id); 35 36 37 38 /** 39 * 根据ID查询丈夫与妻子资料 40 * @param id 41 * @return 42 */ 43 @Select("select * from t_husband where id=#{id}") 44 @Results({ 45 @Result(id=true,property="id",column="id",javaType=Integer.class), 46 @Result(property="name",column="name",javaType=String.class), 47 @Result(property="wife",column="id",javaType=WifeBean.class,one=@One(select="com.lovo.mybatis.mapper.WifeMapper.findWifeByHusbandId")) 48 }) 49 public HusbandBean findHusbandAndWife(int id); 50 51 52 }
1 package com.cy.mybatis.mapper; 2 3 import org.apache.ibatis.annotations.ResultType; 4 import org.apache.ibatis.annotations.Select; 5 6 import com.cy.mybatis.beans.WifeBean; 7 8 public interface WifeMapper { 9 10 11 @Select("select * from t_wife where fk_husband_id = #{id}") 12 @ResultType(WifeBean.class) 13 public WifeBean selectWifeByHusbandId(int id)throws Exception; 14 15 }
注意:使用resultType时,一定要保证,你属性名与字段名相同;如果不相同,就使用resultMap 。
MyBatis学习笔记(四) 注解相关推荐
- mybatis学习笔记四(动态sql)
直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity;/*** 此类是: 用户实体类* @author hpc* @2017 ...
- MyBatis多参数传递之Map方式示例——MyBatis学习笔记之十三
前面的文章介绍了MyBatis多参数传递的注解.参数默认命名等方式,今天介绍Map的方式.仍然以前面的分页查询教师信息的方法findTeacherByPage为例(示例源代码下载地址:http://d ...
- MyBatis多参数传递之混合方式——MyBatis学习笔记之十五
在本系列文章的<MyBatis多参数传递之Map方式示例>一文中,网友mashiguang提问如下的方法如何传递参数:public List findStudents(Map condit ...
- 【应用篇】MyBatis学习笔记
MyBatis学习笔记 一 环境配置 1 什么是MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的 JDBC 代码和参 ...
- 超详细Mybatis学习笔记(可供下载)
文章目录 1.简介 2.第一个Mybatis程序 搭建环境 编写代码 测试 3.CRUD(增删改查) 3.1.几个属性 3.2.select 3.3.insert 3.4.delete 3.5.upd ...
- Mybatis学习笔记——第一天
Mybatis学习打卡 Mybatis学习笔记---第一天 Mybatis笔记 第一天 一.引言 1. 现有Jdbc的缺陷 2. Mybatis框架 3. 搭建环境 1) 引入jar包 2) 引入配置 ...
- MySQL高级学习笔记(四)
文章目录 MySQL高级学习笔记(四) 1. MySql中常用工具 1.1 mysql 1.1.1 连接选项 1.1.2 执行选项 1.2 mysqladmin 1.3 mysqlbinlog 1.4 ...
- Mybatis学习笔记(二)【框架基础搭建】
Mybatis框架基础搭建 一.数据库搭建 二.创建一个maven工程 三.在pom.xml中导入依赖 四.创建一个mybatis的核心配置文件 配置连接数据库的字段值文件(如果使用方式一就不需要配置 ...
- MyBatis学习笔记(二)根据配置文件优化
上一篇:MyBatis学习笔记(一)完整查询数据库流程+增删改查 mybatis_config.xml文件中可以包含以下部分 本文接下来的优化都是针对于mybatis_config.xml文件,并且每 ...
最新文章
- C#游戏开发快速入门教程Unity5.5教程
- QCustomplot几种清理曲线数据方法
- Codevs 1689 建造高塔
- 使用share prefernces实现轻量级数据存储
- SpringBoot 集成 阿里的 FastJson
- 6. 同步化器(Synchronizers)
- Creo曲面基础知识
- 大数据分析案例-用RFM模型对客户价值分析(聚类)
- linux安装多路径软件,IBM服务器多路径软件RDAC安装详解
- 心肌损伤的标志物题库【1】
- YUVPlayer: 基于Android平台的YUV视频原始数据播放器
- Mac系统之----教你怎么显示隐藏文件,或者关闭显示隐藏文件
- DC靶机系列------6
- 第十三届蓝桥杯 EDA 设计与开发科目 模拟试题(详细解读)
- java excel 插入新行_excel:插入行更新公式
- 使用Rational Performance Tester实现DB2 性能测试和监控
- Redis 之布隆过滤器,增强版,布谷鸟过滤器
- 高通骁龙845的android手机有哪些,2018年骁龙845手机有哪些?骁龙845手机怎么样?...
- python全栈开发103_python全栈开发day103-python垃圾回收机制、mro和c3算法解析、跨域jsonp\CORS、Content-Type组件...
- 为什么要选择ABBYY FineReader 14