SSM=Maven build project and Maven sub module build project

Keywords: Spring Maven xml Mybatis

1, General knowledge of software development

(1) Project development process

1. Project approval
Project implementability analysis: (a project) 100W project, the development cycle is expected to be 6 months, 10 people are needed, and the operation (human) cost is 2W / person
The product manager stays in the bank to conduct demand research "demand design specification"
Requirements workshop: all team members attend

2. Design
UI/UE Engineer: page prototype
Architect: unified development environment, project technical options, project architecture design, database design, etc
Product Manager: interface specification, method specification, parameter specification, step requirements, etc. detailed design specification

3. Development
Coding, coding

4. Test
White box testing: code logic and performance
Black box test: user experience
Gray box test: Black + white

5. Deployment
Operation and maintenance personnel product deployment upload maintenance

(2) UML Unified Modeling Language

Unified Modeling Language (UML) is a standardized modeling language for object-oriented software.

Simply put: through some professional graphics, intuitively display project requirements (use case diagram, sequence diagram, class diagram), etc.

Use case diagram: user case

  • Graphical presentation of all the functions in the project, as well as the participants of the functions

Design tool PowerDesigner(PD)

  • Draw use case diagrams, databases, etc

(3) Development environment:

jdk version 1.8

maven version: 3.5.2

mysql version: 5.7

(4) Create a new workspace

Set up the sdk, and more:

Setting of overall project code

maven settings

 

2, Maven:

(1) Basic review

Maven project management tools

Dependency management, project construction

 

(2) The dependence transmission of Maven

What is dependency delivery

In maven, dependencies are transitive. Suppose there are three projects, project A, project B and project C. If A depends on B and B depends on C, then we can deduce that project A depends on C according to the characteristics of Maven project dependence.

If our project directly depends on too many coordinates, version conflicts may occur:

 

(3) How to solve the problem of dependency conflict?

1. Principle of dependency regulation:

First declaration priority principle:

Define dependency in pom file, and use the one who declares it first

 

Principle of priority for those who are close to each other

Define the dependency in the pom file, and use the path close to it

 

2. Exclude dependency

By excluding unnecessary dependent jar packages from pom files

3. Version locking

In the pom file, you can directly specify all version numbers that the project depends on. When importing a dependency, you do not need to specify a version

  1. All public version number constants defined in properties

  2. Locking version number through dependency management: dependent jar packages will not be imported

  3. Dependencies import dependencies: version number does not need to be specified

<!--1.Define version number constant-->
<properties>
    <spring.version>5.2.5.RELEASE</spring.version>
    <junit.version>4.12</junit.version>
</properties>
<!--2.Version lock-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<!--3.Dependency management-->
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
</dependencies>

3, Maven build ssm project

Build ssm project based on maven and standardize dependency management. Scenario: query all enterprise information

(1) Create database

(2) Create web module“

(3) Import dependency:

<!--Unified version number-->
    <properties>
        <spring.version>5.0.5.RELEASE</spring.version>
        <springmvc.version>5.0.5.RELEASE</springmvc.version>
        <mybatis.version>3.4.5</mybatis.version>
    </properties>

    <!--Lock dependent version-->
    <dependencyManagement>
        <dependencies>
            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>

            <!-- springMVC -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${springmvc.version}</version>
            </dependency>


            <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <!--Add dependency-->
    <dependencies>
        <!-- Mybatis and mybatis And spring Integration of -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- MySql drive -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>

        <!-- druid Database connection pool -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>

        <!-- springMVC core-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <!-- spring relevant -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <!-- junit test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
		
		<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
        <!-- Set build version to 1.8 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version >
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        </plugins>
    </build>

Note: auto import

(3) Build MyBatis

Company entity class:

@Data
public class Company implements Serializable {

    private String id; // Enterprise id
    private String name; // Enterprise name
    private Date expirationDate;  //Expiration date
    private String address; // Business address
    private String licenseId; // Business license No
    private String representative; // Legal representative
    private String phone; // cell-phone number
    private String companySize; // Enterprise scale
    private String industry; // industry
    private String remarks; // remarks
    private Integer state; // state
    private Double balance; // balance
    private String city; // city
}

CompanyDao interface and mapping file

public interface CompanyDao {
  
/ / query all
    List<Company> findAll();
}

