SpringBoot整合Shiro(完整版),⾯试题分享
springboot-shiro
0.0.1-SNAPSHOT
springboot-shiro
Demo project for Spring Boot
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.apache.shiro
shiro-spring
1.4.0
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
2、项⽬结构
3、配置链接数据库属性
spring.datasource.url=jdbc:mysql://localhost:3306/shiro?serverTimezone=GMT%2B8 spring.datasource.username=root
spring.datasource.password=root
spring.datasource.sql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.able_lazy_load_no_trans=true
server.port=80
t-path=/shiro
编写代码
1、编写实体类
菜单表实体类TbMenu,Spring-Data-Jpa可以根据实体类去数据库新建或更新对应的表结构,详情可以访问import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.CreatedBy;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
/**
菜单表
@author
*/
@Entity
public class TbMenu {
private Integer id;
private String name;
private String url;
private Integer idx;
@JsonIgnore
private TbMenu parent;
@JsonIgnore
private List children = new ArrayList<>();
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
springboot结构this.id = id;
}
@Column(unique = true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Integer getIdx() {
return idx;
}
public void setIdx(Integer idx) {
this.idx = idx;
}
@ManyToOne
@CreatedBy
public TbMenu getParent() {
return parent;
}
public void setParent(TbMenu parent) {
this.parent = parent;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = “parent”) @OrderBy(value = “idx”)
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
}
⾓⾊及权限表SysRole,parent 为null时为⾓⾊,不为null时为权限。import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.CreatedBy; import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
/***
⾓⾊及⾓⾊对应的菜单权限
@author
*parent 为null时为⾓⾊,不为null时为权限
*/
public class SysRole {
private Integer id;
private String name; //名称
@JsonIgnore
private SysRole parent;
private Integer idx; //排序
@JsonIgnore
private List children = new ArrayList<>();
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length = 20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论