前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dubbo 分布式架构搭建教育 PC 站 - 后端:用户模块,课程模块

Dubbo 分布式架构搭建教育 PC 站 - 后端:用户模块,课程模块

作者头像
RendaZhang
修改2020-11-04 15:20:39
6460
修改2020-11-04 15:20:39
举报
文章被收录于专栏:Renda
用户模块

实体类的编写没有任何技术含量,而且还浪费时间。

生成代码的解决方案有很多种:企业中比较常见的还有 MyBatis 的逆向工程。

修改 Easy Code 插件的作者名称:IDEA 的 Settings -> Other Settings -> Easy Code -> 修改新的作者名称。

使用 EASY CODE 插件可以快速生成 entitydaoservicemapper 等文件:

  • IDEA 安装最新版的 Easy Code 插件来自动生成代码;
  • 在 IDEA 的 Database --> 选择 edu 数据库的 user 表 --> 右键 --> EasyCode --> Generate Code;
  • 弹出对话框询问是否添加数据库类型 bit(1) 的映射关系,选择 No;
  • Group 为 Default,Module 为 lagou-edu-service,Template 勾选 entity.java、dao.java、service.java、serviceImpl.java、mapper.xml;
  • 点击 OK,开始自动生成代码。
  • 把自动生成的 User 实体类移动到 lagou-edu-eneitysrc/main/java 目录下,包名为 com.renda.entity
  • 把自动生成的 UserDao 移动到 lagout-edu-daosrc/main/java 目录下,包名为 com.renda.mapper;把自动生成的 resources 目录下的 mapper/UserDao.xml 移动到 lagout-edu-daosrc/main/resources/com/renda 的目录下。
  • 把 service 目录下自动生成的文件移动到 lagout-edu-servicecom.renda.user 包下,以形成包名 com.renda.user
  • 根据报错修改一下自动生成的代码,Maven Compile 一下看看是否报错。
用户登录 / 注册

功能描述:用户输入手机号、密码,点击登录,调用后端接口开始登录;如果该手机号未注册,则自动注册并登录。

功能接口:/user/login

`lagou-edu-entity`

使用 Lombok 小辣椒,使用注解取代原来冗余的 get 和 set、空构造、全参数构造。

pom.xml

代码语言:javascript
复制
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

com.renda.entity.User

把原来的 getter 和 setter 删除,保留成员变量,然后使用 Lombok 注解来取代:

代码语言:javascript
复制
@Data // 生成 getter 和 setter
@AllArgsConstructor // 生成全参数的构造方法
@NoArgsConstructor // 生成空构造方法
@ToString // 生成 toString 方法
public class User implements Serializable {

    private static final long serialVersionUID = -48156882162112782L;
    ...   
}

用户数据响应的封装类 com.renda.entity.UserDTO

代码语言:javascript
复制
package com.renda.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class UserDTO<User> implements Serializable {

    private static final long serialVersionUID = 1L;
    private int state;  // 操作状态
    private String message;  // 状态描述
    private User content;  // 响应内容

}
`lagou-edu-dao`

src\main\resources\mybatis\mybatis-config.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 后台的日志输出:针对开发者-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
</configuration>

src\main\resources\spring\spring-dao.xml

代码语言:javascript
复制
<?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: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/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">

    <!-- 1.扫描包 -->
    <context:component-scan base-package="com.renda.mapper"/>

    <!-- 2.数据连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql://192.168.186.128:3306/edu?serverTimezone=GMT&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="RendaZhang@666"/>
        <property name="maxActive" value="10"/>
        <property name="minIdle" value="5"/>
    </bean>

    <!-- 3.sqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    </bean>

    <!-- 4.事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 5.开启事务 -->
    <tx:annotation-driven/>

    <!-- 整合 MyBatis -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.renda.mapper"/>
    </bean>

</beans>

删除掉 UserDao 自动生成的代码,增加用户登录注册方法:

代码语言:javascript
复制
@Service
public interface UserDao {

