中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Shiro-SpringBoot(一)

發布時間:2020-06-25 12:12:28 來源:網絡 閱讀:387 作者:lxhllf2005 欄目:編程語言
1.配置加載庫: pom.xml
2.創建模型
3.創建數據庫查詢:
接口<interface>
服務<service>
sql語句mapper<mapper.xml>
sql調用接口<mapper.interface>
4.配置: application.properties
5.入口文件配置掃描位置
6.測試查詢用戶信息
1.配置加載庫: pom.xml
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>

        <!--數據庫相關-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!--工具庫-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>

        <!--jsp相關配置-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-core</artifactId>
            <version>3.0</version>
        </dependency>
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<Role> roles=new HashSet<>();
    private Set<Permission> 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<Role> getRoles() {
        return roles;
    }

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

    public Set<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Permission> 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<Permission> 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<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Permission> 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
<?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="cn.safi16.springbootshiro.mapper.UserMapper">

    <!-- select 對應的方法結果 -->
    <resultMap id="userMap" type="cn.safi16.springbootshiro.model.User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <!--復合結果-->
        <collection property="roles" oftype="cn.safi16.springbootshiro.model.Role">
            <id property="rid" column="rid"/>
            <result property="rname" column="rname"/>
            <!--復合結果-->
            <collection property="permissions" oftype="cn.safi16.springbootshiro.model.Permission">
                <id property="pid" column="pid"/>
                <result property="name" column="name"/>
                <result property="url" column="url"/>
            </collection>
        </collection>
    </resultMap>

    <!--方法執行語句-->
    <select id="findByUsername" property="string" resultMap="userMap">
         SELECT u.*, r.*, p.*
      FROM user u
        INNER JOIN user_role ur on ur.uid = u.uid
        INNER JOIN role r on r.rid = ur.rid
        INNER JOIN permission_role pr on pr.rid = r.rid
        INNER JOIN permission p on pr.pid = p.pid
      WHERE u.username = #{username}
    </select>

</mapper>
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();
        }
    }
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阿勒泰市| 南岸区| 丰镇市| 陕西省| 贵南县| 贵州省| 会昌县| 泾源县| 庐江县| 达日县| 虹口区| 双牌县| 临漳县| 佛坪县| 北安市| 久治县| 教育| 文水县| 铁岭市| 聊城市| 庆云县| 邵武市| 兰考县| 太湖县| 青阳县| 静海县| 无极县| 贞丰县| 鄱阳县| 时尚| 蒙山县| 三明市| 洛南县| 庆云县| 娱乐| 体育| 班戈县| 石景山区| 临泉县| 宜宾市| 长治市|