普通集成SSM

一、sql映射器Mpper

  • 写一个接口XxxMapper
  • 写配置文件XxxMapper.xml
  • 测试

二、高级查询

<!--高级查询-->
    <select id="queryList" parameterType="teacherQuery" resultType="Teacher">
        select * from t_teacher
        <where>
            <if test="name!=null">
                AND name LIKE concat("%",#{name},"%")
            </if>
            <if test="minAge!=null">
                AND age &gt; #{minAge}
            </if>
            <!--<if test="maxAge!=null">
                AND age &lt; #{maxAge}
            </if>-->
            <!--使用<![CDATA[]]> 包含> 表示滤过里面的符号-->
            <if test="maxAge!=null">
                <![CDATA[
                  AND age < #{maxAge}
                ]]>
            </if>
        </where>
    </select>

三、ResultMap结果集映射

3.1 多对一

  • 嵌套结果
<!--多对一,嵌套结果查询 发送一条sql语句查询数据-->
    <!--type属性,返回值的全限定类名,或类型别名.-->
    <!--id,select 中 resultMap-->
    <resultMap id="studentMap" type="student">
        <!--property java类里面属性 column查询出来列-->
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="hobby" column="hobby"></result>
        <!--关联 property:映射数据库列的字段或属性 javaTyp:完整java类名或别名。-->
        <association property="teacher" javaType="Teacher">
            <id property="id" column="tid"></id>
            <result property="name" column="tname"></result>
            <result property="age" column="tage"></result>
        </association>
    </resultMap>
    <select id="queryAll" resultMap="studentMap">
        select s.id,s.name,s.hobby,t.id tid,t.name tname,t.age tage
        from t_student s
        join t_teacher t
        on s.tid = t.id
    </select>
  • 嵌套查询
    <!--嵌套查询 1+n条数据查询-->
    <resultMap id="studentMap" type="student">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="hobby" column="hobby"></result>
        <association property="teacher" column="tid" javaType="Teacher" select="queryTeacherByTid" >
        </association>
    </resultMap>
    <select id="queryAll" resultMap="studentMap">
        SELECT s.* FROM t_student s
    </select>
    <select id="queryTeacherByTid" parameterType="long" resultType="Teacher">
        SELECT * FROM t_teacher WHERE id=#{id}
    </select>

3.2 一对多

  • 嵌套结果
<!--一对多,嵌套结果查询 发送一条sql语句查询数据-->
    <!--type属性,返回值的全限定类名,或类型别名.-->
    <!--id,select 中 resultMap-->
    <resultMap id="teacherMap" type="Teacher">
        <!--property java类里面属性 column查询出来列-->
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <!--关联 property:映射数据库列的字段或属性 javaTyp:完整java类名或别名。-->
        <collection property="students" javaType="arraylist" ofType="student">
            <id property="id" column="sid"></id>
            <result property="name" column="sname"></result>
            <result property="hobby" column="shobby"></result>
        </collection>
    </resultMap>
    <select id="loadAll" resultMap="teacherMap">
        select t.id,t.name,t.age,s.id sid,s.name sname,s.hobby shobby
        from t_teacher t
        join t_student s
        on t.id = s.tid
    </select>
  • 嵌套查询
<!--嵌套查询 一对多-->
    <resultMap id="teacherMap" type="Teacher">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <!--javaType 返回类型 ofType 集合里面的类型(泛型)-->
        <collection property="students" column="id" javaType="arraylist" ofType="student" select="findStudentsByTid"></collection>
    </resultMap>
    <select id="loadAll" resultMap="teacherMap">
        SELECT t.id,t.name,t.age FROM t_teacher t
    </select>
    <select id="findStudentsByTid" parameterType="long" resultType="student">
        SELECT * FROM t_student WHERE tid=#{id}
    </select>

四、延迟加载

  • lazy load: 需要使用的才去加载-- jpa(FetchType.LAZY)
  • mybatis可以配置lazyload–(了解)
  • 前后端分离项目 – lazy用不大的

五、缓存

  • jpa:
    ​ - 一级缓存(自带) 属于entityManager缓存 – 命中条件 同一个EntityManagerFactory 同一个EntityManager 同OID
    ​ - 二级缓存(需要配置实现) 属于entitytManagerFactory – 命中条件 同一个EntityManagerFactory 不同EntityManager 同OID
    • 缓存有什么用: 用空间换取时间概念
  • Mybatis:
    ​ - 一级缓存: 属于sqlSession级别 同一个SqlSessionFactory 同一个SqlSession 同一个id
    • 二级缓存:属于sqlSessionFactory级别 同一个SqlSessionFactory 不同一个SqlSession 同一个id
  • 序列化: 把对象转换成二进制形式 一个对象为什么需要序列化 --方便传输(特别需要保存到磁盘或者硬盘,需要你要去进行序列化)
  • 反序列化: 把二进制的数据转换对象
  • redis(nosql非关系型数据库) 专门做中央缓存

六、SSM集成(普通)

  • domain
  • mapper
  • query
  • service
  • controller
  • resources
    • applicationContext.xml
    <!--扫描service层-->
    <context:component-scan base-package="xx.xx.ssm.service"></context:component-scan>
    <!--配置jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--配置dataSource连接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--配置 mapper.xml 位置-->
        <property name="mapperLocations" value="classpath:xx/xx/ssm/mapper/*Mapper.xml"></property>
        <!--别名配置-->
        <property name="typeAliasesPackage">
            <value>
                xx.xx.ssm.domain
                xx.xx.ssm.query
            </value>
        </property>
    </bean>
    <!--处理mapper接口,spring扫描生成子类,可注入service层-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="xx.xx.ssm.mapper"></property>
    </bean>
    <!--事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--开启注解支持-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    
    • applicationContext-mvc.xml
    <!--扫描controller层-->
    <context:component-scan base-package="xx.xx.ssm.controller" />
    <!--静态资源处理-->
    <mvc:default-servlet-handler/>
    <!--开启注解支持-->
    <mvc:annotation-driven/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    • jdbc.properties
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///ssm
    jdbc.username=root
    jdbc.password=****
    
    • cn.xx.ssm.mapper
      • XxxMapper.xml
  • web
    • web.xml
    <!--读取spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--配置监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--核心控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--编码过滤器-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值