    /**
     * 用户登录
     *
     * @param phone    手机号
     * @param password 密码
     * @return 用户对象
     */
    User login(@Param("phone") String phone, @Param("password") String password);

    /**
     * 检查手机号是否注册过
     *
     * @param phone 手机号
     * @return 0:未注册 , 1:已注册
     */
    Integer checkPhone(String phone);

    /**
     * 用户注册
     *
     * @param phone    手机号
     * @param password 密码
     * @param nickname 昵称
     * @param headimg  头像
     * @return 受影响的行数
     */
    Integer register(@Param("phone") String phone, @Param("password") String password,
                     @Param("nickname") String nickname, @Param("headimg") String headimg);

}

保留 resultMapper,删除掉 UserDao.xml 自动生成的代码,增加登录注册查询:

代码语言:javascript
复制
<?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.renda.mapper.UserDao">

    <resultMap type="com.renda.entity.User" id="UserMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="portrait" column="portrait" jdbcType="VARCHAR"/>
        <result property="phone" column="phone" jdbcType="VARCHAR"/>
        <result property="password" column="password" jdbcType="VARCHAR"/>
        <result property="regIp" column="reg_ip" jdbcType="VARCHAR"/>
        <result property="accountNonExpired" column="account_non_expired" jdbcType="OTHER"/>
        <result property="credentialsNonExpired" column="credentials_non_expired" jdbcType="OTHER"/>
        <result property="accountNonLocked" column="account_non_locked" jdbcType="OTHER"/>
        <result property="status" column="status" jdbcType="VARCHAR"/>
        <result property="isDel" column="is_del" jdbcType="OTHER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <select id="login" resultMap="UserMap">
        select * from `user` where phone = #{phone} and password = #{password}
    </select>

    <select id="checkPhone" resultType="integer">
        select count(*) from `user` where phone = #{phone}
    </select>

    <insert id="register">
        insert  into `user`
        (`name`,portrait,phone,password,create_time,update_time)
        values
        (#{nickname},#{headimg},#{phone},#{password},sysdate(),sysdate())
    </insert>

</mapper>

增加测试类测试登录注册方法 src\test\java\user\TestUser.java

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-dao.xml"})
public class TestUser {

    @Autowired
    private UserDao userDao;

    @Test
    public void login(){
        User user = userDao.login("110", "123");
        System.out.println("user = " + user);
    }

    @Test
    public void checkPhone(){
        Integer i = userDao.checkPhone("110");
        // 0:未注册 , 1:已注册
        System.out.println("i = " + i);
    }

    @Test
    public void register(){
        Integer i = userDao.register("114", "123", "renda", "");
        // 0:注册失败 , 1:注册成功
        System.out.println("i = " + i);
    }

}
`lagou-edu-service`

src\main\resources\spring\spring-service.xml

代码语言:javascript
复制
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 服务提供方在 zookeeper 中的“别名” -->
    <dubbo:application name="lagou-edu-service"/>

    <!-- 注册中心的地址 -->
    <dubbo:registry address="zookeeper://192.168.186.128:2181"/>

    <!-- 扫描类(将什么包下的类作为服务提供类)-->
    <dubbo:annotation package="com.renda"/>

    <dubbo:provider timeout="60000"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="192.168.186.128"/>
        <property name="port" value="6379"/>
    </bean>

</beans>

删除掉 UserServiceUserServiceImpl 自动生成的代码,增加用户登录注册方法:

代码语言:javascript
复制
public interface UserService {
    User login(String phone, String password);

    Integer checkPhone(String phone);

    Integer register(String phone, String password, String nickname, String headimg);
}

@Service // dubbo 暴露服务:让消费者能够找到我
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User login(String phone, String password) {
        return userDao.login(phone, password);
    }

    @Override
    public Integer checkPhone(String phone) {
        return userDao.checkPhone(phone);
    }

    @Override
    public Integer register(String phone, String password, String nickname, String headimg) {
        return userDao.register(phone, password, nickname, headimg);
    }

}

增加测试类测试登录注册方法 src\test\java\user\TestUser.java

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring/spring-*.xml"})
public class TestUser {

