select * from course
where price=0
order by score desc,learn_count desc
limit 0,5;
依赖的传递(导自己创建的JAR包)
1.先创建一个公共工程ovls-common项目(jar包)
项目共同的类包等都可以放这里
把整个项目所需要的包都导到这个里面,其他工程调用这一个包就可以了
刷新
代码
4.0.0
cn.xdl
ovls-common
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
2.0.5.RELEASE
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.springframework.boot
spring-boot-starter-data-redis
2.0.1.RELEASE
org.springframework.boot
spring-boot-starter-jdbc
2.0.5.RELEASE
org.apache.tomcat
jasper
6.0.29
mysql
mysql-connector-java
5.1.43
org.springframework.boot
spring-boot-starter-test
2.0.5.RELEASE
需要的话直接引入他就可以了
2.添加配置文件applicaion.yml在resources包中
注意:跟上一级空一个字符
在值前也空一个字符
其他调用这个的配置文件的配置内容不能重复,要全部删掉,值留下连接端口就可以
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/ovls?userUnicode=true&characterEncoding=utf8
driverClassName: com.mysql.jdbc.Driver
3.user需要修改一下pom.xml
4.0.0
cn.xdl
ovls-user
0.0.1-SNAPSHOT
cn.xdl
ovls-common
0.0.1-SNAPSHOT
4.user需要修改配置文件
server.port=7001
mvn compile 编译源代码
mvn deploy 发布项目
mvn test 运行应用程序中的单元测试
mvn clean 清除项目目录中的生成结果
mvn package 根据项目生成的jar
mvn install 在本地Repository中安装jar
流程:
1.在ovls-common引公共的jar包
2.添加配置文件(只需要写端口)
3.添加启动类CourseRunBoot
4.添加生成好的实体类Course到entity包
5.添加生成好的dao接口CourseMapper
6.添加生成好的实现类CourseSqlProvider
7.添加测试TestCourseMapper
8.添加服务CourseService接口
9.添加实现类CourseServiceImpl
10.添加控制器CourseController
1.在ovls-common引公共的jar包
4.0.0
cn.xdl
ovls-course
0.0.1-SNAPSHOT
cn.xdl
ovls-common
0.0.1-SNAPSHOT
2.添加配置文件(只需要写端口)
server.port=7002
3.在ovls包中添加启动类CourseRunBoot
package cn.xdl.ovls;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
//启动程序包含@Configuration、@EnableAutoConfiguration、@ComponentScan
通常用在主类上
@SpringBootApplication
// 为MapperScan添加动态扫描(占位符)功能
@MapperScan(basePackages={"cn.xdl.ovls.dao"})
//扫描ajax跨域请求过滤器的扫描器公共的必须在这个包里面Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。
@ServletComponentScan
public class CourseRunBoot {
public static void main(String[] args) {
SpringApplication.run(CourseRunBoot.class, args);
}
}
4.添加生成好的实体类Course到entity包
package cn.xdl.ovls.entity;
import java.util.Date;
public class Course {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private Integer id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.name
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private String name;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.intro
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private String intro;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.learn_count
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private Integer learnCount;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.difficulty
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private String difficulty;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.score
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private Integer score;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.image
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private String image;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.publish_time
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private Date publishTime;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.price
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private Integer price;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.subject_id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private Integer subjectId;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.direction_id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private Integer directionId;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column course.learn_time
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
private Integer learnTime;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.id
*
* @return the value of course.id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public Integer getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.id
*
* @param id the value for course.id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setId(Integer id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.name
*
* @return the value of course.name
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public String getName() {
return name;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.name
*
* @param name the value for course.name
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.intro
*
* @return the value of course.intro
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public String getIntro() {
return intro;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.intro
*
* @param intro the value for course.intro
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setIntro(String intro) {
this.intro = intro == null ? null : intro.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.learn_count
*
* @return the value of course.learn_count
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public Integer getLearnCount() {
return learnCount;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.learn_count
*
* @param learnCount the value for course.learn_count
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setLearnCount(Integer learnCount) {
this.learnCount = learnCount;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.difficulty
*
* @return the value of course.difficulty
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public String getDifficulty() {
return difficulty;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.difficulty
*
* @param difficulty the value for course.difficulty
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setDifficulty(String difficulty) {
this.difficulty = difficulty == null ? null : difficulty.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.score
*
* @return the value of course.score
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public Integer getScore() {
return score;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.score
*
* @param score the value for course.score
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setScore(Integer score) {
this.score = score;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.image
*
* @return the value of course.image
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public String getImage() {
return image;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.image
*
* @param image the value for course.image
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setImage(String image) {
this.image = image == null ? null : image.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.publish_time
*
* @return the value of course.publish_time
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public Date getPublishTime() {
return publishTime;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.publish_time
*
* @param publishTime the value for course.publish_time
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setPublishTime(Date publishTime) {
this.publishTime = publishTime;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.price
*
* @return the value of course.price
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public Integer getPrice() {
return price;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.price
*
* @param price the value for course.price
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setPrice(Integer price) {
this.price = price;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.subject_id
*
* @return the value of course.subject_id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public Integer getSubjectId() {
return subjectId;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.subject_id
*
* @param subjectId the value for course.subject_id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setSubjectId(Integer subjectId) {
this.subjectId = subjectId;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.direction_id
*
* @return the value of course.direction_id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public Integer getDirectionId() {
return directionId;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.direction_id
*
* @param directionId the value for course.direction_id
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setDirectionId(Integer directionId) {
this.directionId = directionId;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column course.learn_time
*
* @return the value of course.learn_time
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public Integer getLearnTime() {
return learnTime;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column course.learn_time
*
* @param learnTime the value for course.learn_time
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public void setLearnTime(Integer learnTime) {
this.learnTime = learnTime;
}
}
5.添加生成好的dao接口CourseMapper
package cn.xdl.ovls.dao;
import cn.xdl.ovls.entity.Course;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
public interface CourseMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table course
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
@Delete({
"delete from course",
"where id = #{id,jdbcType=INTEGER}"
})
int deleteByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table course
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
@Insert({
"insert into course (id, name, ",
"intro, learn_count, ",
"difficulty, score, ",
"image, publish_time, ",
"price, subject_id, ",
"direction_id, learn_time)",
"values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, ",
"#{intro,jdbcType=VARCHAR}, #{learnCount,jdbcType=INTEGER}, ",
"#{difficulty,jdbcType=VARCHAR}, #{score,jdbcType=INTEGER}, ",
"#{image,jdbcType=VARCHAR}, #{publishTime,jdbcType=TIMESTAMP}, ",
"#{price,jdbcType=INTEGER}, #{subjectId,jdbcType=INTEGER}, ",
"#{directionId,jdbcType=INTEGER}, #{learnTime,jdbcType=INTEGER})"
})
int insert(Course record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table course
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
@InsertProvider(type=CourseSqlProvider.class, method="insertSelective")
int insertSelective(Course record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table course
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
@Select({
"select",
"id, name, intro, learn_count, difficulty, score, image, publish_time, price, ",
"subject_id, direction_id, learn_time",
"from course",
"where id = #{id,jdbcType=INTEGER}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="intro", property="intro", jdbcType=JdbcType.VARCHAR),
@Result(column="learn_count", property="learnCount", jdbcType=JdbcType.INTEGER),
@Result(column="difficulty", property="difficulty", jdbcType=JdbcType.VARCHAR),
@Result(column="score", property="score", jdbcType=JdbcType.INTEGER),
@Result(column="image", property="image", jdbcType=JdbcType.VARCHAR),
@Result(column="publish_time", property="publishTime", jdbcType=JdbcType.TIMESTAMP),
@Result(column="price", property="price", jdbcType=JdbcType.INTEGER),
@Result(column="subject_id", property="subjectId", jdbcType=JdbcType.INTEGER),
@Result(column="direction_id", property="directionId", jdbcType=JdbcType.INTEGER),
@Result(column="learn_time", property="learnTime", jdbcType=JdbcType.INTEGER)
})
Course selectByPrimaryKey(Integer id);
/**
* 免费好课
*/
@Select({
"select",
"id, name, intro, learn_count, difficulty, score, image, publish_time, price, ",
"subject_id, direction_id, learn_time",
"from course",
"where price = 0",
"order by score desc,learn_count desc"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="intro", property="intro", jdbcType=JdbcType.VARCHAR),
@Result(column="learn_count", property="learnCount", jdbcType=JdbcType.INTEGER),
@Result(column="difficulty", property="difficulty", jdbcType=JdbcType.VARCHAR),
@Result(column="score", property="score", jdbcType=JdbcType.INTEGER),
@Result(column="image", property="image", jdbcType=JdbcType.VARCHAR),
@Result(column="publish_time", property="publishTime", jdbcType=JdbcType.TIMESTAMP),
@Result(column="price", property="price", jdbcType=JdbcType.INTEGER),
@Result(column="subject_id", property="subjectId", jdbcType=JdbcType.INTEGER),
@Result(column="direction_id", property="directionId", jdbcType=JdbcType.INTEGER),
@Result(column="learn_time", property="learnTime", jdbcType=JdbcType.INTEGER)
})
List selectByPrice();
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table course
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
@UpdateProvider(type=CourseSqlProvider.class, method="updateByPrimaryKeySelective")
int updateByPrimaryKeySelective(Course record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table course
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
@Update({
"update course",
"set name = #{name,jdbcType=VARCHAR},",
"intro = #{intro,jdbcType=VARCHAR},",
"learn_count = #{learnCount,jdbcType=INTEGER},",
"difficulty = #{difficulty,jdbcType=VARCHAR},",
"score = #{score,jdbcType=INTEGER},",
"image = #{image,jdbcType=VARCHAR},",
"publish_time = #{publishTime,jdbcType=TIMESTAMP},",
"price = #{price,jdbcType=INTEGER},",
"subject_id = #{subjectId,jdbcType=INTEGER},",
"direction_id = #{directionId,jdbcType=INTEGER},",
"learn_time = #{learnTime,jdbcType=INTEGER}",
"where id = #{id,jdbcType=INTEGER}"
})
int updateByPrimaryKey(Course record);
}
添加内容为
/**
* 免费好课
*/
@Select({
"select",
"id, name, intro, learn_count, difficulty, score, image, publish_time, price, ",
"subject_id, direction_id, learn_time",
"from course",
"where price = 0",
"order by score desc,learn_count desc"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="intro", property="intro", jdbcType=JdbcType.VARCHAR),
@Result(column="learn_count", property="learnCount", jdbcType=JdbcType.INTEGER),
@Result(column="difficulty", property="difficulty", jdbcType=JdbcType.VARCHAR),
@Result(column="score", property="score", jdbcType=JdbcType.INTEGER),
@Result(column="image", property="image", jdbcType=JdbcType.VARCHAR),
@Result(column="publish_time", property="publishTime", jdbcType=JdbcType.TIMESTAMP),
@Result(column="price", property="price", jdbcType=JdbcType.INTEGER),
@Result(column="subject_id", property="subjectId", jdbcType=JdbcType.INTEGER),
@Result(column="direction_id", property="directionId", jdbcType=JdbcType.INTEGER),
@Result(column="learn_time", property="learnTime", jdbcType=JdbcType.INTEGER)
})
List selectByPrice();
6.添加生成好的实现类CourseSqlProvider
package cn.xdl.ovls.dao;
import cn.xdl.ovls.entity.Course;
import org.apache.ibatis.jdbc.SQL;
public class CourseSqlProvider {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table course
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public String insertSelective(Course record) {
SQL sql = new SQL();
sql.INSERT_INTO("course");
if (record.getId() != null) {
sql.VALUES("id", "#{id,jdbcType=INTEGER}");
}
if (record.getName() != null) {
sql.VALUES("name", "#{name,jdbcType=VARCHAR}");
}
if (record.getIntro() != null) {
sql.VALUES("intro", "#{intro,jdbcType=VARCHAR}");
}
if (record.getLearnCount() != null) {
sql.VALUES("learn_count", "#{learnCount,jdbcType=INTEGER}");
}
if (record.getDifficulty() != null) {
sql.VALUES("difficulty", "#{difficulty,jdbcType=VARCHAR}");
}
if (record.getScore() != null) {
sql.VALUES("score", "#{score,jdbcType=INTEGER}");
}
if (record.getImage() != null) {
sql.VALUES("image", "#{image,jdbcType=VARCHAR}");
}
if (record.getPublishTime() != null) {
sql.VALUES("publish_time", "#{publishTime,jdbcType=TIMESTAMP}");
}
if (record.getPrice() != null) {
sql.VALUES("price", "#{price,jdbcType=INTEGER}");
}
if (record.getSubjectId() != null) {
sql.VALUES("subject_id", "#{subjectId,jdbcType=INTEGER}");
}
if (record.getDirectionId() != null) {
sql.VALUES("direction_id", "#{directionId,jdbcType=INTEGER}");
}
if (record.getLearnTime() != null) {
sql.VALUES("learn_time", "#{learnTime,jdbcType=INTEGER}");
}
return sql.toString();
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table course
*
* @mbg.generated Mon Dec 10 14:18:46 CST 2018
*/
public String updateByPrimaryKeySelective(Course record) {
SQL sql = new SQL();
sql.UPDATE("course");
if (record.getName() != null) {
sql.SET("name = #{name,jdbcType=VARCHAR}");
}
if (record.getIntro() != null) {
sql.SET("intro = #{intro,jdbcType=VARCHAR}");
}
if (record.getLearnCount() != null) {
sql.SET("learn_count = #{learnCount,jdbcType=INTEGER}");
}
if (record.getDifficulty() != null) {
sql.SET("difficulty = #{difficulty,jdbcType=VARCHAR}");
}
if (record.getScore() != null) {
sql.SET("score = #{score,jdbcType=INTEGER}");
}
if (record.getImage() != null) {
sql.SET("image = #{image,jdbcType=VARCHAR}");
}
if (record.getPublishTime() != null) {
sql.SET("publish_time = #{publishTime,jdbcType=TIMESTAMP}");
}
if (record.getPrice() != null) {
sql.SET("price = #{price,jdbcType=INTEGER}");
}
if (record.getSubjectId() != null) {
sql.SET("subject_id = #{subjectId,jdbcType=INTEGER}");
}
if (record.getDirectionId() != null) {
sql.SET("direction_id = #{directionId,jdbcType=INTEGER}");
}
if (record.getLearnTime() != null) {
sql.SET("learn_time = #{learnTime,jdbcType=INTEGER}");
}
sql.WHERE("id = #{id,jdbcType=INTEGER}");
return sql.toString();
}
}
7.添加测试TestCourseMapper
import java.util.List;
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;
import com.github.pagehelper.PageHelper;
import cn.xdl.ovls.CourseRunBoot;
import cn.xdl.ovls.dao.CourseMapper;
import cn.xdl.ovls.entity.Course;
@RunWith(SpringRunner.class)
@SpringBootTest(classes={CourseRunBoot.class})
public class TestCourseMapper {
@Autowired
private CourseMapper courseDao;
@Test
public void test1(){
//分页显示,第一页5条
PageHelper.startPage(1,5);
List list = courseDao.selectByPrice();
for(Course c:list){
System.out.println(c.getId()+" "+c.getName());
}
}
}
package cn.xdl.ovls.service;
import cn.xdl.ovls.util.OvlsResult;
public interface CourseService {
public OvlsResult loadFreeCourse(int size);
}
9.添加实现类CourseServiceImpl
package cn.xdl.ovls.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import cn.xdl.ovls.dao.CourseMapper;
import cn.xdl.ovls.entity.Course;
import cn.xdl.ovls.util.OvlsConstant;
import cn.xdl.ovls.util.OvlsResult;
@Service//扫描
public class CourseServiceImpl implements CourseService {
@Autowired//注入
private CourseMapper courseDao;
public OvlsResult loadFreeCourse(int size) {
//分页显示
PageHelper.startPage(1,size);
//查询价格的集合
List list = courseDao.selectByPrice();
//构造返回值result
OvlsResult result = new OvlsResult();
//注意,这里要去OvlsResult定义个private Object data;还有去OvlsConstant这里定义个参数
result.setStatus(OvlsConstant.SUCCESS);
//设置一个data及返回消息
result.setData(list);
result.setMsg(OvlsConstant.QUERY_SUCCESS_MSG);
//返回结果
return result;
}
}
10.添加控制器CourseController
package cn.xdl.ovls.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.xdl.ovls.service.CourseService;
import cn.xdl.ovls.util.OvlsResult;
@RestController
public class CourseController {
@Autowired
private CourseService coursService;
//发出什么请求
@GetMapping("/course/free")
public OvlsResult loadFree(
//设置如果默认没有就调用5条信息
@RequestParam(value="size",required=false,defaultValue="5")int size){
OvlsResult result = coursService.loadFreeCourse(size);
return result;
}
}
package cn.xdl.ovls.controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.xdl.ovls.util.OvlsResult;
@ControllerAdvice
public class MyErrorController {
/**
* @ExceptionHandler处理Controller抛出的异常
* @ResponseBody用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区
* handlerException处理器异常解析器
* setStatus设置状态
* setMsg设置状态信息
*/
@ExceptionHandler
@ResponseBody
public Object handlerException(Exception ex){
OvlsResult result = new OvlsResult();
result.setStatus(-1);
result.setMsg("系统异常");
return result;
}
}