以往寫web都是以大量的xml文件開始,此次課堂上老師要求用springboot完成項目。剛開始接觸時,對於已經習慣使用xml配置的人確實很不習慣,可是研究久了,發現springboot在配置方面確實很是方便,項目搭建至關迅速。可是有的網絡上找資料很不方便,所以,這裏對本身在使用中查到的一些用法作以總結。css
新建maven(gradle)項目文件,在pom文件中添加springboot的項目引用。spring.io上提供了一個例子:http://spring.io/guides/gs/spring-boot/。html
pom文件以下:java
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId> <artifactId>gs-spring-boot</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
snapshot版本和release版本的依賴不一樣,簡單點的配置是release版本。mysql
建立HelloWorldController,用以路徑分發git
package hello; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } }
而後新建Application主類,運行main函數,即可以啓動springboot默認服務器完成配置github
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
@SpringBootApplication 標籤表明了 @Configuration, @EnableAutoConfiguration, @ComponentScan 這3個標籤。web
運行之後,訪問localhost:8080就能獲得index函數返回的數據。spring
application.yml(.properties)中可配置端口和根路徑 sql
server: context-path: / port: 8080
springboot默認的靜態資源路徑是 :
/META-INF/resources/
/resources/
/static/
/public/ 數據庫
固然也能夠本身配置,以下:
@Configuration @ComponentScan public class MyConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // registry.addResourceHandler("/js/**") // .addResourceLocations("classpath:/js/"); // registry.addResourceHandler("/css/**") // .addResourceLocations("classpath:/css/"); // registry.addResourceHandler("/WEB-INF/resources/image/**") // .addResourceLocations("classpath:/image/"); // registry.addResourceHandler("/static/**") // .addResourceLocations("classpath:/html/"); // registry.addResourceHandler("/**").addResourceLocations("/"); } }
addResourceHandler("/js/**")攔截/js/**的訪問
addResourceLocations("classpath:/js/");定義到classpath:/js/路徑下
使用druid鏈接池,pom中添加:
<!-- mysql 驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <!-- 數據庫鏈接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.5</version> </dependency>
而後在application.yml(.properties)中配置便可
spring: datasource: name: sd url: jdbc:mysql://***.***.***.***/3306/sd?useUnicode=true&characterEncoding=utf8 username: * password: * # 使用druid數據源 # 在此處配置便可 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20
一樣,先添加pom
<!-- page helper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>RELEASE</version> </dependency> <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>1.4.9</version> </dependency>
建立mybatis-plus的mapper生成函數
package Generator; import com.baomidou.mybatisplus.annotations.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.ConfigGenerator; public class EntityGenerator { public static void main(String[] args) { ConfigGenerator cg = new ConfigGenerator(); // 配置 MySQL 鏈接 cg.setDbDriverName("com.mysql.jdbc.Driver"); cg.setDbUser("*"); cg.setDbPassword("*"); cg.setDbUrl("jdbc:mysql://***.***.***.***:3306/sd?characterEncoding=utf8"); // 配置包名 cg.setEntityPackage("com.attendance.model"); cg.setMapperPackage("com.attendance.mapper"); cg.setServicePackage("com.attendance.service"); cg.setXmlPackage("com.attendance.mapper.xml"); cg.setServiceImplPackage("com.attendance.service.impl"); // 配置表主鍵策略 cg.setIdType(IdType.AUTO); // 配置保存路徑 cg.setSaveDir("E:\\Users\\OPTIPLEX\\Documents\\Codes\\Attendency\\src\\main\\java\\"); // 其餘參數請根據上面的參數說明自行配置,當全部配置完善後,運行AutoGenerator.run()方法生成Code // 生成代碼 AutoGenerator.run(cg); } }
函數執行之後能夠生成相應的service/mapper/bean等等類
經過配置MybatisPlusConfig就可使用通用單表mapper、service函數,或者不通過配置自定義sql方法。具體內容見mybatis-plus官網。pagehelper同理。
application.yml文件中添加
logging: config: classpath:logback.xml path: /Users/attendancy/logs level: debug
resources下建立logback.xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- 默認配置 --> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="org.springframework.web" level="DEBUG"/> <logger name="com.ibatis" level="DEBUG" /> <!--<logger name="com.ibatis.common.jdbc.SimpleDataSource" level="DEBUG" />--> <!--<logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" />--> <!--<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" />--> <!--<logger name="java.sql.Connection" level="DEBUG" />--> <!--<logger name="java.sql.Statement" level="DEBUG" />--> <!--<logger name="java.sql.PreparedStatement" level="DEBUG" />--> </configuration>
其餘更多配置使用的使用再作記錄。。。。。。。。