At the same time, create the mapping file in the resources folder“

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wsl.dao.company.CompanyDao">

    <resultMap id="BaseResultMap" type="com.wsl.domain.company.Company">
        <id column="id" property="id"></id>
        <!--Change the different fields-->
        <result column="expiration_date" property="expirationDate"></result>
        <result column="license_id" property="licenseId"></result>
        <result column="company_size" property="companySize"></result>
    </resultMap>

    <select id="findAll" resultMap="BaseResultMap">
        SELECT * FROM ss_company
    </select>
</mapper>

spring integrates MyBatis

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/saas?characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=root

applicationContext-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">

    <!--druid Connection pool-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- Give Way spring Administration sqlsessionfactory use mybatis and spring In the integration package -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- Database connection pool -->
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.wsl.domain"></property>
    </bean>

    <!-- mapper Scanner: used to generate proxy objects-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wsl.dao"></property>
    </bean>
</beans>

Test:

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-dao.xml")
public class MyBatisTest {

    @Autowired
    private CompanyDao companyDao;

    @Test
    public void testFindAll()throws Exception{
        List<Company> list = companyDao.findAll();
        System.out.println(list);
    }
}

 

(4) Build spring

CompanyService interface and Implementation

public interface CompanyService {
    
    // Query all
    List<Company> findAll();
}

applicationContext-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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!--Enable annotation component scan-->
    <context:component-scan base-package="com.wsl.service"/>



</beans>

applicationContext-tx.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!--Configure transactions-->
    <!--1.Transaction manager-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--2.Transaction notification-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="save*" read-only="false" propagation="REQUIRED"/>
            <tx:method name="update*" read-only="false" propagation="REQUIRED"/>
            <tx:method name="delete*" read-only="false" propagation="REQUIRED"/>
            <tx:method name="*" read-only="false" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--3.aop-->
    <aop:config>
        <aop:pointcut id="pt" expression="execution( * com.wsl.service..*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
    </aop:config>

</beans>

Test:

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-*.xml")
public class SpringTest {

    @Autowired
    private CompanyService companyService;

    @Test
    public void testFindAll()throws Exception{
        List<Company> list = companyService.findAll();
        System.out.println(list);
    }
}

 

(5) Build spring MVC

1. Page resource import“

2.CompanyController and page

@Controller
@RequestMapping(value = "/company", name = "business management")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @RequestMapping(value = "/list", name = "Query list")
    public String list(HttpServletRequest request) {
        List<Company> list = companyService.findAll();
        request.setAttribute("list", list);
        return "company/company-list";
    }
}

3.spring-mvc.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:aop="http://www.springframework.org/schema/aop"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!--Enable annotation component scan-->
    <context:component-scan base-package="com.wsl.web"/>

    <!--mvc Annotation support-->
    <mvc:annotation-driven/>


    <!--view resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

4.web.xml

Front end controller, filter, listener

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<!--Front end controller-->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- Specifies the loaded profile, via the parameter contextConfigLocation load-->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>4</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<!-- Monitor monitors other spring configuration file -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!--
			classpath:Scan the configuration file under the current project class path
			classpath*:Scan the configuration files under the current project and dependent project class paths
		-->
		<param-value>classpath*:spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>



	<!-- solve post Disorderly code -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

5. Deployment test

See the beginning for the complete project structure

 

4, Maven sub module construction:

What is module building

There are two common ways to split:

  • Split according to the business module. Each module is divided into a maven project. For example, a project is divided into user module, order module, shopping cart module, etc. each module corresponds to a maven project

  • Split by layer, such as persistence layer, business layer and presentation layer. Each layer corresponds to a maven project

Regardless of the above split method, a parent project is usually provided to extract some common code and configuration into the parent project for unified management and configuration.

(1)Maven project inheritance

In the Java language, classes can inherit from each other. Through inheritance, subclasses can refer to non private properties and methods in the parent class.

Similarly, you can inherit between maven projects. After the child project inherits the parent project, you can use the dependency introduced in the parent project. The purpose of inheritance is to eliminate duplicate code.

Create parent project

Create submodule

(2)Maven engineering convergence

At maven pom.xml The < modules > tag can be used in the file to aggregate other Maven projects for unified operation.

