网站建设资讯

NEWS

网站建设资讯

Shiro-SpringBoot(一)

1.配置加载库: pom.xml
2.创建模型
3.创建数据库查询:
接口
服务
sql语句mapper
sql调用接口
4.配置: application.properties
5.入口文件配置扫描位置
6.测试查询用户信息
1.配置加载库: pom.xml

            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.apache.shiro
            shiro-core
            1.4.1
        
        
            org.apache.shiro
            shiro-spring
            1.4.0
        

        
        
            MySQL
            mysql-connector-java
            runtime
        
        
            com.alibaba
            druid
            1.1.6
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.0
        

        
        
            org.apache.commons
            commons-lang3
            3.4
        
        
            org.springframework
            spring-context-support
            4.2.3.RELEASE
        

        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet
            jstl
        

        
            org.apache.ibatis
            ibatis-core
            3.0
        
2.创建模型
2-1.User

import java.util.HashSet;
import java.util.Set;

public class User {
    private Integer uid;
    private String username;
    private String password;
    private Set roles=new HashSet<>();
    private Set permissions=new HashSet<>();

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set getRoles() {
        return roles;
    }

    public void setRoles(Set roles) {
        this.roles = roles;
    }

    public Set getPermissions() {
        return permissions;
    }

    public void setPermissions(Set permissions) {
        this.permissions = permissions;
    }
}
2-2.Role

import java.util.HashSet;
import java.util.Set;

public class Role {
    private Integer rid;
    private String rname;
    private Set permissions=new HashSet<>();

    public Integer getRid() {
        return rid;
    }

    public void setRid(Integer rid) {
        this.rid = rid;
    }

    public String getRname() {
        return rname;
    }

    public void setRname(String rname) {
        this.rname = rname;
    }

    public Set getPermissions() {
        return permissions;
    }

    public void setPermissions(Set permissions) {
        this.permissions = permissions;
    }
}
2-3.Permission

public class Permission {
    private Integer pid;
    private String name;
    private String url;

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    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;
    }
}
3.创建数据库查询
3-1.UserMapper
public interface UserMapper {
    User findByUsername(@Param("username") String username);
}
3-2.UserService
public interface UserService {
    User findByUsername(String username);
}
3-2.UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }
}
3-3.UserMapper.xml





    
    
        
        
        
        
        
            
            
            
            
                
                
                
            
        
    

    
    

6.测试查询用户信息
HomeController
@RestController
public class HomeController {

    @Autowired
    private UserService userService;

    // 登录页面
    @RequestMapping("/test")
    public String login(){
        User user=userService.findByUsername("admin");
        if (user==null){
            return "用户找不到";
        }else{
            return "用户名是: "+user.getUsername();
        }
    }
}

分享文章:Shiro-SpringBoot(一)
新闻来源:http://cdweb.net/article/jpjjhd.html