MyBatis学习笔记(二)

MyBatis学习笔记二


知识纲领:
  • 输入映射和输出映射
  • 动态SQL
  • 关联查询
  • MyBatis整合spring
  • MyBatis逆向工程

一. 映射
对于映射的理解?
  • Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心
输入映射(parameterType)
  • 传递简单类型
    • 使用#{} 或者 ${} 进行SQL拼接
  • 传递pojo类型
    • #{} ${} 中的值为pojo属性名称
  • 传递pojo包装对象
    • 什么是包装类型; pojo 中的一个属性是另外一个pojo
输出映射(resultType)
  • 输出简单类型(integer等类型)
  • 输出pojo对象
  • 输出pojo列表
resultMap
  • resultType可以指定将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。

    ​ 如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。

    ​ resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。

  • resultMap的使用

    <resultMap id="orderResult" type="order">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="note" column="note" />
    </resultMap>
二. 动态SQL
  • if标签, where标签 , include标签
  <!--使用标签进条件查询-->
    <select id="findBySexAndName" parameterType="user" resultType="user">
        select * from user
        <where>
            <if test="sex != null">
                and sex = #{sex}
            </if>
            <if test="username != null and username != ''">
                and username like
                '%${username}%'
            </if>
        </where>
    </select>
  • foreach标签
<!--使用foreeach来查询多个数据-->
    <select id="findUserByIds" parameterType="ubaba" resultType="user">
        select * from user
        <where>
            <foreach collection="ids" item="item" open="id in (" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </select>
三. 关联查询
1. 一对一查询
  • 使用resultType(企业中常用)
    • 自定义一个pojo类来存放数据
  • 使用resultMap
    • 使用的原理就是在pojo中嵌入其他的pojo类
2. 一对多查询

    <resultMap id="userOrderResultMap" type="user">
        <id column="id" property="id"/>
        <result property="username" column="username"/>
        <result property="birthday" column="birthday"/>
        <result property="sex" column="sex"/>
        <result property="address" column="address"/>
        <collection property="orders" javaType="list" ofType="order">
            <id column="id" property="id"/>
            <result property="number" column="number" />
            <result property="createtime" column="createtime" />
            <result property="note" column="note" />
        </collection>
     </resultMap>
四. MyBatis整合spring
整合步骤:
  • 将SqlSessionFactory放到spring容器中作为单例对象
  • sqlsession应该从spring容器中获得
  • mapper代理形式中, 应该从spring容器中直接获得mapper的代理对象
  • 数据库的连接以及数据库连接池事务管理都交给spring容器来完成
SqlMapConfig.xml
<?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>
    <!-- 设置别名 -->
    <typeAliases>
        <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
        <package name="com.lyric.mybatis.pojo" />
    </typeAliases>

    <mappers>
        <package name="com.lyric.mybatis.mapper"/>
    </mappers>

</configuration>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置mybatis核心配置文件 -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>


    <!-- Mapper代理的方式开发方式一,配置Mapper代理对象 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- 配置Mapper接口 -->
        <property name="mapperInterface" value="com.lyric.mybatis.mapper.UserMapper" />
        <!-- 配置sqlSessionFactory -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

    <!-- Mapper代理的方式开发方式二,扫描包方式配置代理 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置Mapper接口 -->
        <property name="basePackage" value="com.lyric.mybatis.mapper" />
    </bean>
</beans>
五. 逆向工程
  • 在配置文件中修改三个地方
深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值