Spring Boot整合Mybatis对数据库实现增查删改(二)

Spring Boot整合Mybatis对数据库实现增查删改(二)下一节

这篇文章我们就来看看怎么使用spring boot整合mybatis来连接数据库并且创建表

首先看一下最终框架结构

在这里插入图片描述

再来看一下pom文件

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>2.1.0</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-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这里面是加了几个依赖的,大家仔细看一下

修改配置文件

本文不使用application.properties文件 而使用更加简洁的application.yml文件。将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),本文创建了两个yml文件(application.yml和application-dev.yml),分别来看一下内容

application.yml

server:
  port: 8080


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


  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect



mybatis:
  mapper-locations: classpath:mapping/mapper/*.xml
  type-aliases-package: com.example.demo.entity
  config-location: classpath:mapping/mybatis-config.xml


#showSql
logging:
  level:
    com:
      example:
        mapper: debug

application-dev.yml


server:
  port: 8080


spring:
  datasource:
    username: root
    password: root
    url:jdbc: mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8
    driver-class-name: com.mysql.jdbc.Driver



mybatis:
  mapper-locations: classpath:mapping/mapper/*.xml
  type-aliases-package: com.example.demo.entity
  config-location: classpath:mapping/mybatis-config.xml


#showSql
logging:
  level:
    com:
      example:

然后开始创建实体类实现业务流程

首先创建文件夹controller、entity、mapper、service。resources下创建mapping创建子文件夹mapper。下面开始看一下各个类的代码。

先看一下启动类

DemoApplication

package com.example.demo;

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


@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {

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

}

UserController.java

package com.example.demo.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")  
public class UserController {
}

User.java

package com.example.demo.entity;


import javax.persistence.*;

@Table(name="ser")          //这里是创建数据库的名称
@Entity
public class User {
    @Id								//这里是设置主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column
    private String name;
    @Column
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

UserMapper.java

package com.example.demo.mapper;

public interface UserMapper {
}

UserServiceImpl.java

package com.example.demo.service.impl;

public class UserServiceImpl {
}

UserService.java

package com.example.demo.service;

import com.example.demo.entity.User;

public interface UserService {
}

上面这三个包是后面操作数据库的增查删改用的,所以这里没有写代码

UserMapper.xml

这里面注意改自己命名的文件夹的地址,然后注意文件的后缀名是.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.example.demo.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="age" jdbcType="VARCHAR" property="age" />
    </resultMap>


</mapper>

mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
    </typeAliases>
</configuration>

连接和创建数据库所用到的代码就是这些了,
数据库我用的是MySQL,然后用来一个可视化工具Navicat Premium,下面来看一下创建的数据表
在这里插入图片描述这样,数据库的连接和创建都已经完成了,大家要注意配置文件里面的类的地址和你自己创建的一定要对应,要不然会报错哟。
下一节写怎么实现对数据库的增查删改,咱们下期再见。有什么问题请大家在下方留言,谢谢大家。
下一节

发布了22 篇原创文章 · 获赞 185 · 访问量 11万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览