2-SpringBoot-整合SSM

SpringBoot SSM整合

一、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

spring:
  #数据源连接配置
  datasource:
    name: test
    type: com.alibaba.druid.pool.DruidDataSource
    druid: #druid相关配置
      url: jdbc:mysql://localhost:3306/maven_ssm
      driver-class-name: com.mysql.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

四、SpringBoot SSM整合总结

完整 pom.xml

 <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>

        <!-- druid 数据源连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>



        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
     
     
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

application.yml

server:
  port: 8080

spring:
  #数据源连接配置
  datasource:
    name: test
    type: com.alibaba.druid.pool.DruidDataSource
    druid: #druid相关配置
      url: jdbc:mysql://localhost:3306/nj-1904?serverTimezone=GMT%2B8
      driver-class-name: com.mysql.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.entity.po        # 对应的实体类的包名

ql设为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.entity.po # 对应的实体类的包名


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值