工具IDEA 配置springboot+maven項目java
首先安裝IDEA,至於怎麼安裝就不介紹了。。mysql
首先安裝maven,先在網上下載一個maven包。在IDEA的settings中Maven設置web
點擊USer settings file 文件夾正常的是空白 如圖找到你下載的maven文件夾,引入settings.xml 好了maven配置完成spring
在IDEA找到File--》New--》project點擊進入到下面的頁面sql
選中maven 點擊Next數據庫
填入相關信息創建工程完畢跨域
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- MYSQL --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Spring Boot JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!--maven打包時應用--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
這是個人文件夾結構 注意application必定在controller代碼上一結構springboot
這是個人啓動項applicationapp
@SpringBootApplication public class application { public static void main(String[] args) { SpringApplication.run(application.class, args); } }
由於我導入了數據庫和c3p0的包必須鏈接數據庫不然報錯,不想導入下面的也不用配置cors
鏈接池配置代碼 注意端口號不要重複個人是7878 數據庫信息必定要配置成功 個人數據庫名是ceshi就很少說了
server.port=7878 c3p0.driverClass=com.mysql.jdbc.Driver c3p0.minPoolSize=2 c3p0.maxPoolSize=10 c3p0.maxIdleTime=1800000 c3p0.acquireIncrement=3 c3p0.maxStatements=1000 c3p0.initialPoolSize=3 c3p0.idleConnectionTestPeriod=60 c3p0.acquireRetryAttempts=30 c3p0.acquireRetryDelay=1000 c3p0.breakAfterAcquireFailure=false c3p0.testConnectionOnCheckout=false c3p0.jdbcUrl=jdbc:mysql://localhost/ceshi?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false c3p0.user=root c3p0.password=123456
config裏的基本配置
@Configuration public class TsetConfig { @Bean public JdbcTemplate getJdbcTemplate() { JdbcTemplate jdbc = new JdbcTemplate(); jdbc.setDataSource(dataSource()); return jdbc; } @Bean(name = "dataSource") @Qualifier(value = "dataSource") @Primary @ConfigurationProperties(prefix = "c3p0") public DataSource dataSource() { return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build(); } /** * 跨域 * @return */ @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } }; }
這是個人項目目錄
個人controller寫了一個測試類
@RestController public class TestController { @GetMapping(value = "/test") public String Test(String tt){ return tt; } }
找到啓動類application 郵件啓動
出現這個7878代表啓動成功
用postmain 測試下
成功!!!!!!!!!!!!!!!!!!!!!!