mybatis

1. mybatis是一个持久层框架,,在实体类和sql语句之间建立映射关系,是apache下的开源项目,前身是itbatis,是一种半自动化的ORM实现,mybatis提供输入和输出的映射,需要程序员自己写sql语句,mybatis重点对sql语句的灵活操作

在这里插入图片描述
2. 利用反射打通Java类与SQL语句之间的相互转换

mybatis-config配置文件

<?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="mapUnderscoreToCamelCase" value="true"/>
        <!--she设置控制台输出sql日志-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <!--给类取别名-->
     <!--   <typeAlias type="com.entor.entity.User" alias="User"/>-->
        <!--指定包下面的所有类以简写类名作为别名-->
        <package name="com.entor.entity"/>
    </typeAliases>
    
    <!--配置数据库默环境,支持多种环境-->
    <environments default="mysql">
        <environment id="mysql">
            <!--使用jdbc事务管理-->
            <transactionManager type="JDBC"/>
            <!--数据源使用池化技术,即使用数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/jsd2107?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--加载orm映射文件-->
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>
</configuration>

UserMapper.xml

<?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">
<!--namespace不能为空,可以是任何值用来区分不同的映射对象,不同的映射文件值不能一样-->
<mapper namespace="com.entor.dao.UserDao"><!--com.entor.entity.User-->
    <!--引用-->
    <sql id="common">
        id,name,username,password,sex,age,birthday,create_time createTime
    </sql>

    <!--sql语句接收参数时,#{任意字符}只是一个占位符,可以填写任意字符
    parameterType="int"可写可不写,如果写了类型,那调用的时候必须传递对应的参数,否则报错
    作为查询语句,resultType="User"必须要指定返回的类型,用来封装查询到的字段值-->
    <!--parameterType传进参数,resultType传出参数 查出来之后自动封装到User(依据:反射找set方法,) 返回对象-->
    <select id="queryById" parameterType="int" resultType="User">  /*别名*/
        /*引用*/
        select <include refid="common"/> from user where id = #{id}

    </select>
    <!--sql语句需要接收多个参数,可以把多个参数封装到一个mao中,#{pageNum}意思是调用map中键为pageNum的值-->
    <select id="queryByPage" parameterType="map" resultType="User">  /*create_time 没有set方法 所以去明白*/
        select id,name,username,password,sex,age,birthday,create_time createTime
        from user order by id limit #{pageNum},#{pageSize}

    </select>
    <!--sql语句需要接收多个参数,可以把多个参数封装到一个对象中,#{name}意思是调用对象中getName方法取到的值-->
   <!--useGeneratedKeys="true" 获取自动增长的主键值
   keyColumn="id" 数据库主键的列字段名称
    keyProperty="id" 数据库主键对应的类对应的id属性
    三个属性的作用是把新增的记录的id主键值注入到对象的id属性中-->
    <insert id="add" useGeneratedKeys="true" keyColumn="id" keyProperty="id"><!--传User进来 调用set方法  parameterType="User"可以省略  -->
        insert into user(name,username,password,sex,age,birthday,create_time)
        values(#{name},#{username},#{password},#{sex},#{age},#{birthday},now())
    </insert>
    <update id="update">
        update user set
                        name = #{name},
                        username = #{username},
                        password = #{password},
                        sex = #{sex},
                        age = #{age},
                        birthday = #{birthday}
        where id = #{id}

    </update>
    <delete id="deleteById">
        delete from user where id = #{id}
    </delete>

    <!--#{}和${}的区别:前者是占位符,传递参数的字符串会添加一对单引号,可以防止sql注入攻击,后者是直接拼接参数,参数是字符串不会加单引号,有sql注入风险,-->
    <delete id="deleteByIds">
<!--方式一:直接sql参数拼接-->
    <!--delete form user where id in (${ids})-->

<!--方式二 生成动态sql-->
   delete from user where id in
    <!--接收array数组参数,遍历数组每一个元素,id指的是数组中的每一个元素-->
    <foreach collection="array" open="(" close=")" separator="," item="id">
        #{id}
    </foreach>

    </delete>
    
    <insert id="addMore">
       insert into user(name,username,password,sex,age,birthday) values
        <foreach collection="list" separator="," item="user">
            (#{user.name},#{user.username},#{user.password},#{user.sex},#{user.age},#{user.birthday})
        </foreach>

    </insert>

    <select id="getTotal" resultType="int">
        select count(*) from user
    </select>
</mapper>

  1. 配置数据库默认环境,支持多种环境
<environments default="mysql">
        <!--配置数据库环境,id唯一标识-->
        <environment id="mysql">
            <!--使用jdbc事务管理-->
            <transactionManager type="JDBC"/>
            <!--数据源使用池化技术,即使用数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/jsd2107?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
  1. 加载orm映射文件
  <mappers>
        <!--加载orm映射文件-->
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值