简单的shiro案例(springboot+mybatis)

1,导入maven



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    war
    demo
    Demo project for Spring Boot

    
        1.8
    

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

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.apache.shiro
            shiro-core
            1.4.1
        
        
        
            org.apache.shiro
            shiro-spring
            1.4.1
        
        
        
            com.alibaba
            druid
            1.1.17
        
        
        
            org.apache.commons
            commons-lang3
            3.9
        
        
        
            org.springframework
            spring-context-support
            5.1.7.RELEASE
        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            9.0.21
        
        
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
        
        
            javax.servlet
            jstl
            1.2
        
        
        
            com.google.guava
            guava
            27.1-jre
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.配置druid文件

package com.example.demo.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.google.common.collect.Lists;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**druid的配置类
 * @author sy
 * @date 2019/3/14 17:35
 */

@Configuration
public class DruidConfig {

    //会扫描application.properties文件的以spring.druid开头的数据注入
    @ConfigurationProperties(prefix = "spring.druid")
    @Bean(initMethod = "init",destroyMethod = "close") //跟着spring一块启动,一块关闭
    public DruidDataSource dataSource(){

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setProxyFilters(Lists.newArrayList(statFilter()));//Lists是guava包
        return dataSource;
    }

    //alibaba的监听器,打印慢sql
    @Bean
    public Filter statFilter(){

        StatFilter filter = new StatFilter();
        filter.setSlowSqlMillis(1);
        filter.setLogSlowSql(true);
        filter.setMergeSql(true);
        return filter;

    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        return new ServletRegistrationBean(new StatViewServlet(),"/druid/*");

    }


}

3,配置application.xml

#druid 阿里巴巴下的一个开源连接池:功能强大,可以监控,稳定。
spring.druid.driverClassName=com.mysql.cj.jdbc.Driver
spring.druid.url=jdbc:mysql://localhost:3306/shiro?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.druid.username=root
spring.druid.password=root
#最大连接数
spring.druid.maxActive=30
#最小连接数
spring.druid.minIdle=5
#获取连接的最大等待时间
spring.druid.maxWait=10000
#解决mysql8小时的问题
spring.druid.validationQuery=SELECT 'x'

#Mybatis配置
mybatis.mapper-locations=mapper/*.xml
mybatis.type-aliases-package=com.example.demo.model

4,创建简单的对象:permission,role ,user 

package com.example.demo.model;

import java.io.Serializable;

public class Permission implements Serializable {

    private Integer pid;

    private String name;

    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;
    }
}
package com.example.demo.model;

import org.eclipse.jdt.internal.compiler.util.HashSetOfInt;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Role implements Serializable {

    private Integer rid;

    private String name;

    private Set permissionSet = new HashSet();


    public Integer getRid() {
        return rid;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set getPermissionSet() {
        return permissionSet;
    }

    public void setPermissionSet(Set permissionSet) {
        this.permissionSet = permissionSet;
    }
}
package com.example.demo.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class User implements Serializable {

    private Integer uid;

    private String username;

    private String password;

    private Set roleSet = 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 getRoleSet() {
        return roleSet;
    }

    public void setRoleSet(Set roleSet) {
        this.roleSet = roleSet;
    }
}

5,创建controller,service,dao

//controller
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserRole")
    @ResponseBody
    public User getUserRole(@RequestParam("username") String username){
        return userService.getUserByUsername(username);
    }

}


//service

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    /**
     * 通过username获取user
     * @return
     */
    @Override
    public User getUserByUsername(String username) {
       return userMapper.getUserByUsername(username);
    }
}

//mapper
@Repository
public interface UserMapper {

    User getUserByUsername(@Param("username") String username);

}

6,写xml文件,,,






    
    
        
        
        
        
            
            
            
                
                
            
        
    

    



7,建表,数据库表:shiro

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for permission
-- ----------------------------
DROP TABLE IF EXISTS `permission`;
CREATE TABLE `permission` (
  `pid` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of permission
-- ----------------------------
INSERT INTO `permission` VALUES ('1', 'update');
INSERT INTO `permission` VALUES ('2', 'delete');
INSERT INTO `permission` VALUES ('3', 'create');
INSERT INTO `permission` VALUES ('4', 'read');

-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
  `rid` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`rid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES ('1', 'admin');
INSERT INTO `role` VALUES ('2', 'user');

-- ----------------------------
-- Table structure for role_permission
-- ----------------------------
DROP TABLE IF EXISTS `role_permission`;
CREATE TABLE `role_permission` (
  `pid` int(11) NOT NULL,
  `rid` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of role_permission
-- ----------------------------
INSERT INTO `role_permission` VALUES ('1', '1');
INSERT INTO `role_permission` VALUES ('2', '1');
INSERT INTO `role_permission` VALUES ('3', '1');
INSERT INTO `role_permission` VALUES ('4', '1');
INSERT INTO `role_permission` VALUES ('4', '2');

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) CHARACTER SET utf8 NOT NULL,
  `password` varchar(255) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '张三', '123456');
INSERT INTO `user` VALUES ('2', '王五', '123546');

-- ----------------------------
-- Table structure for user_role
-- ----------------------------
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
  `uid` int(11) NOT NULL,
  `rid` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of user_role
-- ----------------------------
INSERT INTO `user_role` VALUES ('1', '1');
INSERT INTO `user_role` VALUES ('2', '2');

8,在springboot启动类添加MapperScan扫描

//扫描Mapper文件,注入service的成员变量中。
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

9,启动tomcat,然后访问:  http://localhost:8080/user/getUserRole?username=张三

 

 

此文章如有雷同,纯属意外

你可能感兴趣的:(springboot,shiro)