Boot"依赖起步"
好的没错,boot当然是依赖起步。下面看下我们的pom.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xuxing</groupId>
<artifactId>xuxing_blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xuxing_blog</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
//mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</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>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
此时mybatis依赖是带有版本的,有的同学可能就会有疑问,boot不是管理好版本了吗,这个我也不是很了解,我简单地点进去boot的父依赖进去看了一下,发现其中并没有指定mybatis的版本,其他的倒是有,比如mysql数据库驱动的就有版本管理,这里我们暂时不管,没有就自己加上。
配置文件
配置application.yml文件
接下来是application.yml的配置,有的小伙伴用的可能是自带的application.properties,这个关系不大。没有application.yml文件的话就自己创建一个。名字不能改哦,必须是application开头,这个在boot的源码中是有规定的,具体的我记不得了,有兴趣的小伙伴可以自己去翻翻源码
server:
port: 8027
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: password
url: jdbc:mysql://123.56.25.98:3306/xuxing_blog
#thymeleaf配置
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
suffix: .html
encoding: UTF-8
servlet:
content-type: text/html
mode: HTML5
cache: false
#Mybatis相关配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.xuxing.xuxing_blog.entity
#驼峰命名
configuration:
map-underscore-to-camel-case: true
在看mybatis这部分配置之前,我们先需要配置好DataSource,配置如上,我这里没有阿里的德鲁伊,而是日本的一个连接池,英文不太想写,反正翻译过来就是光(也许想表示很快吧)的意思,2.2.1版本的boot中默认使用的就是这个连接池。
好了我们言归正传,看到mybatis的配置
mybatis:
mapper-locations: classpath:mapper/*.xml 表示mapper映射文件的位置
type-aliases-package: com.xuxing.xuxing_blog.entity 实体类别名
#驼峰命名
configuration:
map-underscore-to-camel-case: true
接下来我们跑一下程序,看看是否能跑通
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
引入单元测试依赖并且建立测试类
@SpringBootTest
@RunWith(SpringRunner.class)
class XuxingBlogApplicationTests {
@Autowired
ConnectTestMapper connectTestMapper;
@Autowired
DataSource dataSource;
@Test
void contextLoads() {
ConnectTest connectTest = connectTestMapper.selectByPrimaryKey(1);
System.out.println(connectTest);
System.out.println("===========DataSource==========");
System.out.println(dataSource);
}
}
结果如下
2019-11-29 13:42:45.914 INFO 6092 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-11-29 13:42:47.757 INFO 6092 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
ConnectTest{id=1, name='cxl', createTime=Fri Nov 29 11:01:29 CST 2019, updateTime=Fri Nov 29 11:01:29 CST 2019}
===========DataSource==========
HikariDataSource (HikariPool-1)
那个mapper接口就不用我教大家怎么写了吧,不定的自己百度一下,还有我们看到的datasource也确实是这个hikar 也证实了我前面所说的boot2.2.1版本默认连接池是这个日本的hikar
萌新发帖,不完善之处,请多指教