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

springboot项目

  • **中国加油,武汉加油!**
  • 篇幅较长,配合目录观看
  • 项目准备
  • 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)
  • 2. user-entity
    • 2.1 导包
    • 2.2 编写实体类
  • 3. shop-mapper
    • 3.1 导包
    • 3.2 编写mapper接口
  • 4. shop-service-api
    • 4.1 导包
    • 4.2 编写service接口
  • 5. shop-service-iml下的user-service
    • 5.1 导包
    • 5.2 编写service接口实现类
    • 5.3 修改程序入口
    • 5.4 编写application.yml
    • 5.5 Test
    • 5.6 启动程序入口
  • 6. shop-web下的user-web
    • 6.1 导包
    • 6.2 编写Controller
    • 6.3 修改程序入口
    • 6.4 编写application.yml
    • 6.5 导入静态资源
      • 6.5.1 导入首页
      • 6.5.2 导入userList
    • 6.6 启动程序入口
  • 7. 扩展 user-service-iml下的user-service
    • 7.1 ServiceImpl重写getDubboPage方法
    • 7.2 往上创建mapper的方法
    • 7.3 shop-mapper添加mapper.xml
    • 7.4 user-service添加配置
    • 7.5 test
    • 7.6 修改userList
    • 7.7 重启程序入口
    • 7.8 编写page.html
    • 7.9 修改userList
    • 7.10 user-service编写插件配置
    • 7.10 修改程序入口
    • 7.11 重启程序入口

中国加油,武汉加油!

篇幅较长,配合目录观看

项目准备

  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
    springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建_第1张图片

2. user-entity

2.1 导包

<dependency>
	<groupId>org.projectlombokgroupId>
	<artifactId>lombokartifactId>
	<version>1.18.8version>
dependency>
<dependency>
	<groupId>com.baomidougroupId>
	<artifactId>mybatis-plus-boot-starterartifactId>
	<version>2.3version>
dependency>

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;
}

3. shop-mapper

3.1 导包

<dependency>
	<groupId>com.wpjgroupId>
	<artifactId>shop-entityartifactId>
	<version>1.0-SNAPSHOTversion>
dependency>

3.2 编写mapper接口

package com.wpj.mapper;

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

public interface IUserMapper extends BaseMapper<User> {
}

4. shop-service-api

4.1 导包

 <dependency>
	<groupId>com.wpjgroupId>
	<artifactId>shop-entityartifactId>
	<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
	<groupId>com.wpjgroupId>
	<artifactId>baseartifactId>
	<version>1.0-SNAPSHOTversion>
dependency>

4.2 编写service接口

package com.wpj.service;

import com.wpj.entity.User;

public interface IUserService extends IBaseService<User> {
}

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

5.1 导包

<dependency>
	<groupId>com.wpjgroupId>
	<artifactId>shop-service-apiartifactId>
	<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
	<groupId>com.wpjgroupId>
	<artifactId>shop-mapperartifactId>
	<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
	<groupId>com.alibaba.bootgroupId>
	<artifactId>dubbo-spring-boot-starterartifactId>
	<version>0.2.0version>
dependency>
<dependency>
	<groupId>mysqlgroupId>
	<artifactId>mysql-connector-javaartifactId>
	<version>5.1.48version>
dependency>

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;
    }
}

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);
    }
}

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

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("[email protected]");
        user.setSex(1);
        int insert = userService.insert(user);
        System.out.println(insert);
    }
}

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

5.6 启动程序入口

springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建_第2张图片

6. shop-web下的user-web

6.1 导包

<dependency>
	<groupId>com.wpjgroupId>
	<artifactId>shop-service-apiartifactId>
	<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
	<groupId>com.alibaba.bootgroupId>
	<artifactId>dubbo-spring-boot-starterartifactId>
	<version>0.2.0version>
dependency>

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";
    }
}

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);
    }

}

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/

6.5 导入静态资源

6.5.1 导入首页

6.5.2 导入userList

6.6 启动程序入口

springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建_第3张图片

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;
    }
}

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);

}

7.3 shop-mapper添加mapper.xml



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

7.4 user-service添加配置

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

7.5 test

@Test
public void getUserPage(){
    Page<User> page = new Page<>();
    page = userService.getDubboPage(page);
    System.out.println(page);
}

7.6 修改userList

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

7.7 重启程序入口

springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建_第4张图片
springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建_第5张图片

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();
    }
}

7.10 修改程序入口

@SpringBootApplication(scanBasePackages = "com.wpj")

7.11 重启程序入口

springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建_第6张图片

你可能感兴趣的:(springboot)