系列教程javao2o商城之(二)項目配置

本系列爲自己學習《Java主流技術棧SSM+SpringBoot商鋪系統》這個課程的一個記錄和分享。

1.爲什麼學習java

現在是前端,一直想成爲全棧,學過node和python之類的後臺語言,也寫過一些小項目 egg Django之類的框架也都可以使用,但是發現不能系統的入門,也加上感覺自己寫的代碼不是特別好,不能很好的使用面嚮對象語言的特性,所以就來學習java。同時對下一步上typescript有幫助。

2.計劃(同一個項目不同的語言去完成)

1.打算做三版不同語言的後臺:java、 node、 python
2.如果有需要做4款不同框架的前臺:vue、react、react-native、angular

3.項目配置

1.補全文件夾

2.剩餘各項配置

1.創建jdbc.properties

//目錄 src/main/resources/jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/javao2o?userUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=pp123456

2.創建mybatis-config.xml

// src/main/resources/mybatis-config.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>
    <!-- 配置全局屬性 -->
    <settings>
        <!-- 使用jdbc的getGeneratedKeys獲取數據庫自增主鍵值 -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- 使用列標籤替換列別名 默認:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- 開啓駝峯命名轉換:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

3.創建spring-dao.xml

//src/main/resources/spring/spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--配置整合mybatis過程-->
    <!--1:配置數據庫相關參數-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--2.數據庫連接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <!--配置連接池屬性-->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!--c3p0連接池的私有屬性-->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!--關閉連接不自動commit-->
        <property name="autoCommitOnClose" value="false"/>
        <!--獲取連接超時時間-->
        <property name="checkoutTimeout" value="3000"/>
        <!--當獲取鏈接失敗重試次數-->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!--3.配置SqlSessionFactory對象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入數據庫連接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--配置mybatis全局配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--掃描entity包 使用別名 可以簡化書寫 多個包時用;隔開-->
        <property name="typeAliasesPackage" value="wang.beastxw.javao2o.entity"/>
        <!--掃描sql配置文件:mapper需要的xml文件-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <!--4.配置掃描Dao接口包,動態實現dao接口,注入到spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--給出需要掃描dao接口包-->
        <property name="basePackage" value="wang.beastxw.javao2o.dao"/>
    </bean>
</beans>

4.創建spring-service.xml

// src/main/resources/spring/spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 掃描service包下所有使用註解的類型 -->
    <context:component-scan base-package="wang.beastxw.javao2o.service" />

    <!-- 配置事務管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入數據庫連接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置基於註解的聲明式事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

5.創建spring-web.xml

// src/main/resources/spring/spring-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 配置SpringMVC -->
    <!-- 1.開啓SpringMVC註解模式 -->
    <mvc:annotation-driven />

    <!-- 2.靜態資源默認servlet配置 (1)加入對靜態資源的處理:js,gif,png (2)允許使用"/"做整體映射 -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:default-servlet-handler />

    <!-- 3.定義視圖解析器 -->
    <!-- 不用jsp,通過結果返回數據,所以是.html -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/html/"></property>
        <property name="suffix" value=".html"></property>
    </bean>
  
    <!-- 4.掃描web相關的bean -->
    <context:component-scan base-package="wang.beastxw.javao2o.web" />
</beans>

6.修改web.xml

// 修改web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1" metadata-complete="true">
    <display-name>Archetype Created Web Application</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <!-- 默認匹配所有的請求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4.源碼

uri: https://github.com/Hericium/j...
分支: feature/startmvc

5.添加羣聊一起學習(698615299)!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章