II. Construction of project development framework

Keywords: Maven Java Spring snapshot

 

 

Create a new project first

 

Select maven project creation

 

Delete the src folder. It's just a shell. It's useless.

Continue to create a maven project in the shell you just created

Not as a parent project, just as a module aggregation here

Then there are more subdirectories.

Same operation, continue to add other modules

 

 BaseDao

import java.util.List;

public interface BaseDao<T> {
    int deleteByPrimaryKey(Long id);

    int insert(T t);

    int insertSelective(T t);

    T selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(T t);

    int updateByPrimaryKey(T t);

     List<T> list();
}

 BaseService

import java.util.List;

public interface BaseService<T> {
    int deleteByPrimaryKey(Long id);

    int insert(T t);

    int insertSelective(T t);

    T selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(T t);

    int updateByPrimaryKey(T t);

    List<T> list();
}

 BaseServiceImpl

import java.util.List;

public abstract class BaseServiceImpl<T> implements BaseService<T> {
    public abstract BaseDao<T> getBaseDao();
    @Override
    public int deleteByPrimaryKey(Long id) {
        return getBaseDao().deleteByPrimaryKey(id);
    }
    @Override
    public int insert(T t) {
        return getBaseDao().insert(t);
    }
    @Override
    public int insertSelective(T t) {
        return getBaseDao().insertSelective(t);
    }
    @Override
    public T selectByPrimaryKey(Long id) {
        return  getBaseDao().selectByPrimaryKey(id);
    }
    @Override
    public int updateByPrimaryKeySelective(T t) {
        return getBaseDao().updateByPrimaryKeySelective(t);
    }
    @Override
    public int updateByPrimaryKey(T t) {
        return getBaseDao().updateByPrimaryKey(t);
    }
    @Override
    public List<T> list() {
        return getBaseDao().list();
    }
}

Then create a springBoot project under LH shop service

Create a springBoot module under LH shop web, the same as above

Write an example test environment

Design a database table first

CREATE TABLE `product_type` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Commodity category primary key',
  `pid` bigint(20) NOT NULL COMMENT 'Product category parent node',
  `name` varchar(50) COLLATE utf8_general_mysql500_ci DEFAULT NULL COMMENT 'Commodity category name',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_mysql500_ci;

Write entity classes and mapper files, which I generate automatically

Because the persistence layer uses the content in entity classes and common, you need to add references to the persistence layer, reference related dependencies, and specify the location of mapper.xml.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lh</groupId>
    <artifactId>lh-shop-mapper</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>com.lh</groupId>
            <artifactId>lh-shop-entity</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.lh</groupId>
            <artifactId>lh-shop-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

 

ProductType

package com.lh.entity;

import java.io.Serializable;

public class ProductType implements Serializable{
    private Long id;

    private Long pid;

    private String name;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getPid() {
        return pid;
    }
    public void setPid(Long pid) {
        this.pid = pid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
}

ProductTypeMapper

public interface ProductTypeMapper extends BaseDao<ProductType>{
}

ProductTypeMapper.xml

<?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.lh.mapper.ProductTypeMapper">
  <resultMap id="BaseResultMap" type="com.lh.entity.ProductType">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="pid" jdbcType="BIGINT" property="pid" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <sql id="Base_Column_List">
    id, pid, name
  </sql>

  <select id="list" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from product_type
  </select>

  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from product_type
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from product_type
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.lh.entity.ProductType">
    insert into product_type (id, pid, name)
    values (#{id,jdbcType=BIGINT}, #{pid,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.lh.entity.ProductType">
    insert into product_type
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="pid != null">
        pid,
      </if>
      <if test="name != null">
        name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="pid != null">
        #{pid,jdbcType=BIGINT},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.lh.entity.ProductType">
    update product_type
    <set>
      <if test="pid != null">
        pid = #{pid,jdbcType=BIGINT},
      </if>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lh.entity.ProductType">
    update product_type
    set pid = #{pid,jdbcType=BIGINT},
      name = #{name,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>

Then add some information to the database table

 

Then start to write the interface, because common and entity classes are also used in the interface, so we need to add dependency in pom.xml, api module and service module as well.

 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lh</groupId>
    <artifactId>lh-shop-product-api</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.lh</groupId>
            <artifactId>lh-shop-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.lh</groupId>
            <artifactId>lh-shop-entity</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

IProductTypeService

public interface IProductTypeService extends BaseService<ProductType> {
}

In the service module, we also need to add dependency and dubbo dependency.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.9.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.lh</groupId>
   <artifactId>lh-shop-product-service</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>lh-shop-product-service</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>com.lh</groupId>
         <artifactId>lh-shop-api</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>

      <dependency>
         <groupId>com.lh</groupId>
         <artifactId>lh-shop-entity</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>

      <dependency>
         <groupId>com.lh</groupId>
         <artifactId>lh-shop-common</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>

      <dependency>
         <groupId>com.lh</groupId>
         <artifactId>lh-shop-mapper</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>com.lh</groupId>
         <artifactId>lh-shop-product-api</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>


      <!-- Add to dubbo Dependence -->
      <dependency>
         <groupId>com.alibaba.boot</groupId>
         <artifactId>dubbo-spring-boot-starter</artifactId>
         <version>0.2.0</version>
      </dependency>

   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

ProductTypeService

import com.alibaba.dubbo.config.annotation.Service;
import com.lh.api.product.IProductTypeService;
import com.lh.entity.ProductType;
import com.lh.mapper.ProductTypeMapper;
import com.lh.shop.common.base.BaseDao;
import com.lh.shop.common.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Service
public class ProductTypeService extends BaseServiceImpl<ProductType> implements IProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Override
    public BaseDao<ProductType> getBaseDao() {
        return productTypeMapper;
    }
}

Write web module, introduce dubbo and api dependency

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.9.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.lh</groupId>
   <artifactId>lh-shop-background</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>lh-shop-background</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>com.lh</groupId>
         <artifactId>lh-shop-product-api</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>


      <!-- Add to dubbo Dependence -->
      <dependency>
         <groupId>com.alibaba.boot</groupId>
         <artifactId>dubbo-spring-boot-starter</artifactId>
         <version>0.2.0</version>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

Writing controller layer in web module

ProductTypeController

package com.lh.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lh.api.product.IProductTypeService;
import com.lh.entity.ProductType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("productType")
public class ProductTypeController {
    @Reference
    private IProductTypeService productTypeService;

    @GetMapping("list")
    public List<ProductType> list(){
        return productTypeService.list();
    }
}

Then write the web startup class and the service startup class.

Then write configuration files for both

Then start the service startup class and the web startup class, and then visit the web page.

Test complete!

Posted by scampbell on Sat, 26 Oct 2019 19:56:23 -0700