本文主要是介绍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 重启程序入口
中国加油,武汉加油!
篇幅较长,配合目录观看
项目准备
- 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)
- Spring Boot DevTool
- Spring Web
- 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>
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.wpj</groupId><artifactId>shop-entity</artifactId><version>1.0-SNAPSHOT</version>
</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.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>
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.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>
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 {@Autowiredprivate IUserMapper userMapper;@Overridepublic 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-shopusername: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driverdubbo:application:name: user-serviceregistry:address: zookeeper://47.98.33.215:2181protocol: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 {@Autowiredprivate IUserService userService;@Testpublic 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);}
}
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>
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 {@Referenceprivate 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-webregistry:address: zookeeper://47.98.33.215:2181consumer:check: falseretries: 3spring:thymeleaf:cache: falseprefix: classpath:/templates/
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 {@Autowiredprivate IUserMapper userMapper;@Overridepublic BaseMapper<User> getMapper() {return userMapper;}@Overridepublic 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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//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">SELECTu1.*,u2.username as create_usernameFROMt_user u1LEFT JOIN t_user u2 ON (u1.create_user = u2.id)</select>
</mapper>
7.4 user-service添加配置
mybatis-plus:type-aliases-package: com.wpj.entitymapper-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 重启程序入口
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 {@Beanpublic PaginationInterceptor paginationInterceptor(){return new PaginationInterceptor();}
}
7.10 修改程序入口
@SpringBootApplication(scanBasePackages = "com.wpj")
7.11 重启程序入口
这篇关于springboot篇】二十一. 基于springboot电商项目 一 商场环境搭建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!