springBoot整合ssm


 

SpringBoot SSM整合

一、Spring Boot整合Web開發

1.默認靜態資源配置

WebMvcAutoConfiguration該類下找到屬性:ResourceProperties進入可查看到:Spring Boot 默認將 /** 所有訪問映射到以下目錄:

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources

如:在src/main/resources目錄下新建 public、resources、static 三個目錄,並分別放入 a.jpg b.jpg c.jpg 圖片

均能正常訪問相應的圖片資源。那么說明,Spring Boot 默認會挨個從 public resources static 里面找是否存在相應的資源,如果有則直接返回。

 

2.在application配置

在application.properties中添加配置:

注意:通過spring.mvc.static-path-pattern這種方式配置,會使Spring Boot的默認配置失效,也就是說,/public /resources 等默認配置不能使用。配置中配置了靜態模式為/static/,就只能通過/static/來訪問。


spring:
mvc:
  static-path-pattern: /static/**

3. 配置主頁面

WebMvcAutoConfiguration:默認:index.html

在靜態資源下 public、resources、static可直接訪問


@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
   return new WelcomePageHandlerMapping(new       TemplateAvailabilityProviders(applicationContext),
                    applicationContext, getWelcomePage(),                     this.mvcProperties.getStaticPathPattern());
}

二、SpringBoot整合MyBatis

1、新建SpringBoot工程

2、POM.XML SSM

    <dependencies>
       <!-- spring web mvc-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <!-- spring mybatis -->
       <dependency>
           <groupId>org.mybatis.spring.boot</groupId>
           <artifactId>mybatis-spring-boot-starter</artifactId>
           <version>2.1.0</version>
       </dependency>
       <!-- mysql 驅動-->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <scope>runtime</scope>
       </dependency>

       <!-- 熱部署 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
           <optional>true</optional>
       </dependency>

       <!-- 單元測試 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
   </dependencies>

 


<!-- druid 數據源連接池 -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid-spring-boot-starter</artifactId>
           <version>1.1.10</version>
       </dependency>

3、bean代碼


@Data
public class User {
   private Integer id;
   private String name;
   private String telphone;
   private Integer status;  
}

4、mapper代碼


@Mapper  //MapperScannerConfigurer 自動掃描 將Mapper接口生成代理注入到Spring DAO接口所在包名,Spring會自動查找其下的類
public interface UserMapper {
   List<User> selectAll();
  ........
}

 

5、application.yml配置

server:
port: 8080
context-path: /boot
spring:
#數據源連接配置
datasource:
  name: test
  type: com.alibaba.druid.pool.DruidDataSource
  druid: #druid相關配置
    url: jdbc:mysql://localhost:3306/school?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    #監控統計攔截的filters
    filters: stat
    #配置初始化大小/最小/最大
    initial-size: 1
    min-idle: 1
    max-active: 20
    #獲取連接等待超時時間
    max-wait: 60000
    #間隔多久進行一次檢測,檢測需要關閉的空閑連接
    time-between-eviction-runs-millis: 60000
    #一個連接在池中最小生存的時間
    min-evictable-idle-time-millis: 300000
    validation-query: SELECT 'x'
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    #打開PSCache,並指定每個連接上PSCache的大小。oracle設為true,mysql設為false。分庫分表較多推薦設置為false
    pool-prepared-statements: false
    max-pool-prepared-statement-per-connection-size: 20


#整合myBatis
mybatis:
mapper-locations: classpath:mapper/*.xml   # mapper映射對應的配置文件位置.xml
type-aliases-package: com.qfjy.bean       # 對應的實體類的包名

 

6、啟動類 Application

注意:@MapperScan(value="com.qfjy.mapper") 或接口上@Mapper各選一個


@SpringBootApplication
@MapperScan(value = "com.qfjy.mapper")
public class Boot2SsmApplication {
   public static void main(String[] args) {
       SpringApplication.run(Boot2SsmApplication.class, args);
  }
}

 

7、異常注意事項


Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2019-07-24 19:24:52.225 ERROR 8704 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource   : init datasource error, url: jdbc:mysql://localhost:3306/maven_ssm

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if
.................

使用Idea做jdbc的時候用了最新的mysql-connector-java-8.0.11庫發現編碼沒有異常但是運行時出現了兩個異常,如下

意思是 mysql.jdbc.driver被棄用了新的驅動類是“com.mysql.cjdbc.driver”。驅動程序通過SPI自動注冊,而手動加載類通常是不必要的,解決方案如下:


把com.mysql.jdbc.Driver 改為com.mysql.cj.jdbc.Driver 即可

第二個異常是時區的錯誤,因此只你需要設置為你當前系統時區即可


?serverTimezone=GMT%2B8

完整如下:


spring:
#數據源連接配置
datasource:
  name: test
  type: com.alibaba.druid.pool.DruidDataSource
  druid: #druid相關配置
    url: jdbc:mysql://localhost:3306/maven_ssm?serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM