当前位置:   article > 正文

springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建_springboot api商城

springboot api商城

中国加油,武汉加油!

篇幅较长,配合目录观看

项目准备

  1. gitee地址 https://gitee.com/springboot-dubbo/nz1904-springboot-shop

1. 新建站点工程nz1904-shop(maven

1.1 删除src目录

1.2 新建shop-entity(module-maven)

1.3 新建shop-mapper(module-maven)

1.4 新建shop-service-api(module-maven)

1.5 新建shop-common(module-maven)

1.6 新建shop-service-iml(module-maven)

1.6.1 删掉src

1.6.2 新建user-service(module-springboot)

1.7 新建shop-web(module-maven)

1.7.1 删掉src

1.7.2 新建user-web(module-springboot)

  1. Spring Boot DevTool
  2. Spring Web
  3. Thymeleaf
    在这里插入图片描述

2. user-entity

2.1 导包

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.8</version>
</dependency>
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>2.3</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.2 编写实体类

package com.wpj.entity;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_user")
public class User implements Serializable {

  @TableId(type = IdType.AUTO)
  private Integer id;
  private String username;
  private String password;
  private Integer sex;
  private String email;
  @TableField(exist = false)
  private User createUser;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3. shop-mapper

3.1 导包

<dependency>
	<groupId>com.wpj</groupId>
	<artifactId>shop-entity</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
  • 1
  • 2
  • 3
  • 4

3.2 编写mapper接口

package com.wpj.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wpj.entity.User;

public interface IUserMapper extends BaseMapper<User> {
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4. shop-service-api

4.1 导包

 <dependency>
	<groupId>com.wpj</groupId>
	<artifactId>shop-entity</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>com.wpj</groupId>
	<artifactId>base</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.2 编写service接口

package com.wpj.service;

import com.wpj.entity.User;

public interface IUserService extends IBaseService<User> {
}
  • 1
  • 2
  • 3
  • 4
  • 5

5. shop-service-iml下的user-service

5.1 导包

<dependency>
	<groupId>com.wpj</groupId>
	<artifactId>shop-service-api</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>com.wpj</groupId>
	<artifactId>shop-mapper</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>0.2.0</version>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.48</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

5.2 编写service接口实现类

package com.wpj.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wpj.entity.User;
import com.wpj.mapper.IUserMapper;
import com.wpj.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    
    @Autowired
    private IUserMapper userMapper;
    
    @Override
    public BaseMapper<User> getMapper() {
        return userMapper;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

5.3 修改程序入口

package com.wpj;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@DubboComponentScan(basePackages = "com.wpj.service")
@MapperScan(basePackages = {"com.wpj.mapper"})
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

5.4 编写application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/nz1904-springboot-shop
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

dubbo:
  application:
    name: user-service
  registry:
    address: zookeeper://47.98.33.215:2181
  protocol:
    port: -1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

5.5 Test

package com.wpj.service.impl;

import com.wpj.entity.User;
import com.wpj.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestService {

    @Autowired
    private IUserService userService;

    @Test
    public void testAdd(){
        User user = new User();
        user.setUsername("admin");
        user.setPassword("admin");
        user.setEmail("admin@qq.com");
        user.setSex(1);
        int insert = userService.insert(user);
        System.out.println(insert);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

flag默认1
在这里插入图片描述

5.6 启动程序入口

在这里插入图片描述

6. shop-web下的user-web

6.1 导包

<dependency>
	<groupId>com.wpj</groupId>
	<artifactId>shop-service-api</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>0.2.0</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6.2 编写Controller

package com.wpj.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.wpj.entity.User;
import com.wpj.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    @Reference
    private IUserService userService;

    @RequestMapping("/getUserPage")
    public String getUserPage(Page<User> page, ModelMap map){
        page = userService.getDubboPage(page);
        map.put("url", "user/getUserPage");
        map.put("page",page);
        return "user/userList";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

6.3 修改程序入口

package com.wpj;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(scanBasePackages = "com.wpj", exclude = DataSourceAutoConfiguration.class)
public class UserWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserWebApplication.class, args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

6.4 编写application.yml

dubbo:
  application:
    name: user-web
  registry:
    address: zookeeper://47.98.33.215:2181
  consumer:
    check: false
    retries: 3

spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

6.5 导入静态资源

6.5.1 导入首页

6.5.2 导入userList

6.6 启动程序入口

在这里插入图片描述

7. 扩展 user-service-iml下的user-service

7.1 ServiceImpl重写getDubboPage方法

package com.wpj.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wpj.entity.User;
import com.wpj.mapper.IUserMapper;
import com.wpj.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Override
    public BaseMapper<User> getMapper() {
        return userMapper;
    }

    @Override
    public Page<User> getDubboPage(Page<User> page) {
        int current = page.getCurrent();
        int size = page.getSize();
        userMapper.selectPage(page, null);
        page.setRecords(userMapper.getUserList((current-1) * size, size));
        return page;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

7.2 往上创建mapper的方法

package com.wpj.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.wpj.entity.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface IUserMapper extends BaseMapper<User> {

    List<User> getUserList(@Param("index") int index, @Param("size") int size);

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

7.3 shop-mapper添加mapper.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.wpj.mapper.IUserMapper">


    <resultMap id="userResultMap" type="user">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="sex" property="sex"/>
        <result column="email" property="email"/>

        <association property="createUser" javaType="user">
            <id column="create_user" property="id"/>
            <result column="create_username" property="username"/>
        </association>
    </resultMap>

    <select id="getUserPage" resultMap="userResultMap">
			SELECT
				u1.*,
			  u2.username as create_username
			FROM
				t_user u1
			LEFT JOIN t_user u2 ON (u1.create_user = u2.id)
	</select>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

7.4 user-service添加配置

mybatis-plus:
  type-aliases-package: com.wpj.entity
  mapper-locations: classpath:mapper/*.xml
  • 1
  • 2

7.5 test

@Test
public void getUserPage(){
    Page<User> page = new Page<>();
    page = userService.getDubboPage(page);
    System.out.println(page);
}
  • 1
  • 2
  • 3
  • 4
  • 5

7.6 修改userList

<td th:text="${user.createUser.username}"/>

    7.7 重启程序入口

    在这里插入图片描述
    在这里插入图片描述

    7.8 编写page.html

    7.9 修改userList

    <div th:replace="/common/page::pageDiv"></div>

      7.10 user-service编写插件配置

      package com.wpj.config;
      
      import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      public class PagePlusConfig {
          @Bean
          public PaginationInterceptor paginationInterceptor(){
              return new PaginationInterceptor();
          }
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12

      7.10 修改程序入口

      @SpringBootApplication(scanBasePackages = "com.wpj")

        7.11 重启程序入口

        在这里插入图片描述

        声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/143786
        推荐阅读
        相关标签
          

        闽ICP备14008679号