Note that not subprojects can also be aggregated and managed in a unified way

In general, the parent project will act as an aggregation project to manage its own sub modules

 

5, Building SSM project by modules

Transforming the ssm project into sub module development

(1) Create a parent project

(2)export_commons

(3)export_domain

(4) export_dao

(5)export_system_dao

(6)export_manager_web

The idea of transfer dependency between submodules

Test:

Also configure server deployment

 

 

6, Maven private service

explain:

maven warehouse is divided into local warehouse and remote warehouse, while remote warehouse is divided into maven central warehouse, other remote warehouse and private server.

maven private server is the maven remote warehouse in the company's LAN. Each employee's computer installs maven software and connects with maven private server. Programmers can make their own projects into jars and publish them to the private server. Other project team members can download the dependent jars from the private server. The private server also acts as a proxy server. When there is no jar package on the private server, it will be automatically downloaded from the maven central warehouse.

For example: enter enterprise development, different project groups develop different projects

  • Team A: export_dao project is developed and released to private server.

  • Team B: export_system_service downloads export from private server_ Dao

(1) Setting up private service environment with nexus

Nexus is a maven Warehouse Manager (in fact, it is a software). Nexus can serve as Maven private server. At the same time, nexus also provides powerful warehouse management, component search and other functions

-Download:
	http://www.sonatype.org/nexus/archived/
	
-Installation:
	1. Decompression: nexus-2.14.10-01-bundle.zip
	2. Enter the bin directory after decompression and open the cmd window
	3. Execution nexus.bat  install
		Note: the cmd command needs to be run as an administrator
	
-Uninstall:
	cmd enters bin directory for execution nexus.bat  uninstall
		Note: the cmd command needs to be run as an administrator
		
-Start:
	Method 1: cmd enters the bin directory and executes nexus.bat  start
	Mode 2: start nexus in Windows system service
	
-Visit:
	http://localhost:8081/nexus
		User name: admin
		Password: admin123

 

(2) Warehouse type

As can be seen from the previous list of warehouses, nexus has many built-in warehouses by default. These warehouses can be divided into four types. Each type of warehouse is used to store specific jar packages. The specific instructions are as follows:

1. hosted
	Deploy your own jar to this type of warehouse, including Releases and Snapshots
		Releases is the internal release version warehouse of the company, and Snapshots is the internal test version warehouse of the company 

2. proxy, proxy warehouse
	It is used for agent remote public warehouse, such as maven central warehouse. The user connects to the private server, and the private server automatically goes to the central warehouse to download the jar package or plug-in

3. group, warehouse group
	It is used to merge multiple hosted/proxy warehouses. Usually, we configure our own maven connection warehouse group

4. Virtual: compatible with Maven 1 jar or plug-in

 

(3) Publish project to private server

At maven setting.xml Configuration in file:

<server>
    <id>releases</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>snapshots</id>
    <username>admin</username>
    <password>admin123</password>
</server>

When the pom.xml Configuration in file:

<distributionManagement>
    <repository>
        <id>releases</id>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

Execute deploy on the project to be published

Before deployment, it must be installed in the local warehouse to upload to the private server

 

(4) Download jar package from private server

At maven setting.xml Configuration in file

<profile>
    <id>dev</id>
    <repositories>
        <repository>
            <id>nexus</id>
            <!--Warehouse address, i.e nexus Address of the warehouse group-->
            <url>
                http://localhost:8081/nexus/content/groups/public/</url>
            <!--Download or not releases component-->
            <releases>
                <enabled>true</enabled>
            </releases>
            <!--Download or not snapshots component-->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <!-- Plug in repository, maven You need to download the plug-in from the private server -->
        <pluginRepository>
            <id>public</id>
            <name>Public Repositories</name>
            <url>
                http://localhost:8081/nexus/content/groups/public/
            </url>
        </pluginRepository>
    </pluginRepositories>
</profile>

activation

<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>

Note that in order to download the private server, you must first delete the local repository jar package

 

Data:

Private service construction: https://www.cnblogs.com/knowledgesea/p/11190579.html

https://blog.csdn.net/gyshun/article/details/79297460

maven sub module construction project: https://blog.csdn.net/javanotes/article/details/81128020

Posted by andybrooke on Mon, 08 Jun 2020 22:30:50 -0700