(一) springboot 項目搭建 --《springboot與shiro整合》

廢話很少說, 直接上代碼java

 

1.1 咱們先配置spring boot mysql

1.添加pom依賴web

   <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 配置spring

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

 

3.程序啓動類sql

@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 apache

 

 

好了,如今咱們springboot 已經搭建好了,下面咱們咱們來作一些前期工做,與數據庫結合完成一個登陸的功能springboot

這裏咱們使用mybatismybatis

1. 前期準備工做app

數據庫表結構

用戶表(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

 

代碼在這裏

相關文章
相關標籤/搜索