前言
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默认是事务不提交
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。