程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> springmvc學習筆記(7)-springmvc整合mybatis之mapper

springmvc學習筆記(7)-springmvc整合mybatis之mapper

編輯:JAVA綜合教程

springmvc學習筆記(7)-springmvc整合mybatis之mapper


springmvc學習筆記(7)-springmvc整合mybatis之mapper


本文記錄springmvc整合dao的配置

整合dao

首先在resource文件夾下添加兩個文件:數據庫配置文件和日志配置文件

數據庫配置文件db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://120.25.162.238:3306/mybatis001?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
日志配置文件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文件

<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20%3F%2D%2D%3E-->

<configuration>

    <!--{cke_protected}{C}%3C!%2D%2D%20%E5%85%A8%E5%B1%80setting%E9%85%8D%E7%BD%AE%EF%BC%8C%E6%A0%B9%E6%8D%AE%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%20%2D%2D%3E-->

    <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AE%E5%88%AB%E5%90%8D%20%2D%2D%3E-->
    <typealiases>
        <!--{cke_protected}{C}%3C!%2D%2D%20%E6%89%B9%E9%87%8F%E6%89%AB%E6%8F%8F%E5%88%AB%E5%90%8D%20%2D%2D%3E-->
        <package name="com.iot.learnssm.firstssm.po">
    </package></typealiases>

    <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEmapper%0A%20%20%20%20%E7%94%B1%E4%BA%8E%E4%BD%BF%E7%94%A8spring%E5%92%8Cmybatis%E7%9A%84%E6%95%B4%E5%90%88%E5%8C%85%E8%BF%9B%E8%A1%8Cmapper%E6%89%AB%E6%8F%8F%EF%BC%8C%E8%BF%99%E9%87%8C%E4%B8%8D%E9%9C%80%E8%A6%81%E9%85%8D%E7%BD%AE%E4%BA%86%E3%80%82%0A%20%20%20%20%E5%BF%85%E9%A1%BB%E9%81%B5%E5%BE%AA%EF%BC%9Amapper.xml%E5%92%8Cmapper.java%E6%96%87%E4%BB%B6%E5%90%8C%E5%90%8D%E4%B8%94%E5%9C%A8%E4%B8%80%E4%B8%AA%E7%9B%AE%E5%BD%95%0A%20%20%20%20%20%2D%2D%3E-->

    <!--{cke_protected}{C}%3C!%2D%2D%20%3Cmappers%3E%0A%0A%20%20%20%20%3C%2Fmappers%3E%20%2D%2D%3E-->
</configuration></code>

applicationContext-dao.xml

在resources目錄下新建spring文件夾,並新建applicationContext-dao.xml文件

配置:

數據源 SqlSessionFactory mapper掃描器
<code class="language-xml hljs "><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8A%A0%E8%BD%BDdb.properties%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%86%85%E5%AE%B9%EF%BC%8Cdb.properties%E6%96%87%E4%BB%B6%E4%B8%ADkey%E5%91%BD%E5%90%8D%E8%A6%81%E6%9C%89%E4%B8%80%E5%AE%9A%E7%9A%84%E7%89%B9%E6%AE%8A%E8%A7%84%E5%88%99%20%2D%2D%3E-->
    <context:property-placeholder location="classpath:db.properties">
    <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E6%BA%90%20%EF%BC%8Cdbcp%20%2D%2D%3E-->

    <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="30">
        <property name="maxIdle" value="5">
    </property></property></property></property></property></property></bean>

    <!--{cke_protected}{C}%3C!%2D%2D%20%E4%BB%8E%E6%95%B4%E5%90%88%E5%8C%85%E9%87%8C%E6%89%BE%EF%BC%8Corg.mybatis%3Amybatis-spring%3A1.2.4%20%2D%2D%3E-->
    <!--{cke_protected}{C}%3C!%2D%2D%20sqlSessionFactory%20%2D%2D%3E-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--{cke_protected}{C}%3C!%2D%2D%20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%20%2D%2D%3E-->
        <property name="dataSource" ref="dataSource">
        <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8A%A0%E8%BD%BDmybatis%E7%9A%84%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%20%2D%2D%3E-->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml">
    </property></property></bean>
    <!--{cke_protected}{C}%3C!%2D%2D%20mapper%E6%89%AB%E6%8F%8F%E5%99%A8%20%2D%2D%3E-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--{cke_protected}{C}%3C!%2D%2D%20%E6%89%AB%E6%8F%8F%E5%8C%85%E8%B7%AF%E5%BE%84%EF%BC%8C%E5%A6%82%E6%9E%9C%E9%9C%80%E8%A6%81%E6%89%AB%E6%8F%8F%E5%A4%9A%E4%B8%AA%E5%8C%85%EF%BC%8C%E4%B8%AD%E9%97%B4%E4%BD%BF%E7%94%A8%E5%8D%8A%E8%A7%92%E9%80%97%E5%8F%B7%E9%9A%94%E5%BC%80%20%2D%2D%3E-->
        <property name="basePackage" value="com.iot.learnssm.firstssm.mapper">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
       <!--{cke_protected}{C}%3C!%2D%2D%20%3Cproperty%20name%3D%22sqlSessionFactory%22%20ref%3D%22sqlSessionFactory%22%20%2F%3E%0A%20%20%20%20%20%20%20%E4%BC%9A%E5%AF%BC%E8%87%B4%E6%95%B0%E6%8D%AE%E6%BA%90%E9%85%8D%E7%BD%AE%E4%B8%8D%E7%AE%A1%E7%94%A8%EF%BC%8C%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E4%B8%8D%E4%B8%8A%E3%80%82%0A%20%20%20%20%20%20%20%E4%B8%94spring%204%E5%BC%83%E7%94%A8%0A%20%20%20%20%20%20%20%2D%2D%3E-->
    </property></property></bean>

