mybatis注解与xml常用语句汇总
作者:wotrd 时间:2022-05-17 18:39:47
前言
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
本文将给大家详细介绍关于mybatis注解与xml常用语句的相关内容,下面话不多说了,来一起看看详细的介绍吧
mybatis注解使用
1.简单crud
public interface UserMapper {
//查询
@Select("select * from user where id=#{id}")
User selectUser(int id);
//查询全部
@Select("select * from user")
List<User> selectUserList();
//增加数据
@Insert("insert into user (name) values(#{name})")
boolean insertUser(String name);
//修改用户
@Update("update user set name=#{name} where id=#{id}")
boolean updateUser(@Param("name") String name,@Param("id") int id);
//删除用户
@Delete("delete from user where id=#{id}")
boolean deleteUser(int id);
}
2.一对一注解
@Select("select * from user")
@Results({
@Result(id = true,property = "id",column = "id"),//id=true 对应于主键
@Result(property = "uid",column = "uid"),
@Result(property = "user",column = "uid",javaType = User.class,
one = @One(select = "com.example.dao.UserDao.findUserByid",fetchType = FetchType.DEFAULT))
//user 对应实体类中一对一的实体类名字,uid表示通过uid外键查询User,JavaType表示查询结果
//映射成User类型对象,one=表示一对xx fetchType.default默认是立即加载全部查询,使用lazy懒加载需要才查询
})
List<User> selectUserList();
3,一对多注解
mybatis的xml配置
1.配置resultMap
<resultMap id="BaseResultMap" type="xx" >
<id column="id" property="ID" jdbcType="BIGINT" />
<result column="aa" property="aa" jdbcType="VARCHAR" />
<result column="bb" property="bb" jdbcType="INTEGER" />
<result column="cc" property="cc" jdbcType="DECIMAL" javaType="java.math.BigDecimal" />
<result column="dd" property="dd" jdbcType="DATE" />
</resultMap>
2.通用sql短语
<sql id="Base_Column_List" >
aa, bb
</sql>
<sql id="where">
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test="id != null and id != ''">
AND t.ID = #{id}
</if>
<if test="content != null and content != ''">
AND t.CONTENT LIKE concat('%', #{content},'%')
</if>
AND t.APP_CODE IN
<foreach item="item" index="index" collection="appcodes"
open="(" separator="," close=")">
#{item}
</foreach>
and t.USER_ID=u.id and t.REMOVED=0
</trim>
</sql>
3.需要验证的插入
<insert id="insert" parameterType="xxx"
useGeneratedKeys="true" keyColumn="id" keyProperty="id">
insert into xxx (
<trim suffixOverrides=",">
<if test="title != null and title != '' ">
TITLE ,
</if>
</trim>
) VALUES (
<trim suffixOverrides=",">
<if test="title != null and title != '' ">
#{title} ,
</if>
</trim>
)
</insert>
4.需要验证的更新
<update id="update" parameterType="xxx">
UPDATE xxx
<set>
<if test="title != null and title != '' ">
TITLE = #{title} ,
</if>
</set>
WHERE
ID = #{id}
</update>
5.<!--批量更新ticketid和SeatNo-->
<update id="xxxUpdate" parameterType="java.util.List">
update xxx
<trim prefix="set" suffixOverrides=",">
<trim prefix="AA =case" suffix="end,">
<foreach collection="orders" item="item" index="index">
<if test="item.aa !=null">
when ID=#{item.id} then #{item.aa}
</if>
</foreach>
</trim>
<trim prefix="BB =case" suffix="end,">
<foreach collection="orders" item="item" index="index">
<if test="item.bb !=null">
when ID=#{item.id} then #{item.bb}
</if>
</foreach>
</trim>
</trim>
where ID in
<foreach collection="orders" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>
mybatis可以使用string给数据库int类型赋值
springboot中开启日志
#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
1.ORDER BY ${columnName}
这里 MyBatis 不会修改或转义字符串。NOTE 用这种方式接受用户的输入,并将其用于语句中的参数是不安全的,会导致潜在的 SQL 注入攻击,因此要么不允许用户输入这些字段,要么自行转义并检验。
2.如何使用连接池。
首先实例化连接池数据源对象,让他实现DataSourceFactory这个接口。然后实现方法。在mybatis。conf文件中设置数据连接池这个类,将数据库连接信息放在config.properties文件中。
3.mybatis.config文件中setting和数据源的设置参数区别
会被覆盖。
4.连接参数查询顺序
首先查询properties文件,然后查询resource文件,最后查询方法参数。重复的话会被覆盖。
5.druid连接池配置方式:
详见官网
DruidDataSourceFactory首先实行setproperties方法,然后返回设置数据源方法。drui数据源也需要在DataSource中设置properties文件
6.实体类的方法不定义也可以进行映射
7.mybatis默认是事务不提交
来源:https://segmentfault.com/a/1190000016333566
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 数据结构与算法系列精讲之排序算法
![](https://img.aspxhome.com/file/2023/4/65304_0s.jpg)
SpringCloud用Zookeeper搭建配置中心的方法
JAVA读取文件夹大小的几种方法实例
Springboot - Fat Jar示例详解
java GUI编程之paint绘制操作示例
基于WebClient实现Http协议的Post与Get对网站进行模拟登陆和浏览实例
JDK源码分析之String、StringBuilder和StringBuffer
![](https://img.aspxhome.com/file/2023/0/68160_0s.jpg)
springAop实现权限管理数据校验操作日志的场景分析
Java多线程编程中使用Condition类操作锁的方法详解
Java使用Apache.POI中HSSFWorkbook导出到Excel的实现方法
![](https://img.aspxhome.com/file/2023/3/63323_0s.png)
Mybatis拦 截 器实现数据权限的示例代码
C#实现观察者模式(Observer Pattern)的两种方式
![](https://img.aspxhome.com/file/2023/0/66130_0s.png)
SpringbootJPA分页 PageRequest过时的替代方法
![](https://img.aspxhome.com/file/2023/1/68001_0s.png)
Springboot如何根据实体类生成数据库表
![](https://img.aspxhome.com/file/2023/7/59677_0s.jpg)
Java实现简单的递归操作方法实例
![](https://img.aspxhome.com/file/2023/6/65306_0s.png)
使用Spring Data JDBC实现DDD聚合的示例代码
基于Java实现缓存Cache的深入分析
Java多线程回调方法实例解析
![](https://img.aspxhome.com/file/2023/2/59002_0s.png)
springboot中的pom文件 project报错问题
![](https://img.aspxhome.com/file/2023/9/62519_0s.png)
Java微信公众平台开发(13) 微信JSSDK中Config配置
![](https://img.aspxhome.com/file/2023/9/67439_0s.jpg)