(一) springboot 项目搭建 --《springboot与shiro整合》

废话不多说, 直接上代码

 

1.1 我们先配置spring boot 

1.添加pom依赖

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath />
    </parent>
  
   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- auto redeploy -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
   </dependencies>
   
   <build>
        <plugins>
            <!-- compiler插件参数设置,指定编码 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

2.application.properties 配置

#配置项目端口
server.port=8080

 

3.程序启动类

@SpringBootApplication
public class Application {
    
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.run(args);
    }
}

 

4.创建Controller

@RestController
@RequestMapping("/user")
public class TestController {
    
    @RequestMapping("/login")
    public String users(){
        return "hello world";
    }
}

 

现在访问 http://localhost:8080/user/login 

 

 

好了,现在我们springboot 已经搭建好了,下面我们我们来做一些前期工作,与数据库结合完成一个登录的功能

这里我们使用mybatis

1. 前期准备工作

数据库表结构

用户表(sys_users) ,角色表(sys_roles) ,权限表(sys_permissions)  ,用户角色关系表(sys_users_roles) ,角色权限关系表(sys_roles_permissions)

引入mybatis 依赖 

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>
        
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
</dependency>


<!-- jdbc driver -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

  <!--Mybatis -->
  <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.3.1</version>
  </dependency>


  <dependency>
     <groupId>org.mybatis</groupId>
     <artifactId>mybatis-spring</artifactId>
     <version>1.2.4</version>
 </dependency>


  <dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.2</version>
    <scope>compile</scope>
    <optional>true</optional>
  </dependency>

 

配置数据源

@Component
@Configuration
@MapperScan(basePackages = "com.zhangls.blog.dao", sqlSessionTemplateRef  = "systemSqlSessionTemplate")
public class DataSourceConfig {
    
    @Bean(name = "systemDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.jdbc.properties") // application.properteis中对应属性的前缀
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "systemSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("systemDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/system/*.xml"));
        bean.setTypeAliasesPackage("com.zhangls.blog.entity");        
        return bean.getObject();
    }

    @Bean(name = "systemTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("systemDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "systemSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("systemSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

 

 DAO ,Service , Controller ,Mapper  等代码这里就不写了

 

@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/login")
    public String users(){
        
        StringBuilder msg = new StringBuilder();
        User user = userService.findByUsername("zhang");
        if(user != null){
            msg.append("登录成功");
        }else{
            msg.append("登录失败");
        }
        return msg.toString();
    }
}

 

请求登录接口

 

 前期的准备工作已经完毕,下面开始整合shiro

 

代码在这里

 

posted @ 2018-04-10 15:28  暖少年  阅读(1053)  评论(0编辑  收藏  举报