写点什么

一个项目了解 SpringBoot 集成 MyBatis(1),面试必备知识点

用户头像
极客good
关注
发布于: 刚刚

然后引入 mybatis-spring-boot-starter 的依赖,可以在这个网站查询最新的依赖:[mybatis-spring-boot-starter 依赖](


)


说明:由于 springboot 整合 mybatis 版本中默认依赖 mybatis,因此不需要额外引入 mybatis 版本,否则会出现冲突。


<dependency>


<groupId>org.mybatis.spring.boot</groupId>


<artifactId>mybatis-spring-boot-starter</artifactId>


<version>2.1.2</version>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>druid</artifactId>


<version>1.1.12</version>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


<version>5.1.38</version>


</dependency>


建立入口类 Application


@SpringBootApplication


public class Application {


public static void main(String[] args) {


SpringApplication.run(Application.class, args);


}


}


[](


)完整的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<modelVersion>4.0.0</modelVersion>


<groupId>com.yusael</groupId>


<artifactId>springboot 集成 mybatis</artifactId>


<version>1.0-SNAPSHOT</version>


<packaging>war</packaging>


<name>springboot 集成 mybatis Maven Webapp</name>


<url>http://www.example.com</url>


<properties>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


<maven.compiler.source>1.7</maven.compiler.source>


<maven.compiler.target>1.7</maven.compiler.target>


</properties>


<parent>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-parent</artifactId>


<version>2.2.5.RELEASE</version>


</parent>


<dependencies>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-web</artifactId>


</dependency>


<dependency>


<groupId>org.projectlombok</groupId>


<artifactId>lombok</artifactId>


<version>1.18.8</version>


<scope>provided</scope>


</dependency>


<dependency>


<groupId>org.mybatis.spring.boot</groupId>


<artifactId>mybatis-spring-boot-starter</artifactId>


<version>2.1.2</version>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>druid</artifactId>


<version>1.1.12</version>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


<version>5.1.38</version>


</dependency>


</dependencies>


<build>


<finalName>springboot 集成 mybatis</finalName>


<plugins>


<plugin>


<artifactId>maven-clean-plugin</artifactId>


<version>3.1.0</version>


</plugin>


<plugin>


<artifactId>maven-resources-plugin</artifactId>


<version>3.0.2</version>


</plugin>


<plugin>


<artifactId>maven-compiler-plugin</artifactId>


<version>3.8.0</version>


</plugin>


<plugin>


<artifactId>maven-surefire-plugin</artifactId>


<version>2.22.1</version>


</plugin>


<plugin>


<artifactId>maven-war-plugin</artifactId>


<version>3.2.2</version>


</plugin>


<plugin>


<artifactId>maven-install-plugin</artifactId>


<version>2.5.2</version>


</plugin>


<plugin>


<artifactId>maven-deploy-plugin</artifactId>


<version>2.8.2</version>


</plugin>


</plugins>


</pluginManagement>


</build>


</project>


[](


)2.配置文件


=========================================================================


修改配置文件 application.properties


server.servlet.context-path=/sm


数据库配置文件:


#指定连接池类型


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


#指定驱动


spring.datasource.driver-class-name=com.mysql.jdbc.Driver


#指定 url


spring.datasource.url=jdbc:mysql://localhost:3306/sm


#指定用户名


spring.datasource.username=root


#指定密码


spring.datasource.password=1234


[](


)加入 mybatis 配置




mybatis 的配置:


#指定 mapper 配置文件位置


mybatis.mapper-locations=classpath:/com/yusael/mapper/*.xml


#指定起别名来的类


mybatis.type-aliases-package=com.yusael.entity


在入口类 Application 中添加 @MapperScan


@SpringBootApplication


@MapperScan("com.yusael.dao")


public class Application {


public static void main(String[] args) {


SpringApplication.run(Application.class, args);


}


}


[](


)完整的配置文件




server.servlet.context-path=/sm


#指定连接池类型


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


#指定驱动


spring.datasource.driver-class-name=com.mysql.jdbc.Driver


#指定 url


spring.datasource.url=jdbc:mysql://localhost:3306/sm


#指定用户名


spring.datasource.username=root


#指定密码


spring.datasource.password=1234


#指定 mapper 配置文件位置


mybatis.mapper-locations=classpath:/com/yusael/mapper/*.xml


#指定起别名来的类


mybatis.type-aliases-package=com.yusael.entity


[](


)3. 数据库建表


============================================================================


sm 数据库下建立 t_user 表:


CREATE TABLE t_user (


id varchar(100) NOT NULL,


name varchar(100) DEFAULT NULL,


age int(11) DEFAULT NULL,


bir datetime DEFAULT NULL,


PRIMARY KEY (id)


) ENGINE=InnoDB DEFAULT CHARSET=utf8;


[](


)4. 开发实体类


============================================================================


java.com.yusael.entity 包下创建 User 类;



@Data


@AllArgsConstructor


@NoArgsConstructor


@ToString


public class User {


private String id;


private String name;


private Integer age;


private Date bir;


}


[](


)5. 开发 DAO 接口以及 Mapper


======================================================================================


java.com.yusael.dao 包下创建 UserDAO 类;


resources.com.yusael.mapper 下创建 UserDAOMapper



public interface UserDAO {


void save(User user);


List<User> findAll();


}


<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.yusael.dao.UserDao">


<insert id="save" parameterType="User">

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
一个项目了解 SpringBoot 集成 MyBatis(1),面试必备知识点