栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

搭建一个SpringBoot小项目,使用mybatis连接数据库,并对外提供数据库增删改查restful接口

搭建一个SpringBoot小项目,使用mybatis连接数据库,并对外提供数据库增删改查restful接口

搭建一个SpringBoot小项目,使用mybatis连接数据库,并对外提供数据库增删改查restful接口

由于要连接数据库,使用pojo实体类一套

项目结构

1、环境搭建

jdk 1.8

mysql 5.7

maven 3.8.2

IDEA

1.1 建表
CREATE DATAbase `mybatis`;
 
USE `mybatis`;
 
DROp TABLE IF EXISTS `t_user`;
 
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(10) DEFAULT NULL COMMENT '姓名',
  `age` int(2) DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
1.2 建立一个springboot项目

添加依赖

在pom.xml文件中添加依赖,配置后整个文件如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.xingye
    test03
    0.0.1-SNAPSHOT
    test03
    test03
    
        1.8
    
    
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



1.3 连接数据库并整合Mybatis

在如上application.properties文件中添加:

#连接数据库
spring.datasource.username=
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#整合mybatis
mybatis.type-aliases-package=com.xingye.test03.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

第一、二行username与password后输入自己的mysql账号密码

一般username为root

连接上数据库

成功后如图

2、搭建项目

编写相应代码,对应前文项目结构

2.1 实体类pojo-User
package com.xingye.test03.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
}
2.2 接口类mapper-UserMapper
package com.xingye.test03.mapper;

import com.xingye.test03.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    List queryUserList();

    User queryUserById(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);
}

@Mapper表示这是一个MyBatis的mapper类

是Dao层使用@Repository,可不添加该注解

2.3 UserMapper.xml

映射SQL语句,可在mybatis官方文档中找到例子



//此处绑定自己对应的接口类

    
    
    //sql语句
    
        select *
        from t_user
        where id = #{id}
    

    
        insert into t_user (id,name,age) values (#{id},#{name},#{age})
    

    
        update t_user set name=#{name},age=#{age} where id = #{id}
    

    
        delete from t_user where id = #{id}
    

    
        delete from t_user where name = #{name}
    

parameterType为接收参数类型

注:namespace中绑定自己对应的接口类

2.4 控制层controller-UserComtroller
package com.xingye.test03.controller;

import com.xingye.test03.mapper.UserMapper;
import com.xingye.test03.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/queryUserList")
    public List queryUserList(){
        List userList = userMapper.queryUserList();
        for (User user:userList){
            System.out.println(user);
        }
        return userList;
    }

    @GetMapping("/add/{id}/{name}/{age}")
    public String addUser(@PathVariable int id,@PathVariable String name,@PathVariable int age){
        userMapper.addUser(new User(id,name,age));
        return "已成功添加一个用户!";
    }

    @GetMapping("/updateUser/{id}/{name}/{age}")
    public String updateUser(@PathVariable int id,@PathVariable String name,@PathVariable int age){
        userMapper.updateUser(new User(id,name,age));
        return "已成功更改用户信息!";
    }

    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable int id){
        userMapper.deleteUser(id);
        return "已成功删除一个用户!";
    }
}

其中@Getmapping后为打开的接口

@PathVariable实现了restful风格

3、 测试 Test03ApplicationTests
package com.xingye.test03;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.SQLException;

@SpringBootTest
class Test03ApplicationTests {

    @Autowired
    DataSource dataSource;

    @Test
    void contextLoads() throws SQLException {
        System.out.println(dataSource.getClass());
        System.out.println(dataSource.getConnection());
    }

}

localhost:8080/queryUserList

localhost:8080/add/{id}/{name}/{age}

localhost:8080/deleteUser/{id}

localhost:8080/updateUser/{id}/{name}/{age}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/281216.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号