    @Autowired
    private UserService userService;

    @Test
    public void login(){
        User user = userService.login("110", "123");
        System.out.println("user = " + user);
    }

    @Test
    public void checkPhone() {
        Integer i = userService.checkPhone("110");
        System.out.println("i = " + i); //0:未注册 , 1:已注册
    }

}

src\main\webapp\WEB-INF\web.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/spring-*.xml</param-value>
    </context-param>
</web-app>

使用 Tomcat 插件 8001 端口:

代码语言:javascript
复制
...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8001</port>
                <path>/</path>
            </configuration>
            <executions>
                <execution>
                    <!-- 打包完成后,运行服务 -->
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
...

使用 Maven 在 lagou-edu-parent 下执行 install 命令在本地安装部署好需要依赖的 jar 包,然后在 lagou-edu-service 下执行 package 命令启动服务。

`lagou-edu-web`

创建对应的接口来引用要消费的服务 com.renda.user.UserService(确保包名与提供的服务的包名一致,否则会出现空指针异常):

代码语言:javascript
复制
public interface UserService {
    User login(String phone, String password);

    Integer checkPhone(String phone);

    Integer register(String phone, String password,String nickname,String headimg);
}

com.renda.user.controller.UserController

代码语言:javascript
复制
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference // dubbo 远程消费
    private UserService userService;

    @GetMapping("/login")
    public UserDTO login(String phone, String password, String nickname, String heading) {
        UserDTO userDTO = new UserDTO();
        User user;

        if (nickname == null || nickname.isEmpty()) {
            nickname = phone;
        }

        System.out.println(phone);
        System.out.println(password);
        System.out.println(nickname);

        // 检测手机号是否注册
        if ( == userService.checkPhone(phone)) {
            // 未注册,先自动注册,然后登录
            userService.register(phone, password, nickname, heading);
            userDTO.setMessage("手机号没有被注册,现已自动注册");
            user = userService.login(phone, password);
        } else {
            // 已经注册,直接进行登录
            user = userService.login(phone, password);
            if (user == null) {
                userDTO.setState();
                userDTO.setMessage("帐号密码不匹配,登录失败!");
            } else {
                userDTO.setState();
                userDTO.setMessage("登录成功!");
            }
        }

        userDTO.setContent(user);
        return userDTO;
    }

}

src\main\webapp\index.jsp

代码语言:javascript
复制
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/login">
    <p><input type="text" placeholder="请输入手机号..." name="phone"></p>
    <p><input type="text" placeholder="请输入密码..." name="password"></p>
    <p>
        <button>登录</button>
    </p>
</form>
</body>
</html>

使用 Tomcat 插件 8002 端口:

代码语言:javascript
复制
...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8002</port>
                <path>/</path>
            </configuration>
            <executions>
                <execution>
                    <!-- 打包完成后,运行服务 -->
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
...

启动 dubbo-admin 来监控消费者和提供者的状态。

使用 Maven 在 lagou-edu-web 下执行 package 命令启动消费者;同时确保服务提供者也启动完毕。

浏览器访问:http://localhost:8002/,输入已有的用户名和密码测试登录功能,输入新的用户名和密码测试注册功能,控制台和网页查看输出结果。

课程模块

course 课程

teacher 老师

activity_course 课程活动

course_section 章节

course_lesson 课时

course_media 课节视频

一个 course 对一个 teacher

一个 course 对一个 activity_course

一个 course 对多个 course_section

一个 course_section 对多个 course_lesson

一个 course_lesson 对多个 course_media

全部课程

功能描述:用户在未登录状态,获取所有上架的课程,课程顺序为 - 配置活动信息标签的优先显示,再根据课程显示序列号顺序显示;序列号相同按照创建时间倒序显示,课程都是未购买状态。

功能接口:/course/getAllCourse

`lagou-edu-entity`

使用 Easy Code 快速生成对应的实体类,再使用 Lombok 注解取代原来冗余的 get 和 set、空构造、全参数构造。

