Series Tutorial Project Configuration for javao2o Shop

Posted by bobthebullet990 on Sat, 31 Aug 2019 07:05:18 +0200

This series is a record and share of the course "Java Mainstream Technology Stack SSM+SpringBoot Shop System".

1. Why learn java

Now it's the front end, I've always wanted to be a full stack, I've learned background languages like node and python, and I've written a few small projects like egg Django that can also be used, but I find that I can't get started with the system, plus I don't feel that the code I write is particularly good, and I can't use object-oriented language very well.To learn java.It is also helpful for typescript on the next step.

2. Plan (to be completed in different languages for the same project)

1. I plan to do three different languages in the background: java, node, python
2. If you need to do 4 different frameworks: vue, react, react-native, angular

3. Project Configuration

1. Complete Folders

2. Remaining configurations

1. Create jdbc.properties

//Directory 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. Create 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>
    <!-- Configure Global Properties -->
    <settings>
        <!-- Use jdbc Of getGeneratedKeys Get Database Self-Increasing Primary Key Value -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- Replace column alias default with column labels:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- Turn on hump naming conversion:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

3. Create 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">
    <!--Configuration Integration mybatis process-->
    <!--1:Configure database related parameters-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--2.Database Connection Pool-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <!--Configure Connection Pool Properties-->
        <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 Private properties of connection pools-->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!--Close connection not automatically commit-->
        <property name="autoCommitOnClose" value="false"/>
        <!--Get connection timeout-->
        <property name="checkoutTimeout" value="3000"/>
        <!--Number of retries when getting links fails-->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!--3.To configure SqlSessionFactory object-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--Inject database connection pool-->
        <property name="dataSource" ref="dataSource"/>
        <!--To configure mybatis Global Profile-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--scanning entity Packages use aliases to simplify writing multiple packages;Separate-->
        <property name="typeAliasesPackage" value="wang.beastxw.javao2o.entity"/>
        <!--scanning sql configuration file:mapper Needed xml file-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <!--4.Configuration Scan Dao Interface packages, dynamic implementation dao Interface, injected into spring In container-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--injection sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--Give needed scan dao Interface Package-->
        <property name="basePackage" value="wang.beastxw.javao2o.dao"/>
    </bean>
</beans>

4. Create 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">
    <!-- scanning service All types of notes used under the package -->
    <context:component-scan base-package="wang.beastxw.javao2o.service" />

    <!-- Configure Transaction Manager -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- Inject database connection pool -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- Configuring annotation-based declarative transactions -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

5. Create 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">
    <!-- To configure SpringMVC -->
    <!-- 1.open SpringMVC Annotation mode -->
    <mvc:annotation-driven />

    <!-- 2.Static Resource Default servlet To configure (1)Join the processing of static resources: js,gif,png (2)Allow use"/"Map as a whole -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:default-servlet-handler />

    <!-- 3.Define View Parser -->
    <!-- No need jsp,Returns data from the result, so is.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.scanning web Dependent bean -->
    <context:component-scan base-package="wang.beastxw.javao2o.web" />
</beans>

6. Modify web.xml

// Modify 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>
        <!-- Match all requests by default -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4. Source Code

uri: https://github.com/Hericium/j...
Branch: feature/startmvc

5. Add group chat to learn together (698615299)!

Topics: Java Spring xml JDBC Mybatis