Springboot整合SpringMVC+Mybatis
需求分析:通過使用Springboot+SpringMVC+Mybatis 整合實作一個對資料庫表users表的CRUD操作,
1.創建專案
①.修改pom檔案
springmvc的jar包因為已經包含在了web啟動器中了,所以就不需要單獨匯入了,但是mybatis需要單獨匯入springboot對mybatis支持的啟動器
- 添加mybatis啟動器
- 添加mysql資料庫驅動
- 添加druid資料庫連接池
- 添加Thymeleaf的坐標
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<groupId>com.dxh</groupId>
<artifactId>12-spring-boot-springmvc-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Spring Boot 的啟動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf的坐標 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Mybatis啟動器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySql資料庫驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid資料庫連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</project>
②.修改(修改)application.properties全域組態檔
- 配置連接資料庫的資訊
- 配置資料源
- 起別名
#mysql 訪問資料庫的基本資訊
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#資料源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#起別名
mybatis.type-aliases-package=com.dxh.pojo
③.資料庫表設計
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/65096.html
標籤:Java