com.renda.entity.Course

代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Course implements Serializable {

    private static final long serialVersionUID = 337303050449398216L;

    // 一门课程对应一个讲师
    private Teacher teacher;
    //一门课程对应多个章节
    private List<CourseSection> courseSections;
    // 一门课对一个活动
    private ActivityCourse activityCourse;

    ...
}

com.renda.entity.Teacher

代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Teacher implements Serializable {
    private static final long serialVersionUID = -97051504830274283L;

    ...
}

com.renda.entity.ActivityCourse

代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ActivityCourse implements Serializable {
    private static final long serialVersionUID = 576079752103902168L;

    ...   
}

com.renda.entity.CourseSection

代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CourseSection implements Serializable {
    private static final long serialVersionUID = -40082782487572703L;

    // 一章对应多个小节
    private List<CourseLesson> courseLessons;

    ...
}

com.renda.entity.CourseLesson

代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CourseLesson implements Serializable {
    private static final long serialVersionUID = -29342440566142798L;

    // 一小节课对应一个视频
    private CourseMedia courseMedia;

    ...
}

com.renda.entity.CourseMedia

代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CourseMedia implements Serializable {
    private static final long serialVersionUID = -93086504227451781L;

    ...
}
`lagou-edu-dao`

使用 Easy Code 快速生成 Course 的 mapper 文件,然后进行修改。

src\main\resources\com\renda\mapper\CourseDao.xml

