打造最简单的springboot项目构建方式

1.用springboot官方网站提供的工具创建springboot maven项目如下

Getting Started | Building an Application with Spring Boot

删除项目中无用的依赖,下边红框中的内容删掉。

 下载maven(如果有就跳过此步骤)

https://maven.apache.org/download.cgi

 配置maven国内镜像(这个一定要做,可以参考下边这篇文章)

(83条消息) 国内最快的Maven仓库(全)_国内mvn仓库_普通网友的博客-CSDN博客

同时配置好maven仓库所在位置即可。

接着配置maven的环境变量,我本机的变量配置如下:

 进入到springboot项目执行

mvn install

我们一般使用springboot全家桶+mybatis注解开发项目常用的依赖如下:

 <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
    </dependencies>

常用的配置文件如下:

# server
server.port = 8080
# datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:3306/db?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=300
spring.datasource.hikari.connection-timeout=10000
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.connection-test-query = select 1

# thymeleaf
spring.thymeleaf.cache=false

# redis
spring.redis.host=ip
spring.redis.prot=6379
spring.redis.password=password
spring.redis.database=0

此时我们的springboot最简单项目已经做好了,注意数据源不配置无法正常启动,另外大多数后台项目还需要用到认证和授权,我们需要配置一下springSecurity。列出个简单配置类,真正的实战项目要读者自己去完善。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    MyUserDetailsManager myUserDetailsManager;
    @Autowired
    MyFHandler myFHandler;
    @Autowired
    MySHandler mySHandler;
    @Autowired
    MyFHandler myHandler;
    /**
     * 登录配置
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/g/web/login")
                .loginProcessingUrl("/g/srv/doLogin")
                .successHandler(mySHandler)
                .failureHandler(myFHandler)
                .and().authorizeRequests()
                .mvcMatchers("/g/**").permitAll()
                .mvcMatchers("/**").authenticated()
                .and().csrf().disable().headers().frameOptions().sameOrigin();
    }
    /* 
     * 认证配置
    */
    @Autowired
    public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception{
        authenticationManagerBuilder.userDetailsService(myUserDetailsManager);
    }
    /*
     * 密码加密,createUser 和 authticate的时候用到
    */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值