mybatis基础操作

概述

mybatis作用

Mybatis是支持定制sql,存储过程以及高级映射的优秀的半自动化框架.mybatis避免了几乎所有的JDBC代码和手动设置参数以及获取对焦.mybatis使用了简单的xml用于配置和原始映射,将接口和java的pojo类映射成数据库中的记录,开发者只需要关注sql本身,而不需要话费精力去处理例如注册驱动创建connection,statement,手动设置参数,结果集检查等jdbc复杂的过程代码

为什么使用mybatis

对于开发人员,核心sql还是需要自己优化,mybatis是一个本自动化的持久层框架,mybatis是支持定制化,存储过程以及高级映射的优秀的持久层框架

#{}

表示一个展位符号,通过#{}可以实现prepareStatement向占位符中设置值自动进行java类型和jdbc类型转换,可以有效的防止sql注入,#{}可以接受简单类型值或pojo属性值,如果parameterType传输单个简单类型值,#{}括号中可以是value或其他名称

${}

通过${}可以将parameterType传入的内容拼接在sql中且不进行jdbc类型转换

${}可以接收简单类型值或pojo属性值

如果parameterType传输单个简单类型值,${}括号中只能是value

属性值

parameterType(传入的参数类型)

输入参数类型,mybatis通过ognl从对象中获取参数拼接在sql中

可以直接写String 或者Integer(可以省略java.lang的前缀) ,也可以直接按照包名写自定义的模型对象(可以省略),可以通过批量定义包名免写前缀

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <!-- 批量别名定义,扫描整个包下的类,别名为类名(大小写不敏感) -->
        <package name="com.itlike.domain" />
    </typeAliases>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
    </plugins>
</configuration>

resultType(返回值)

指定输出结果类型,mybatis将sql查询结果的一行记录映射为resultMap的指定类型的对象

如果有多条数据,则分别映射,并把对象放到list容器当中

可以直接写String 或者Integer(可以省略java.lang的前缀) ,也可以直接按照包名写自定义的模型对象(可以省略)

如果返回值1是java.util.Map ,会把字段名作为key,值作为我value存放到value当中

可以在mapper接口上面加上注解@MapKey("id"),可以实现用id来作为key

update/insert/delete只会返回影响行数,接口里返回类型定义成int即可,void自然啥都木有

使用result必须保证数据库和模型类保持一致

resultMap

如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性作为一个对应关系表名与模型类

column表示数据库中的哪一列,property表示对象中的哪个属性

<resultMap id="BaseResultMap" type="com.itlike.domain.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="state" property="state" jdbcType="TINYINT" />
    <result column="admin" property="admin" jdbcType="TINYINT" />
</resultMap>

用户功能实现

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.itlike.mapper.UserMapper" >
  <resultMap id="BaseResultMap" type="com.itlike.domain.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="state" property="state" jdbcType="TINYINT" />
    <result column="admin" property="admin" jdbcType="TINYINT" />
  </resultMap>
  <!--根据id删除用户-->
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <!--创建用户-->
  <insert id="insert" parameterType="com.itlike.domain.User" >
    insert into user  (username, password, admin)
    values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{admin,jdbcType=TINYINT})
  </insert>
  <!--根据用户id修改用户(编辑用户的时候使用)-->
  <update id="updateByPrimaryKey" parameterType="com.itlike.domain.User" >
    update user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      state = #{state,jdbcType=TINYINT},
      admin = #{admin,jdbcType=TINYINT}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select id, username, password, state, admin
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <!--查询用户名并排除当前id-->
  <select id="selectEdit" resultMap="BaseResultMap" parameterType="com.itlike.domain.User" >
    select id, username, password, state, admin
    from user
    where username = #{username}
    and id  &lt;&gt;  #{id}
  </select>
  <!--根据用户名查询对应用户是否存在(登录的时候使用)-->
  <select id="selectByUsername" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select id,username
    from user
    where username = #{username}
  </select>
  <!--列出全部用户-->
  <select id="selectAll" resultMap="BaseResultMap" >
    select id, username, password, state, admin
    from user
  </select>
  <!--用户登录-->
  <select id="userLogin"  resultMap="BaseResultMap" parameterType="User">
    select * from `user`where
    username=#{username} and password=#{password}
  </select>
</mapper>
Last modification:April 22, 2022
如果觉得我的文章对你有用,请随意赞赏