代码语言:javascript
复制
<?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.renda.mapper.CourseDao">

    <resultMap type="com.renda.entity.Course" id="CourseMap">
        <result property="id" column="c_id" jdbcType="OTHER"/>
        <result property="courseName" column="course_name" jdbcType="VARCHAR"/>
        <result property="brief" column="brief" jdbcType="VARCHAR"/>
        <result property="price" column="price" jdbcType="OTHER"/>
        <result property="priceTag" column="price_tag" jdbcType="VARCHAR"/>
        <result property="discounts" column="discounts" jdbcType="OTHER"/>
        <result property="discountsTag" column="discounts_tag" jdbcType="VARCHAR"/>
        <result property="courseDescriptionMarkDown" column="course_description_mark_down" jdbcType="OTHER"/>
        <result property="courseDescription" column="course_description" jdbcType="OTHER"/>
        <result property="courseImgUrl" column="course_img_url" jdbcType="VARCHAR"/>
        <result property="isNew" column="is_new" jdbcType="OTHER"/>
        <result property="isNewDes" column="is_new_des" jdbcType="VARCHAR"/>
        <result property="lastOperatorId" column="last_operator_id" jdbcType="INTEGER"/>
        <result property="autoOnlineTime" column="auto_online_time" jdbcType="TIMESTAMP"/>
        <result property="createTime" column="c_create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="c_update_time" jdbcType="TIMESTAMP"/>
        <result property="isDel" column="c_is_del" jdbcType="OTHER"/>
        <result property="totalDuration" column="total_duration" jdbcType="INTEGER"/>
        <result property="courseListImg" column="course_list_img" jdbcType="VARCHAR"/>
        <result property="status" column="c_status" jdbcType="INTEGER"/>
        <result property="sortNum" column="sort_num" jdbcType="INTEGER"/>
        <result property="previewFirstField" column="preview_first_field" jdbcType="VARCHAR"/>
        <result property="previewSecondField" column="preview_second_field" jdbcType="VARCHAR"/>
        <result property="sales" column="sales" jdbcType="INTEGER"/>
        <!-- 一个老师 -->
        <association property="teacher" javaType="com.renda.entity.Teacher">
            <result property="id" column="t_id" jdbcType="OTHER"/>
            <result property="courseId" column="t_course_id" jdbcType="INTEGER"/>
            <result property="teacherName" column="teacher_name" jdbcType="VARCHAR"/>
            <result property="position" column="position" jdbcType="VARCHAR"/>
            <result property="description" column="t_description" jdbcType="VARCHAR"/>
            <result property="createTime" column="t_create_time" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="t_update_time" jdbcType="TIMESTAMP"/>
            <result property="isDel" column="t_is_del" jdbcType="OTHER"/>
        </association>
        <!-- 一个活动课程 -->
        <association property="activityCourse" javaType="com.renda.entity.ActivityCourse">
            <result property="id" column="ac_id" jdbcType="INTEGER"/>
            <result property="courseId" column="ac_course_id" jdbcType="INTEGER"/>
            <result property="beginTime" column="begin_time" jdbcType="TIMESTAMP"/>
            <result property="endTime" column="end_time" jdbcType="TIMESTAMP"/>
            <result property="amount" column="amount" jdbcType="OTHER"/>
            <result property="stock" column="stock" jdbcType="INTEGER"/>
            <result property="status" column="ac_status" jdbcType="OTHER"/>
            <result property="isDel" column="ac_is_del" jdbcType="OTHER"/>
            <result property="remark" column="remark" jdbcType="VARCHAR"/>
            <result property="createTime" column="ac_create_time" jdbcType="TIMESTAMP"/>
            <result property="createUser" column="create_user" jdbcType="VARCHAR"/>
            <result property="updateTime" column="ac_update_time" jdbcType="TIMESTAMP"/>
            <result property="updateUser" column="update_user" jdbcType="VARCHAR"/>
        </association>
        <!-- 多个章节 -->
        <collection property="courseSections" ofType="com.renda.entity.CourseSection">
            <result property="id" column="cs_id" jdbcType="OTHER"/>
            <result property="courseId" column="cs_course_id" jdbcType="INTEGER"/>
            <result property="sectionName" column="section_name" jdbcType="VARCHAR"/>
            <result property="description" column="cs_description" jdbcType="VARCHAR"/>
            <result property="createTime" column="cs_create_time" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="cs_update_time" jdbcType="TIMESTAMP"/>
            <result property="isDe" column="is_de" jdbcType="OTHER"/>
            <result property="orderNum" column="cs_order_num" jdbcType="INTEGER"/>
            <result property="status" column="cs_status" jdbcType="INTEGER"/>
            <!-- 多个小节 -->
            <collection property="courseLessons" ofType="com.renda.entity.CourseLesson">
                <result property="id" column="cl_id" jdbcType="OTHER"/>
                <result property="courseId" column="cl_course_id" jdbcType="INTEGER"/>
                <result property="sectionId" column="cl_section_id" jdbcType="INTEGER"/>
                <result property="theme" column="theme" jdbcType="VARCHAR"/>
                <result property="duration" column="cl_duration" jdbcType="INTEGER"/>
                <result property="isFree" column="is_free" jdbcType="OTHER"/>
                <result property="createTime" column="cl_create_time" jdbcType="TIMESTAMP"/>
                <result property="updateTime" column="cl_update_time" jdbcType="TIMESTAMP"/>
                <result property="isDel" column="cl_is_del" jdbcType="OTHER"/>
                <result property="orderNum" column="cl_order_num" jdbcType="INTEGER"/>
                <result property="status" column="cl_status" jdbcType="INTEGER"/>
                <!-- 一个小节视频 -->
                <association property="courseMedia" javaType="com.renda.entity.CourseMedia">
                    <result property="id" column="cm_id" jdbcType="INTEGER"/>
                    <result property="courseId" column="cm_course_id" jdbcType="INTEGER"/>
                    <result property="sectionId" column="cm_section_id" jdbcType="INTEGER"/>
                    <result property="lessonId" column="cm_lesson_id" jdbcType="INTEGER"/>
                    <result property="coverImageUrl" column="cover_image_url" jdbcType="VARCHAR"/>
                    <result property="duration" column="cm_duration" jdbcType="VARCHAR"/>
                    <result property="fileEdk" column="file_edk" jdbcType="VARCHAR"/>
                    <result property="fileSize" column="file_size" jdbcType="OTHER"/>
                    <result property="fileName" column="file_name" jdbcType="VARCHAR"/>
                    <result property="fileDk" column="file_dk" jdbcType="VARCHAR"/>
                    <result property="createTime" column="cm_create_time" jdbcType="TIMESTAMP"/>
                    <result property="updateTime" column="cm_update_time" jdbcType="TIMESTAMP"/>
                    <result property="isDel" column="cm_is_del" jdbcType="OTHER"/>
                    <result property="durationNum" column="duration_num" jdbcType="INTEGER"/>
                    <result property="fileId" column="file_id" jdbcType="VARCHAR"/>
                </association>
            </collection>
        </collection>
    </resultMap>

    <sql id="courseInfo">
    select
        c.id c_id,`course_name`,`brief`,`price`,`price_tag`,`discounts`,`discounts_tag`,`course_description_mark_down`,
        `course_description`,`course_img_url`,`is_new`,`is_new_des`,`last_operator_id`,`auto_online_time`,
        c.create_time c_create_time,c.update_time c_update_time,c.is_del c_is_del,`total_duration`,`course_list_img`,
        c.status c_status,`sort_num`,`preview_first_field`,`preview_second_field`,`sales`,
        t.id t_id, t.course_id t_course_id,`teacher_name`,`position`,t.description t_description,t.create_time t_create_time,
        t.update_time t_update_time,t.is_del t_is_del,
        cs.id cs_id, cs.course_id cs_course_id,`section_name`, cs.description cs_description,cs.create_time cs_create_time,
        cs.update_time cs_update_time,`is_de`,cs.order_num cs_order_num ,cs.status cs_status,
        cl.id cl_id,cl.course_id cl_course_id,cl.section_id cl_section_id, `theme`,cl.duration cl_duration,`is_free`,
        cl.create_time cl_create_time,cl.update_time cl_update_time, cl.is_del cl_is_del,cl.order_num cl_order_num,
        cl.status cl_status,
        cm.id cm_id,cm.course_id cm_course_id, cm.section_id cm_section_id ,cm.lesson_id cm_lesson_id,`cover_image_url`,
        cm.duration cm_duration,`file_edk`, `file_size`,`file_name`,`file_dk`,cm.create_time cm_create_time,
        cm.update_time cm_update_time,cm.is_del cm_is_del, `duration_num`,`file_id`, `begin_time`,`end_time`,`amount`,`stock`,
        ac.status ac_status,ac.is_del ac_is_del,`remark`,ac.create_time ac_create_time,`create_user`,ac.update_time ac_update_time,`update_user`
    from activity_course ac right join course c on c.id = ac.course_id
                            inner join teacher t on c.id = t.course_id
                            inner join course_section cs on c.id = cs.course_id
                            inner join course_lesson cl on cl.section_id = cs.id
                            left join course_media cm on cm.lesson_id = cl.id
    </sql>

    <select id="getAllCourse" resultMap="CourseMap">
        <include refid="courseInfo"/>
        order by amount desc, c_id, ac_create_time desc
    </select>

