spring-springmvc-mybatis專案介紹

pysasuke發表於2017-09-04

spring-springmvc-mybatis專案介紹

單純的spring整合springmvc+mybatis,整合所需算是最簡配置
專案程式碼獲取:https://github.com/pysasuke/s…

專案結構

 main

  • controller:控制層,UserController展示了兩種返回而型別情況:跳轉頁面和返回物件

    //@RequestMapping("getUser")
    @RequestMapping(value = "getUser", method = RequestMethod.GET)
    public String getUser(@RequestParam("id") Long id, Model model) {
        User user = userServicre.getById(id);
        model.addAttribute("user", user);
        return "user";
    }

    @RequestMapping("getById")
    @ResponseBody
    /*
    POJO物件要轉成Json,則要求POJO中的屬性必須都有getter方法
    需要有json對應的包
    不加返回時406報錯:
    The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.-->
    */
    public User getById(@RequestParam("id") Long id) {
        User user = userServicre.getById(id);
        return user;
    }
  • service:業務處理層,包含一個impl包,Service以介面型別存在,impl包下存放Service介面的實現類

  • dao:資料庫互動層

  • model:實體物件層

resources

  • application.xml:spring配置檔案入口,載入spring-config.xml

  • spring-mvc.xml:springmvc配置相關檔案

    <!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 -->
    <context:component-scan base-package="com.py.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--註解方式(處理請求)-->
    <mvc:annotation-driven/>
 
    <!--靜態資源預設servlet配置a
        1、加入對靜態資源的處理:js,css,gif,png
        2、允許使用"/"做整體對映
    -->
    <mvc:default-servlet-handler/>
     <!-- 靜態資源處理  css js imgs 可以直接訪問而不被攔截-->
    <mvc:resources mapping="/html/**" location="/WEB-INF/html/"/>

    <!-- 定義跳轉的檔案的前字尾 ,檢視模式配置  解析控制層return "index" 一類的操作-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
  • spring-config.xml:載入其他整合的配置檔案,這裡載入spring-mybatis.xml和db.properties

  • spring-mybatis.xml:mybatis相關配置檔案

    <!-- 自動掃描(自動注入) -->
    <context:component-scan base-package="com.py.*"/>

    <!-- 配置資料來源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
        <!-- 初始化連線大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 連線池最大數量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 連線池最大空閒 -->
        <!--<property name="maxIdle" value="${maxIdle}"></property>-->
        <!-- 連線池最小空閒 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 獲取連線最大等待時間 -->
        <property name="maxWait" value="${maxWait}"></property>

        <property name="filters" value="stat,log4j,wall"/>

    </bean>


    <!-- myBatis檔案 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自動掃描mapping.xml檔案 -->
        <!--以mapper命名時報錯:
        org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)-->
        <!--<property name="mapperLocations" value="classpath*:mapping/*.xml"/>-->
        <property name="mapperLocations" value="classpath*:mapping/*.xml"/>
    </bean>

    <!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.py.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
  • db.properties:資料庫相關引數

  • mapping:存放mybatis對映檔案,以UserMapper.xml為例

<!--與dao中的介面類對應-->
<mapper namespace="com.py.dao.UserMapper">

    <select id="getById" resultType="com.py.model.User">
        select id,username,password,email from user where id=#{id,jdbcType=BIGINT}
    </select>

</mapper>

webapp

  • web.xml

    <!-- SpringMVC核心 -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Spring的配置檔案 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:application.xml</param-value>
    </context-param>

    <!-- Spring監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- SpringMVC攔截設定 -->
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- 由SpringMVC攔截所有請求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- SpringMVC攔截設定結束 -->

相關文章