1.上来先撸个数据库(这样简单就可以了)
直接给你们写好复制吧
/*
Navicat MySQL Data Transfer
Source Server : 新连接
Source Server Type : MySQL
Source Server Version : 50515
Source Host : localhost:3306
Source Schema : mybatis
Target Server Type : MySQL
Target Server Version : 50515
File Encoding : 65001
Date: 05/07/2020 17:17:05
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(255) NULL DEFAULT NULL,
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`pwd` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '王强', '123');
INSERT INTO `user` VALUES (5, '啊猫', '456789');
INSERT INTO `user` VALUES (5, '啊猫', '456789');
INSERT INTO `user` VALUES (6, 'xyj', '66666');
SET FOREIGN_KEY_CHECKS = 1;
2.整体结构
3.(1)导入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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.xyjgroupId>
<artifactId>springboot-mybatisartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springboot-mybatisname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASEspring-boot.version>
properties>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
3.(2)application.properties与数据库连接
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#整合mybatis
mybatis.type-aliases-package=com.xyj.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
4.构建实体类User
package com.xyj.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author xyj
* @date 2020/7/5 -16:30
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
5.UserMapper接口(增删改查)
package com.xyj.mapper;
import com.xyj.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author xyj
* @date 2020/7/5 -16:32
*/
@Mapper
@Repository
public interface UserMapper {
ListqueryUserList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
6.UserMapper.xml编写(与UserMapper一一对应)
xml version="1.0" encoding="UTF-8" ?>
br mpa-from-tpl="t" /> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xyj.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user
select>
<select id="queryUserById" resultType="User">
select * from user where id =#{id}
select>
<insert id="addUser" parameterType="User">
insert into user (id, name, pwd) values (#{id}, #{name}, #{pwd});
insert>
<update id="updateUser" parameterType="User">
update user set name=#{name}, pwd=#{pwd} where id =#{id};
update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id};
delete>
mapper>
7.增删改查UserController
package com.xyj.controller;
import com.xyj.mapper.UserMapper;
import com.xyj.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author xyj
* @date 2020/7/5 -16:54
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
//查
@GetMapping("/queryUserList")
public ListqueryUserList(){
List userList = userMapper.queryUserList();return userList;
}//增@GetMapping("/addUser")public String addUser(){
userMapper.addUser(new User(6,"xyj","66666"));return "addUser";
}//改@GetMapping("/updateUser")public String updateUser(){
userMapper.updateUser(new User(888,"xyj222","777"));return "updateUser";
}//删@GetMapping("/deleteUser")public String deleteUser(){
userMapper.deleteUser(3);return "deleteUser";
}
}
8.分别在网页里,写出相应的@GetMapping里的单词 即可实现每个操作,与数据库比对
9.这样,我们简单的增删改查就完成了
————————————————
版权声明:本文为CSDN博主「Nym_车厘子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:
https://blog.csdn.net/weixin_44625302/article/details/107141629