</mapper>

com.renda.mapper.CourseDao

代码语言:javascript
复制
public interface CourseDao {
    /**
     * 查询全部课程信息
     */
    List<Course> getAllCourse();
}

src\test\java\course\TestCourse.java

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-dao.xml"})
public class TestCourse {

    @Autowired
    private CourseDao courseDao;

    @Test
    public void getAllCourse() {
        List<Course> list = courseDao.getAllCourse();
        for (Course course : list) {
            String flag = course.getActivityCourse() != null ? "【火爆活动中】" : "";
            System.out.println("课程:" + flag + course.getId() + "->" + course.getCourseName());
            for (CourseSection cs : course.getCourseSections()) {
                System.out.println("\t\t章节" + cs.getId() + "--->" + cs.getSectionName());
                for (CourseLesson cl : cs.getCourseLessons()) {
                    if (cl.getCourseMedia() != null) {
                        System.out.println("\t\t\t小节" + cl.getId() + "--->" + cl.getTheme()
                                + ",视频:" + cl.getCourseMedia().getFileId() + ",时长:【"
                                + cl.getCourseMedia().getDuration() + "】");
                    } else {
                        System.out.println("\t\t\t小节" + cl.getId() + "--->" + cl.getTheme()
                                + ",视频:【待上传】,时长:【00:00】");
                    }
                }
            }
        }
    }

}
`lagou-edu-service`

