SpringBoot:连接MySQL

SpringBoot可以快速搭建我们所需要的框架,只需要轻微的配置即可。

此次所需要的依赖:

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

	</dependencies>

不难看出,这是一些集成的依赖组。

搭建SSM框架只需要 这些,如果是普通搭建,那么依赖是一大堆的,Spring、SpringMVC、Mybatis等,以及整合、辅助等等。

而SpringBOOT就是这么方便

下面是连接MySQL的必需配置:

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/cn?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

连接数据库的路径等等

spring.datasource.password=root:千万是这种的,否则报错

utf-8,解决可能出现的乱码

serverTimezone:是解决时差的

如果有使用到xml形式的Mapper

那么application.properties:

mybatis.type-aliases-package=com.ssm.springboot02.model
mybatis.mapper-locations=classpath:com/ssm/springboot02/model/*.xml

pom.xml:

<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>*.yml</include>
					<include>*.properties</include>
					<include>*.xml</include>
					<include>**/*.ftl</include>
				</includes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

controller、mapper、model、service、serviceImpl、dao

这些都是和SSM相同的

启动类

package com.hc.tempboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.hc.tempboot.mapper")
@SpringBootApplication
public class TempbootApplication {

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

SpringBoot已经内置了tomcat,直接启动即可。

@MapperScan:注明mapper包

application.yml:

server:
  port: 8090
  servlet:
    context-path: /ssm

spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/t203?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  type-aliases-package: com.ssm.springboot02.model
  mapper-locations: classpath:com/ssm/springboot02/model/*.xml

猜你喜欢

转载自blog.csdn.net/qq_43532342/article/details/84312458
今日推荐