EchoDemo's Blogs

springmvc学习笔记(7)-springmvc整合mybatis之mapper

元丰六年十月十二日夜,解衣欲睡,月色入户,欣然起行。念无与为乐者,遂至承天寺寻张怀民。怀民亦未寝,相与步于中庭。

庭下如积水空明,水中藻、荇交横,盖竹柏影也。何夜无月?何处无竹柏?但少闲人如吾两人者耳。

—-宋·苏轼《记承天寺夜游》

数据库配置文件和日志配置文件

在resource文件夹下添加两个文件:数据库配置文件和日志配置文件。

1、数据库配置文件db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=

2、日志配置文件log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

sqlMapConfig.xml(mybatis自己的配置文件)

在resources目录下新建mybatis文件夹,并新建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>
    <!-- 全局setting配置,根据需要添加 -->

    <!-- 别名定义 -->
    <typeAliases>
        <!-- 批量别名定义
        指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名
        (首字母大写或小写都可以)
        -->
        <package name="com.echodemo.springmvcmybatis.bean"/>
    </typeAliases>

    <!-- 配置mapper
     由于使用spring和mybatis的整合包进行mapper扫描,这里不需要进行配置
     必须遵循:mapper.xml和mapper.java文件同名,且在同一个目录下-->

    <!--<mappers>-->
        <!--&lt;!&ndash;通过resource方法一次加载一个映射文件 &ndash;&gt;-->
        <!--&lt;!&ndash;<mapper resource="UserMapper.xml"></mapper>&ndash;&gt;-->

        <!--&lt;!&ndash; 批量加载mapper-->
        <!--指定mapper接口的包名,mybatis自动扫描包下边所有mapper接口进行加载-->
        <!--遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,-->
        <!--且在一个目录中上边规范的前提是:使用的是mapper代理方法-->
         <!--&ndash;&gt;-->
        <!--<package name="com.echodemo.springmybatis.mapper"/>-->
    <!--</mappers>-->
</configuration>

applicationContext-dao.xml

在resources目录下新建spring文件夹,并新建applicationContext-dao.xml文件。分别配置:数据源、SqlSessionFactory、mapper扫描器。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

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

    <!-- 数据源,使用dbcp -->
    <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="minIdle" value="5"/>
    </bean>

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

    <!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册
        遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录中
        自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
        -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定扫描的包名,如果需要扫描多个包,中间使用半角的逗号隔开 -->
        <property name="basePackage" value="com.echodemo.springmvcmybatis.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

</beans>

逆向工程生成po类及mapper(单表增删改查)

具体参见:mybatis学习笔记(18)-mybatis逆向工程-mybatis%E9%80%86%E5%90%91%E5%B7%A5%E7%A8%8B/)

手动定义商品查询mapper

针对综合查询mapper,一般情况会有关联查询,建议自定义mapper。

1、ItemsMapperCustom.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">
<mapper namespace="com.echodemo.springmvcmybatis.mapper.ItemsMapperCustom">

    <!-- 定义商品查询的SQL片段 -->
    <sql id="query_items_where">
        <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
        <!-- 商品中的查询条件通过ItemsQueryVo包装对象中itemsCustom属性值传递 -->
        <if test="itemsCustom!=null">
            <if test="itemsCustom.name!=null and itemsCustom.name!=''">
                items.name LIEK '${itemsCustom.name}'
            </if>
        </if>
    </sql>

    <!-- 商品的列表查询 -->
    <!-- 建议paramterType传入包装对象
         resultType建议使用扩展对象
    -->
    <select id="findItemsList" parameterType="com.echodemo.springmvcmybatis.bean.ItemsQueryVo" resultType="com.echodemo.springmvcmybatis.bean.ItemsCustom">
      SELECT * FROM items
      <where>
          <include refid="query_items_where"></include>
      </where>
    </select>
</mapper>

2、ItemsMapperCustom.java

package com.echodemo.springmvcmybatis.mapper;

import com.echodemo.springmvcmybatis.bean.ItemsCustom;
import com.echodemo.springmvcmybatis.bean.ItemsQueryVo;

import java.util.List;

public interface ItemsMapperCustom {

    // 查询商品列表
    List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}

3、po类ItemsCustom

package com.echodemo.springmvcmybatis.bean;

/**
 * 商品信息的扩展类
 */
public class ItemsCustom extends Items{
    // 添加商品信息的扩展属性
}

4、输入pojo的包装类

package com.echodemo.springmvcmybatis.bean;

/**
 * 商品的包装对象
 */
public class ItemsQueryVo {
    // 商品信息
    private Items items;

    // 为了系统的可扩展性,对原始生成的po类进行扩展
    private ItemsCustom itemsCustom;

    public Items getItems() {
        return items;
    }

    public void setItems(Items items) {
        this.items = items;
    }

    public ItemsCustom getItemsCustom() {
        return itemsCustom;
    }

    public void setItemsCustom(ItemsCustom itemsCustom) {
        this.itemsCustom = itemsCustom;
    }
}
🐶 您的支持将鼓励我继续创作 🐶
-------------本文结束感谢您的阅读-------------