spring-springmvc-mybatis項目介紹

spring-springmvc-mybatis項目介紹

單純的spring整合springmvc+mybatis,整合所需算是最簡配置
項目代碼獲取:https://github.com/pysasuke/s...css

項目結構

 main

  • controller:控制層,UserController展現了兩種返回而類型狀況:跳轉頁面和返回對象html

//@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接口的實現類git

  • dao:數據庫交互層github

  • model:實體對象層web

resources

  • application.xml:spring配置文件入口,加載spring-config.xmlspring

  • spring-mvc.xml:springmvc配置相關文件sql

<!-- 自動掃描該包,使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
        一、加入對靜態資源的處理:js,css,gif,png
        二、容許使用"/"作總體映射
    -->
    <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相關配置文件express

<!-- 自動掃描(自動注入) -->
    <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:數據庫相關參數apache

  • 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攔截設置結束 -->
相關文章
相關標籤/搜索