如何简单地实现一些接口SpringBoot 实现一些接口
(非正式,青春学习版)
你需要做哪些工作?
1、下载IDEA
2、配置好java的环境
3、配置到meavn环境,并把meavn的数据库改成国内镜像的数据库,如:阿里云等
4、新建一个project
4.1、Spring Initializr数据库
5、配置application.properties
目的是为了配置与数据库的连接和与mybatis的包配置等
代码:
#端口、路径、数据库端口、数据库名、数据库角色名、密码需要手动更改
server.port=8080
server.servlet.context-path=/
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF8&serverTimezone=PRC
spring.datasource.username=root
spring.datasource.password=admin
#根据自身情况需要改一下路径
#放sql语句的包和xml文件
mybatis.mapper-locations= classpath:mapping/*Mapper.xml
#放实体类的包
mybatis.type-aliases-package=com.example.test4_23_2.entity
6、向pom.xml中添加包
- 在target包中点开pom.xml,在标签内加入要引入的包
- mybait包
- swagger包
- 分页pagehelper包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
7、建一些必要的包
common为了建立一个公共类
config用户配置一些组件
controller用于实现接口
service用于被controller中调用实现服务
service中的serviceImpl继承service,用于实现service中的方法
service需要调用dao
dao需要调用mapping中的*mapper.xml
具体如图:
以其中一个为例代码如下:
- common:Result:
package com.example.test4_23_2.common;
public class Result {
private int code;
private String msg;
private Object data;
public Result() {
}
public Result(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Result(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
- config:corsConfig:
package com.example.test4_23_2.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
final String ORIGINS[]={"GET","POST","PUT","DELETE"};
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowCredentials(false).allowedMethods(ORIGINS).maxAge(3600).allowedHeaders("*");
}
}
swaggerConfig:
package com.example.test4_23_2.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket petApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.test4_23_2.controller")) //指定提供接口所在的基包
.paths(PathSelectors.any())
.build();
}
/**
* 该套 API 说明,包含作者、简介、版本、host、服务URL
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("demo api 说明")
.version("0.1")
.termsOfServiceUrl("localhost:8080/demo1/")
.description("demo api")
.build();
}
}
之上是类似于全局的,以下是为了实现某个具体功能的:
自下而上
mapping:
RoleMapper.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.example.test4_23_2.dao.RoleDao">
<resultMap id ="RoleResultMap" type="com.example.test4_23_2.entity.Role">
<id property="id" jdbcType="INTEGER" column="role_id"></id>
<result property="name" column="role_name" jdbcType="VARCHAR"></result>
<result property="create_time" column="role_create_time" jdbcType="TIMESTAMP"></result>
<result property="status" column="role_status" jdbcType="VARCHAR"></result>
<result property="father_role" column="role_father_role" jdbcType="INTEGER"></result>
<result property="describe" column="role_describe" jdbcType="VARCHAR"></result>
<!-- 一对多-->
<collection property="privs" ofType="com.example.test4_23_2.entity.Priv">
<id property="id" jdbcType="INTEGER" column="priv_id"></id>
<result property="priv_name" column="priv_name" jdbcType="VARCHAR"></result>
<result property="status" column="priv_status" jdbcType="VARCHAR"></result>
<result property="priv_father_id" column="priv_father_id" jdbcType="INTEGER"></result>
<result property="priv_describe" column="priv_describe" jdbcType="VARCHAR"></result>
<result property="priv_href" column="priv_href" jdbcType="VARCHAR"></result>
<result property="priv_sort" column="priv_sort" jdbcType="INTEGER"></result>
<result property="priv_sign" column="priv_sign" jdbcType="INTEGER"></result>
</collection>
</resultMap>
<!-- 根据角色查询权限-->
<select id="selectPrivByRoleName" parameterType="String" resultMap="RoleResultMap">
SELECT
r.*,p.priv_name,p.priv_href,p.priv_sign,p.priv_describe,p.priv_sort,p.`status` as priv_status
FROM role r
LEFT JOIN priv_role pr on r.id=pr.priv_id
LEFT JOIN priv p on pr.priv_id=p.id
WHERE r.`name`=#{rolename}
</select>
<select id="selectAllRole" resultMap="RoleResultMap">
SELECT
NAME AS role_name,
role.create_time AS role_create_time,
role.`describe` AS role_describe,
STATUS AS role_status
FROM
role
</select>
</mapper>
dao包:RoleDao:
package com.example.test4_23_2.dao;
import com.example.test4_23_2.entity.Role;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface RoleDao {
public Role selectPrivByRoleName(String rolename);
List<Role> selectAllRole();
}
service包
RoleService:
package com.example.test4_23_2.service;
import com.example.test4_23_2.entity.Role;
import java.util.List;
public interface RoleService {
public Role selectPrivByRoleName(String rolename);
List<Role> selectAllRole();
}
service.impl:
roleserviceimpl:
package com.example.test4_23_2.service.impl;
import com.example.test4_23_2.dao.RoleDao;
import com.example.test4_23_2.entity.Role;
import com.example.test4_23_2.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoleServiceimpl implements RoleService {
@Autowired
RoleDao roleDao;
@Override
public Role selectPrivByRoleName(String rolename) {
return roleDao.selectPrivByRoleName(rolename);
}
@Override
public List<Role> selectAllRole() {
return roleDao.selectAllRole();
}
}
controller:
RoleController:
package com.example.test4_23_2.controller;
import com.example.test4_23_2.common.Result;
import com.example.test4_23_2.entity.Role;
import com.example.test4_23_2.entity.User;
import com.example.test4_23_2.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Api(value = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
@Autowired
RoleService roleService;
@ApiOperation(value = "通过角色名查找该角色名下的权限")
@RequestMapping(value = "/findPrivByRoleName",method = RequestMethod.GET)
public Result findPrivByRoleName(String rolename){
Result result;
try {
Role role=roleService.selectPrivByRoleName(rolename);
result=new Result(200,"成功",role);
}catch (Exception ex){
ex.printStackTrace();
result=new Result(500,"错误");
}
return result;
}
@ApiOperation(value = "查找所有角色",httpMethod = "GET")
@RequestMapping(value = "/selectAllRole",method = RequestMethod.GET)
public Result selectAllRole(){
Result result;
try {
List<Role> list= roleService.selectAllRole();
result=new Result(200,"成功",list);
}catch (Exception e){
e.printStackTrace();
result=new Result(500,"失败");
}
return result;
}
}
嗯,就没了吧大概
总之我可以运行。