springboot個人博客系統---搭建項目環境(一)

一、我使用的是idea編輯器創建springboot項目:
使用的idea編輯器創建springboot項目

二、選擇你要用到的組件打鉤

三、創建完成後的項目目錄結構:
這裏寫圖片描述
定製Banner(網上看到的小玩意~):
我們在啓動Spring Boot項目的時候,在控制檯會默認輸出一個啓動圖案,如下:
這裏寫圖片描述
1.在src/main/resources下新建一個banner.txt文檔
2.通過http://patorjk.com/software/taag網站生成需要的字符,將字符拷貝到步驟1所創建的txt文檔中

四、對配置文件進行配置:
這只是我這暫時的配置,可以根據實際需要自行修改;
(如果你導入了spring-session組件那麼配置文件就得加入spring.session.store-type=none,如下)

#設置需要加載的額外配置文件(application-jdbc.properties)
spring.profiles.active=jdbc

# server
server.context-path=/xxblog
server.port=8082

# thymeleaf start
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
#開發時關閉緩存,不然沒法看到實時頁面
spring.thymeleaf.cache=false
# thymeleaf end

# mybatis
mybatis.mapper-locations=classpath:/mapper/*.xml
mybatis.type-aliases-package=com.myblog.dao

# session
spring.session.store-type=none

#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql

五、配置啓動類:

@MapperScan("com.myblog.dao")
@SpringBootApplication
@EnableTransactionManagement// 開啓註解事務管理,等同於xml配置文件中的 <tx:annotation-driven />
/**
 * @author xxw
 * @date 2017.11.22
 * 項目啓動入口類
 */
public class XxblogApplication extends SpringBootServletInitializer {
    //需要啓動類繼承自SpringBootServletInitializer並覆蓋configure方法方可正常部署至常規tomcat下
    //其主要能夠起到web.xml的作用
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }

    /**
     * 根據配置文件創建數據源
     */
    @Bean(initMethod = "init", destroyMethod = "close")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    /**
     * 創建事物管理器
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }


    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        //加載mybatis配置文件與數據源注入到sqlSessionFactory,
        //拿到sqlSessionFactory並注入到spring容器中
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mapper/*Mapper.xml"));
        return sqlSessionFactoryBean.getObject();
    }

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

注意!!!
如果啓動類沒有配置@MapperScan(“xxx.mapper”)註解,則會報如下錯誤(提示你找不到Mapper接口):

Description:

A component required a bean of type 'xxx.xxx.XxxMapper' that could not be found.


Action:

Consider defining a bean of type 'xxx.xxx.XxxMapper' in your configuration.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章