com.renda.course.CourseService

代码语言:javascript
复制
public interface CourseService {
    /**
     * 查询全部课程信息
     */
    List<Course> getAllCourse();
}

com.renda.course.impl.CourseServiceImpl

代码语言:javascript
复制
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;

    @Override
    public List<Course> getAllCourse() {
        return courseDao.getAllCourse();
    }
}
`lagou-edu-web`

com.renda.course.CourseService

代码语言:javascript
复制
public interface CourseService {
    List<Course> getAllCourse();
}

com.renda.course.controller.CourseController

代码语言:javascript
复制
@RestController
@RequestMapping("/course")
public class CourseController {
    // 远程消费
    @Reference
    private CourseService courseService;

    @GetMapping("/getAllCourse")
    public List<Course> getAllCourse() {
        return courseService.getAllCourse();
    }
}

浏览器访问:http://localhost:8002/course/getAllCourse

已购课程

功能描述:1、用户未登录则显示为空;2、用户登录,通过用户 ID 调用服务获取已购课程。

功能接口:/course/getCourseByUserId/{userid}

`lagou-edu-dao`

src\main\resources\com\renda\mapper\CourseDao.xml

代码语言:javascript
复制
...
<select id="getCourseByUserId" resultMap="CourseMap">
    <include refid="courseInfo"/>
    where c.id in
    (select course_id from user_course_order where status = 20 and is_del = 0 and user_id = #{userId})
    order by amount desc, c_id, ac_create_time desc
</select>
...

com.renda.mapper.CourseDao

代码语言:javascript
复制
public interface CourseDao {
    ...

    /**
     * 查询已登录用户购买的全部课程信息
     * @return
     */
    List<Course> getCourseByUserId(@Param("userId") String userId);
}

src\test\java\course\TestCourse.java

代码语言:javascript
复制
...
@Test
public void getCoursesByUserid() {
    List<Course> list = courseDao.getCourseByUserId("100030018");
    for (Course course : list) {
        String flag = course.getActivityCourse() != null ? "【火爆活动中】" : "";
        System.out.println("课程:" + flag + course.getId() + "->" + course.getCourseName());
        for (CourseSection cs : course.getCourseSections()) {
            System.out.println("\t\t章节" + cs.getId() + "--->" + cs.getSectionName());
            for (CourseLesson cl : cs.getCourseLessons()) {
                if (cl.getCourseMedia() != null) {
                    System.out.println("\t\t\t小节" + cl.getId() + "--->" + cl.getTheme()
                                       + ",视频:" + cl.getCourseMedia().getFileId()
                                       + ",时长:【" + cl.getCourseMedia().getDuration() + "】");
                } else {
                    System.out.println("\t\t\t小节" + cl.getId() + "--->" + cl.getTheme()
                                       + ",视频:【待上传】,时长:【00:00】");
                }
            }
        }
    }
}
...
`lagou-edu-service`

com.renda.course.CourseService

代码语言:javascript
复制
public interface CourseService {
    ...
    List<Course> getCourseByUserId(String userId);
}

com.renda.course.impl.CourseServiceImpl

代码语言:javascript
复制
@Service
public class CourseServiceImpl implements CourseService {
    ...
    @Override
    public List<Course> getCourseByUserId(String userId) {
        return courseDao.getCourseByUserId(userId);
    }
}
`lagou-edu-web`

com.renda.course.CourseService

代码语言:javascript
复制
public interface CourseService {
    ...
    List<Course> getCourseByUserId(String userId);
}

com.renda.course.controller.CourseController

代码语言:javascript
复制
@RestController
@RequestMapping("/course")
public class CourseController {
    ...

    @GetMapping("getCourseByUserId/{userid}")
    public List<Course> getCourseByUserId(@PathVariable("userid") String userid) {
        return courseService.getCourseByUserId(userid);
    }
}

浏览器访问:http://localhost:8002/course/getCourseByUserId/100030018

课程详情

功能描述:

1、用户如果没有登录,通过课程 ID 获取课程的详细信息中会显示“立即购买”按钮。点击够买按钮会显示登录。同时会展示课程目录,课程信息和老师信息。注意课程信息是展示字段course_description 中的信息。

2、用户登录后,我们通过课程Id获取课程信息后,会通过用户 ID 和课程 ID 调用订单服务来获取来判断用户是否购买,如果够买则显示“立即学习”,否则显示“立即够买”,同时会展示课程目录,课程信息和老师信息。注意课程信息是展示字段 course_description 中的信息。

功能接口:/course/getCourseById/{courseid}

`lagou-edu-dao`

src\main\resources\com\renda\mapper\CourseDao.xml

代码语言:javascript
复制
...
<select id="getCourseById" resultMap="CourseMap">
    <include refid="courseInfo"/>
    where c.id = #{courseid}
</select>
...

com.renda.mapper.CourseDao

代码语言:javascript
复制
public interface CourseDao {
    ...

    /**
     * 查询某门课程的详细信息
     *
     * @param courseid 课程编号
     */
    Course getCourseById(@Param("courseid") Integer courseid);
}

src\test\java\course\TestCourse.java

代码语言:javascript
复制
...
@Test
public void getCourseById() {
    Course course = courseDao.getCourseById();
    String flag = course.getActivityCourse() != null ? "【火爆活动中】" : "";
    System.out.println("课程:" + flag + course.getId() + "->" + course.getCourseName());
    for (CourseSection cs : course.getCourseSections()) {
        System.out.println("\t\t章节" + cs.getId() + "--->" + cs.getSectionName());
        for (CourseLesson cl : cs.getCourseLessons()) {
            if (cl.getCourseMedia() != null) {
                System.out.println("\t\t\t小节" + cl.getId() + "--->" + cl.getTheme()
                                   + ",视频:" + cl.getCourseMedia().getFileId()
                                   + ",时长:【" + cl.getCourseMedia().getDuration() + "】");
            } else {
                System.out.println("\t\t\t小节" + cl.getId() + "--->" + cl.getTheme()
                                   + ",视频:【待上传】,时长:【00:00】");
            }
        }
    }
}
...
`lagou-edu-service`

com.renda.course.CourseService

代码语言:javascript
复制
public interface CourseService {
    ...
    Course getCourseById(Integer courseid);
}

com.renda.course.impl.CourseServiceImpl

代码语言:javascript
复制
@Service
public class CourseServiceImpl implements CourseService {
    ...
    @Override
    public Course getCourseById(Integer courseid) {
        return courseDao.getCourseById(courseid);
    }
}
`lagou-edu-web`

com.renda.course.CourseService

代码语言:javascript
复制
public interface CourseService {
    ...
    Course getCourseById(Integer courseid);
}

com.renda.course.controller.CourseController

代码语言:javascript
复制
@RestController
@RequestMapping("/course")
public class CourseController {
    ...
    @GetMapping("getCourseById/{courseid}")
    public Course getCourseById(@PathVariable("courseid") Integer courseid) {
        return courseService.getCourseById(courseid);
    }
}

浏览器访问:http://localhost:8002/course/getCourseById/7

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-10-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Renda 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 用户模块
    • 用户登录 / 注册
      • `lagou-edu-entity`
      • `lagou-edu-dao`
      • `lagou-edu-service`
      • `lagou-edu-web`
  • 课程模块
    • 全部课程
      • `lagou-edu-entity`
      • `lagou-edu-dao`
      • `lagou-edu-service`
      • `lagou-edu-web`
    • 已购课程
      • `lagou-edu-dao`
      • `lagou-edu-service`
      • `lagou-edu-web`
    • 课程详情
      • `lagou-edu-dao`
      • `lagou-edu-service`
      • `lagou-edu-web`
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档