mybatis select 返回值long null_Mybatis框架(二)
【31】
1. <typeAliases>标签
1.1 功能
用于给 java 类型定义别名, 方便在配置文件中使用.
1.2 使用方式
a) 给 User 类型定义别名为 u
<!-- typeAliases给类型起别名 -->
<typeAliases>
<!-- 给User类起别名为u -->
<typeAlias type="com.bjsxt.pojo.User" alias="u" />
</typeAliases>
b)<typeAlias>中, 可以省略 alias 属性, 表示类别名为类名, 大小写不敏感
<typeAliases>
<!-- 给User类起别名, 别名为user -->
<typeAlias type="com.bjsxt.pojo.User" />
</typeAliases>
c)可以通过<package>给整个包下的所有类定义别名为类名
<typeAliases>
<!-- 给包下的所有类定义别名为类名 -->
<package name="com.bjsxt.pojo" />
</typeAliases>
1.3 MyBatis 的内建别名
1. 带参数的查询
如果执行的是条件查询, 需要在调用方法时传参数进来, 此时, 可以在 select 标签中通过 parameterType 属性指定参数的类型. 而在 SQL 语句中, 可以通过#{}的方式获取参数.
1.1 一个参数的查询
例如, 根据 id 查询用户信息. 当只有一个参数时, #{}中可以任意填写.
<!-- parameterType, 参数类型, 用于参数的传递 -->
<select id="selById" resultType="user" parameterType="int">
<!--
#{}用于获取参数
index, 索引, 从0开始
param+数字, param1, param2
-->
select * from t_user where id=#{param1}
</select>
测试代码:
@Test
public void selById() {SqlSession session = null;
try {session = new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsStream("mybatis-cfg.xml"))
.openSession();
User user =
session.selectOne("com.bjsxt.mapper.UserMapper.selById", 2);
System.out.println(user);
} catch (IOException e) {e.printStackTrace();
} finally {session.close();
}
}
1.2 多个参数的查询
多个参数传递时, 由于 sqlSession 中提供的查询方法只允许传入一个参数, 因此可以对多个参数进行封装. 可以使用对象或 Map 集合.
1.2.1 封装为对象
<select id="sel" resultType="user" parameterType="user">
<!-- 如果参数是对象, 可以通过#{属性名}来获取 -->
select * from t_user where username=#{username} and
password=#{password}
</select>
测试代码:
@Test
public void sel() {SqlSession session = null;
try {session = new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsStream("mybatis-cfg.xml"))
.openSession();
User u = new User();
u.setUsername("zhangsan");
u.setPassword("123");
User user =
session.selectOne("com.bjsxt.mapper.UserMapper.sel", u);
System.out.println(user);
} catch (IOException e) {e.printStackTrace();
} finally {session.close();
}
}
1.2.2 封装为 Map
<select id="sel" resultType="user" parameterType="map">
<!-- 如果参数是map, 可以通过#{key}来获取 -->
select * from t_user where username=#{uname} and password=#{upwd}
</select>
测试代码:
@Test
public void sel() {SqlSession session = null;
try {session = new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsStream("mybatis-cfg.xml"))
.openSession();
Map<String, String> map = new HashMap<>();
map.put("uname", "lisi");
map.put("upwd", "123");
User user =
session.selectOne("com.bjsxt.mapper.UserMapper.sel", map);
System.out.println(user);
} catch (IOException e) {e.printStackTrace();
} finally {session.close();
}
}
1. 事务(Transaction)
- 事务是数据库操作的最小单元, 有 ACID 的特性. 应该保证一个事务的的 SQL 语句要么同时成功, 要么都不成功.
- MyBatis 中配置了事务管理器, type 属性设置为 JDBC.表示 MyBatis 采用和原生 JDBC 相同的事务管理机制.
- 在 MyBatis 执行的开始时,将自动提交功能关闭了.所以,在执行 DML 操作时, 需要手动提交事务.
2. 简单提取工具类
package com.bjsxt.util;
public class MyBatisUtil {private static SqlSessionFactory factory = null;
static {try {InputStream is =
Resources.getResourceAsStream("mybatis-cfg.xml");
factory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {e.printStackTrace();
}
}
public static SqlSession getSession() {SqlSession session = null;
if (factory != null) {// true表示开启自动提交
// session = factory.openSession(true);
session = factory.openSession();
}
return session;
}
}
3. 新增(insert)
mapper 文件中, 通过<insert>定义新增语句. 注意, 由于DML 操作的返回值都是 int 类型, 所以, 不需要定义resultType 属性.
<!-- 新增 -->
<insert id="insUser" parameterType="user">
insert into t_user values (default, #{username}, #{password})
</insert>
测试代码:
@Test
public void testIns() {SqlSession session = MyBatisUtil.getSession();
User user = new User();
user.setUsername("小明");
user.setPassword("123");
int num = session.insert("com.bjsxt.mapper.UserMapper.insUser",
user);
if(num > 0) {// 提交事务
session.commit();
System.out.println("SUCCESS!");
} else {// 回滚事务
session.rollback();
System.out.println("FAILED!");
}
// 关闭资源
session.close();
}
1. 修改(update)和删除(delete)
1.1 修改
<!-- 修改 -->
<update id="updUser" parameterType="user">
update t_user set username=#{username}, password=#{password} where
id=#{id}
</update>
测试代码:
@Test
public void testUpd() {SqlSession session = MyBatisUtil.getSession();
User user = new User();
user.setId(9);
user.setUsername("大名");
user.setPassword("abc");
int num = session.update("com.bjsxt.mapper.UserMapper.updUser",
user);
if (num > 0) {System.out.println("SUCCESS");
session.commit();
} else {System.out.println("FAILED");
session.rollback();
}
session.close();
}
1.2 删除
<!-- 删除 -->
<delete id="delUser" parameterType="int">
delete from t_user where id=#{0}
</delete>
测试代码:
@Test
public void testDel() {SqlSession session = MyBatisUtil.getSession();
int num = session.delete("com.bjsxt.mapper.UserMapper
.delUser",9);
if (num > 0) {System.out.println("SUCCESS");
session.commit();
} else {System.out.println("FAILED");
session.rollback();
}
session.close();
}
1. 接口绑定方案
MyBatis 中, 提供了一套接口绑定方案. 程序员可以提供一个 接 口 , 然 后 提 供 对 应 接 口 的 一 个 mapper.xml 文 件 .MyBatis 会自动将接口和 xml 文件进行绑定. 实际上就是MyBatis 会根据接口和对应的 xml 文件创建接口的实现类.换言之, 就是可以得到接口类型的对象, 方便方法的调用.
1.1 实现方式
1.1.1 定义接口
package com.bjsxt.mapper;
import java.util.List;
import com.bjsxt.pojo.User;
public interface UserMapper {List<User> selAll();
}
1.1.2 编写对应接口的映射文件
注意:
- xml 文件名要和接口名一致
- namespace 属性必须为接口的全限定路径
- id 属性必须和接口对应的方法名一致
<mapper namespace="com.bjsxt.mapper.UserMapper">
<select id="selAll" resultType="User">
select * from t_user
</select>
</mapper>
1.1.3 在核心配置文件中扫描接口
a) 扫描单个接口, 可以使用 mapper 标签的 class 属性
<mappers>
<mapper class="com.bjsxt.mapper.UserMapper" />
</mappers>
b) 当扫描多个接口时,为简化配置,可以使用 package 标签,表示扫描对应包下的所有接口.
<mappers>
<package name="com.bjsxt.mapper" />
</mappers>
1.1.4 应用
在使用时, 可以通过 SqlSession 对象的 getMapper 方法,得到接口的代理对象, 从而可以调用定义好的方法.
@Test
public void testBind() {SqlSession session = MyBatisUtil.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> list = mapper.selAll();
for (User user : list) {System.out.println(user);
}
session.close();
}
1. 通过接口绑定解决多参数的传递
1.1 方式一
a) 接口中定义方法
User selByUP(String username, String password);
b) 映射文件中提供对应的标签. 此时, SQL语句中获取方式有两种, 通过#{index}或#{param+数字}的方式.
<select id="selByUP" resultType="user">
select * from t_user where username=#{0} and password=#{1}
</select>
1.2 方式二
a) 接口中定义方法, 参数中使用@Param 注解设定参数名用于在 SQL 语句中使用.
User selByUP(@Param("username") String username, @Param("password")
String password);
b) 映射文件中提供对应的标签. 此时, SQL语句中获取方式有两种, 通过#{参数名称}或#{param+数字}的方式.
<select id="selByUP" resultType="user">
select * from t_user where username=#{username} and
password=#{password}
</select>
1. 动态 SQL
根据条件的不同, SQL 语句也会随之动态的改变. MyBatis 中,提供了一组标签用于实现动态 SQL.
1.1 <if>
用于进行条件判断, test 属性用于指定判断条件. 为了拼接条件, 在 SQL 语句后强行添加 1=1 的恒成立条件.
<select id="sel" resultType="user">
select * from t_user where 1=1
<if test="username != null and username != ''">
and username=#{username}
</if>
<if test="password != null and password != ''">
and password=#{password}
</if>
</select>
1.2 <where>
用于管理 where 子句. 有如下功能:
a)如果没有条件, 不会生成 where 关键字
b)如果有条件, 会自动添加 where 关键字
c)如果第一个条件中有 and, 则去除
<select id="sel" resultType="user">
select * from t_user
<where>
<if test="username != null and username != ''">
and username=#{username}
</if>
<if test="password != null and password != ''">
and password=#{password}
</if>
</where>
</select>
1. 动态 SQL
1.1 <choose><when><otherwise>这是一套标签, 功能类似于 switch...case...
<select id="sel" resultType="user">
select * from t_user
<where>
<choose>
<when test="username != null and username != ''">
and username = #{username}
</when>
<when test="password != null and password != ''">
and password = #{password}
</when>
<otherwise>
and 1=1
</otherwise>
</choose>
</where>
</select>
1.2 <set>
用于维护 update 语句中的 set 子句. 功能如下:
- 满足条件时, 会自动添加 set 关键字
- 会去除 set 子句中多余的逗号
- 不满足条件时, 不会生成 set 关键字
int updUser(User user);
<update id="updUser" parameterType="user">
update t_user
<set>
id=#{id}, <!-- 防止所有条件不成立时的语法错误 -->
<if test="username != null and username != ''">
username=#{username},
</if>
<if test="password != null and password != ''">
password=#{password},
</if>
</set>
where id=#{id}
</update>
1. 动态 SQL
1.1 <trim>
用于在前后添加或删除一些内容
- prefix, 在前面添加内容
- prefixOverrides, 从前面去除内容
- suffix, 向后面添加内容
- suffixOverrides, 从后面去除内容
<update id="updUser" parameterType="user">
update t_user
<!--
prefix: 前缀, 表示向前面添加内容
prefixOverrides: 从前面删除内容
suffix: 后缀, 表示向后面添加内容
suffixOverrides: 从后面删除内容
-->
<trim prefix="set" prefixOverrides="user" suffix="hahaha"
suffixOverrides=",">
username=#{username},
</trim>
where id=#{id}
</update>
1.2 <bind>
用于对数据进行再加工, 用于模糊查询
<select id="sel" resultType="user">
select * from t_user
<where>
<if test="username!=null and username!=''">
<bind name="username" value="'%' + username + '%'"/>
and username like #{username}
</if>
</where>
</select>
1. 动态 SQL
1.1 <foreach>
用于在 SQL 语句中遍历集合参数, 在 in 查询中使用
- collection: 待遍历的集合
- open: 设置开始符号
- item: 迭代变量
- separator: 项目分隔符
- close: 设置结束符号
<select id="selIn" parameterType="list" resultType="user">
select * from t_user where id in
<foreach collection="list" open="(" separator="," close=")"
item="item">
#{item}
</foreach>
</select>
List<User> selIn(@Param("list") List<Integer> list);
1.2 <sql><include>
<sql>用于提取 SQL 语句, <include>用于引用 SQL 语句
<sql id="mySql">
id, username, password
</sql>
<select id="selIn" parameterType="list" resultType="user">
select
<include refid="mySql"/>
from t_user where id in
<foreach collection="list" open="(" separator="," close=")"
item="item">
#{item}
</foreach>
</select>
1. MyBatis 的缓存机制
- 缓存用于提高查询的效率.
- MyBatis的缓存是使用SQL标签的ID作为缓存的唯一标识的. 执行相同的标签可以使用缓存. 不同的标签不能使用缓存.
- MyBatis 中有两种缓存机制.
1.1 一级缓存
a)默认开启. 线程级别的缓存, SqlSession 的缓存
b)在一个 SqlSession 生命周期中有效. SqlSession 关闭,缓存清空.
1.2 二级缓存
a)进程级别的缓存, SqlSessionFactory 的缓存
b)在一个 SqlSessionFactory 生命周期中有效.可以在多个SqlSession 生命中期中共享.
c)默认关闭, 需要使用的时候, 要为某个命名空间开启二级缓存(在 mapper.xml 中配置<cache>).
<!-- 开启二级缓存, 要求实体类进行序列化 或者添加readonly(true)-->
<cache />
mybatis select 返回值long null_Mybatis框架(二)相关推荐
- mybatis delete返回值_从零开始学习在IntelliJ IDEA 中使用mybatis
纯新手,打算学习下mybatis的用法,在官网和教程上看了资料,整理下笔记. 既然是从零开始,就是不依赖任何框架和模板,从空白项目开始.在IDEA上先新建一个空的java项目. 1. 添加项目依赖 需 ...
- mybatis delete返回值_面试:谈谈你对MyBatis执行过程之SQL执行过程理解
前言 在了解了MyBatis初始化加载过程后,我们也应该研究看看SQL执行过程是怎样执行?这样我们对于Mybatis的整个执行流程都熟悉了,在开发遇到问题也可以很快定位到问题. 更重要的,在面试中遇到 ...
- mybatis update 返回值
mybatis sql: <update id="test" parameterType="map">update test_0731 set na ...
- java grpc 客户端处理 go 服务端多返回值_grpc基础实践(二)
在此篇中我们将简要介绍关于grpc对java客户端的实现. 在开始开发前,我们需要先导入 io.grpc grpc-netty 1.11.0io.grpc grpc-protobuf 1.11.0io ...
- mybatis update返回值的意义
2019独角兽企业重金招聘Python工程师标准>>> update返回值的意义是影响了几行记录,而非更新了几行记录 转载于:https://my.oschina.net/28923 ...
- mybatis update返回值_mybatis 详解(六)通过mapper接口加载映射文件
通过 mapper 接口加载映射文件,这对于后面 ssm三大框架 的整合是非常重要的.那么什么是通过 mapper 接口加载映射文件呢? 我们首先看以前的做法,在全局配置文件 mybatis-conf ...
- Mybatis 查询 返回值中只有id有值,其他都是null;
最近在重温mybatis, 但是在做练习的时候发现一个问题; 查询,简单的查询,返回之后发现结果中,只有id被映射了值,其他属性都是null; 很纳闷,为什么一个简单的测试会出现这种问题; 一开始以为 ...
- 深入了解MyBatis返回值
深入了解MyBatis返回值 想了解返回值,我们需要了解resultType,resultMap以及接口方法中定义的返回值. 我们先看resultType和resultMap resultType和r ...
- mybatis 添加语句返回对象_mybatis的insert语句插入数据时的返回值的实现
mybatis的insert语句插入数据时的返回值的实现,语句,返回值,那条,都是,站长站 mybatis的insert语句插入数据时的返回值的实现 易采站长站,站长之家为您整理了mybatis的in ...
最新文章
- R语言vtreat包自动处理dataframe的缺失值并生成对应的数据列_isbad来指示数据的原始缺失情况、查看特定字段缺失的那些数据行、查看数据集中多个字段的均值
- template_1
- npm 安装less插件_node+npm+webpack+less安装
- Session History 属性和方法
- 用于确定两个字符串最长公共子串的函数
- 安装nltk,textacy库
- 访问服务器 信号灯超时时间已到,win7系统分区提示信号灯超时时间已到怎么办...
- 介绍VMware虚拟化存储原理及数据恢复方法
- 什么是IT和什么是IT行业
- channel(3) 一 基本定义
- HTML基础知识笔记-01
- 电脑版 钉钉 卡顿 解决办法
- 基于RBF神经网络的数据预测
- Inventor记录
- kubernets 学习记录
- DCDC输出纹波大的原因
- python怎么计算相关系数、偏相关系数?
- subtext 安装PythonIDE -Anaconda
- 南京林业计算机技术883,2018年南京林业大学信息科学技术学院883电工及电子技术之电工学-电工技术考研核心题库...
- 小程序分享并携带参数,方便做分销,拼团,返佣等功能
热门文章
- 数字签名和加密的基本原理及其区别?
- 搭建Docker环境---Docker概述
- POJ 2096 Collecting Bugs:期望dp
- struct、union、enum and sizeof
- 解决:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
- 图片 滚动切换效果(五) 高级篇
- 小米手机连接不上网络 或者 暂时关闭状况不佳的连接
- SQL Server中 char与varchar
- FusionCharts 中文乱码
- AndroidStudio_android多线程和异步任务_要学内容介绍_相关知识点---Android原生开发工作笔记241