欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自学SpringBoot--04整合持久层技术--整合SpringMVC+Mybatis

程序员文章站 2022-05-26 09:14:20
...

SpringBoot

Spring整合SpringMVC+Mybatis

需求分析:通过使用SpringBoot+SpringMVC+Mybatis整合实现一个对数据库中的users表的CRUD的操作

创建项目

自学SpringBoot--04整合持久层技术--整合SpringMVC+Mybatis
finish

修改pom文件

<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>1.5.10.RELEASE</version>
  </parent>
  <groupId>com.bjsxt</groupId>
  <artifactId>12-spring-boot-springmvc-mybatis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
  	<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
  	<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
  </properties>
  
  <dependencies>
    <!-- springboot的启动器 -->
	<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.1.1</version>
  	</dependency>
  	<!-- mysql数据库驱动 -->
	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  	</dependency>
  	<!-- druid数据库连接池 -->
	<dependency>
  		<groupId>com.alibaba</groupId>
  		<artifactId>druid</artifactId>
  		<version>1.0.9</version>
  	</dependency>
  </dependencies>
</project>

添加application.properties全局配置文件

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

mybatis.type-aliases-package=com.bjsxt.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;

自学SpringBoot--04整合持久层技术--整合SpringMVC+Mybatis