关键词:
使用MyBatis注解开发,可以省去类配置文件,简洁方便。但是比较复杂的SQL和动态SQL还是建议书写类配置文件。
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,[email protected](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学习笔记:分页使用注解开发(代码片段)
...ize缺省写法:默认从零开始select*fromuserlimitpageSize使用mybatis实现分页,核心SQL1.接口//分页List<User>ge 查看详情
springboot学习笔记-整合mybatis
Springboot学习笔记(二)- 整合MyBatis SpringBoot中整合MyBatis,并通过注解方式实现映射。整合MyBatis以Springboot学习笔记(一)-Helloworld 为基础项目,在pom.xml中添加如下依赖 <dependency> <groupId>org.mybatis.spring.b... 查看详情
mybatis学习笔记-04(代码片段)
mybatis学习笔记-041.设置日志1.1默认日志1.2LOG4J2.分页2.1limit分页2.2RowBounds分页3.lombok使用方法4.使用注解开发使用注解进行CRUD1.设置日志日志可以方便我们排错,默认是不开启的,开启需要在核心配置文件中设置。name和value... 查看详情
mybatis学习笔记-04(代码片段)
mybatis学习笔记-041.设置日志1.1默认日志1.2LOG4J2.分页2.1limit分页2.2RowBounds分页3.lombok使用方法4.使用注解开发使用注解进行CRUD1.设置日志日志可以方便我们排错,默认是不开启的,开启需要在核心配置文件中设置。name和value... 查看详情
2021-08-13-mybatis学习笔记(代码片段)
Mybatis学习笔记1.增删改查(修改dao中接口类,xml和test类)1.1namespace1.2select.insert.update.delete.2.使用注解开发2.1面向接口编程:解耦2.2注解开发1.增删改查(修改dao中接口类,xml和test类)整个过程:1.编写接口2.... 查看详情
mybatis-plus工具学习笔记---[常用注解,条件构造器,插件使用](代码片段)
文章目录1.常用注解1.1@TableName全局配置1.2@TableId全局配置1.3雪花算法介绍1.4@TableField1.5@TableLogic2.条件构造器2.1组装查询条件案例2.2组装排序案例2.3组装删除条件案例2.4修改功能案例2.5条件的优先级设置2.6组装select精确... 查看详情
mybatis学习笔记
1. Mybatis介绍(ibatis)MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将... 查看详情
mybatis学习笔记三(关联关系)
学习mybatis的关联关系,主要注解在代码上,这里不做解释。配置文件一样的就不贴了1.关联关系表创建(学生对应老师多对一) 学生老师表 2.表对应的实体类packagecom.home.entity;/***此类是:学生*@authorhpc*@2017年1月1... 查看详情
mybatis学习笔记(一)入门
一、理解什么是MyBatis?MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索。 MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口... 查看详情
mybatis-plus工具学习笔记---[常用注解,条件构造器,插件使用](代码片段)
...件案例2.11LambdaQueryWrapper案例2.12LambdaUpdateWrapper使用案例3.Mybatis-Plus分页插件分页数据获取自定义分页功能4.乐观锁悲观锁模拟修改冲突乐观锁插件5.通用枚举6代码生成器7.mybatis-X插件生成代码近期也是计划学习mybatis-plus,扩展知识;... 查看详情
mybatis学习笔记——mybatis快速入门
一、Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将... 查看详情
mybatisplus学习笔记(代码片段)
文章目录MyBatisPlus学习笔记简介特性框架结构快速使用引入mybatis-plus相关maven依赖引入mybatis-plus在springboot中的场景启动器项目中其他需要导入依赖创建表SQL语句项目结构一览项目配置配置MapperScan注解application.properties配置创建Entit... 查看详情
springboot+mybatis学习笔记(代码片段)
Springboot+Mybatis实战笔记目录结构1.controller层:控制层-->调用业务层方法来控制业务逻辑2.service层3.dao层:持久层,主要与数据库交互4.entity层:实体层-->数据库在项目中的类5.resources下的mapper.userMap6.controller层和service层注解... 查看详情
mybatis学习笔记(一)for概念
mybaits相关概念1.1mybatis简介mybatis是是一款优秀的基于ORM的半自动轻量级持久层框架,它支持定制化SQL、存储过程以及高级映射。(与另一基于ORM的持久层框架hibernate区别是,hibernate是全自动的,可以完全不写sql语句。)mybatis避... 查看详情
mybatis学习笔记二:mybatis生产中使用环境搭建
这里是在上一个环境的基础上修改的,这里就不在给出所有的配置,只给出哪里修改的配置1.修改POJO对象为注解方式 2.创建Dao层接口packagecom.orange.dao;importcom.orange.model.Person;publicinterfacePersonDao{//这里的返回值和方法名必须和PersonM... 查看详情
mybatis学习笔记总结(代码片段)
文章目录1.Mybatis生命周期和作用域2.解决实体类的属性名和数据库的字段名不一致的问题3.resultMap结果集映射4.日志4.1日志工程4.2LOG4J日志输出5.分页5.1limit分页5.2RowBounds分页(了解)5.3分页插件6.使用注解开发6.1面向接口编程6.2Mybatis... 查看详情
mybatis框架入门到精通-入门必备的学习笔记
MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java... 查看详情
mybatis学习笔记-04(代码片段)
mybatis学习笔记-041.设置日志1.1默认日志1.2LOG4J2.分页2.1limit分页2.2RowBounds分页3.lombok使用方法4.使用注解开发使用注解进行CRUD1.设置日志日志可以方便我们排错,默认是不开启的,开启需要在核心配置文件中设置。name和value... 查看详情