</context:property-placeholder></beans>
</code>

逆向工程生成po類及mapper(單表增刪改查)

手動定義商品查詢mapper

針對綜合查詢mapper,一般情況會有關聯查詢,建議自定義mapper

ItemsMapperCustom.xml
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20%3F%2D%2D%3E-->

<mapper namespace="com.iot.learnssm.firstssm.mapper.ItemsMapperCustom">

   <!--{cke_protected}{C}%3C!%2D%2D%20%E5%AE%9A%E4%B9%89%E5%95%86%E5%93%81%E6%9F%A5%E8%AF%A2%E7%9A%84sql%E7%89%87%E6%AE%B5%EF%BC%8C%E5%B0%B1%E6%98%AF%E5%95%86%E5%93%81%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6%20%2D%2D%3E-->
   <sql id="query_items_where">
    <!--{cke_protected}{C}%3C!%2D%2D%20%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql%EF%BC%8C%E9%80%9A%E8%BF%87if%E5%88%A4%E6%96%AD%EF%BC%8C%E6%BB%A1%E8%B6%B3%E6%9D%A1%E4%BB%B6%E8%BF%9B%E8%A1%8Csql%E6%8B%BC%E6%8E%A5%20%2D%2D%3E-->
    <!--{cke_protected}{C}%3C!%2D%2D%20%E5%95%86%E5%93%81%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6%E9%80%9A%E8%BF%87ItemsQueryVo%E5%8C%85%E8%A3%85%E5%AF%B9%E8%B1%A1%20%E4%B8%ADitemsCustom%E5%B1%9E%E6%80%A7%E4%BC%A0%E9%80%92%20%2D%2D%3E-->
        <if test="itemsCustom!=null">
            <if test="itemsCustom.name!=null and itemsCustom.name!=''">
                items.name LIKE '%${itemsCustom.name}%'
            </if>
        </if>

   </sql>

    <!--{cke_protected}{C}%3C!%2D%2D%20%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8%E6%9F%A5%E8%AF%A2%20%2D%2D%3E-->
    <!--{cke_protected}{C}%3C!%2D%2D%20parameterType%E4%BC%A0%E5%85%A5%E5%8C%85%E8%A3%85%E5%AF%B9%E8%B1%A1(%E5%8C%85%E8%A3%85%E4%BA%86%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6)%0A%20%20%20%20%20%20%20%20resultType%E5%BB%BA%E8%AE%AE%E4%BD%BF%E7%94%A8%E6%89%A9%E5%B1%95%E5%AF%B9%E8%B1%A1%0A%20%20%20%20%20%2D%2D%3E-->
    <select id="findItemsList" parametertype="com.iot.learnssm.firstssm.po.ItemsQueryVo" resulttype="com.iot.learnssm.firstssm.po.ItemsCustom">
        SELECT items.* FROM items  
        
            
        
    </select>

</mapper></code>
ItemsMapperCustom.java
public interface ItemsMapperCustom {
    //商品查詢列表
    List findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}
po類ItemsCustom
package com.iot.learnssm.firstssm.po;

/**
 * Created by Brian on 2016/3/2.
 * 商品信息的擴展類
 */
public class ItemsCustom extends Items{
    //添加商品信息的擴展屬性
}
輸入pojo的包裝類
package com.iot.learnssm.firstssm.po;

/**
 * Created by Brian on 2016/3/2.
 */
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;
    }
}

整合好dao後的工程目錄如圖

springmvc_整合工程-2

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved