mybatisplus iservice接口(iphone 8 plus怎么样)

Mybatis-plus之IService的使用

大家好,又见面了,我是你们的朋友全栈君。

新建实体类

package com.example.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName(value = "user")
public class User {
    @TableId(value = "id",type = IdType.AUTO)//主键自增
    private Integer id;
    @TableField(value = "name")
    private String name;
    @TableField(value = "pwd")
    private String pwd;
}

新建Mapper接口继承BaseMapper

package com.example.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

编写Service接口继承IService

package com.example.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pojo.User;


public interface UserService  extends IService<User> {
}

编写UserviceImpl实现类,继承ServiceImpl并实现UserService

package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

测试类(这里仅测试单个插入和批量插入):

package com.example;

import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest
class MybatisgreneratorApplicationTests {

	@Autowired
	private UserService userService;
    

    //测试批量插入
	@Test
	public void testServiceSaveBatch(){
		List<User> userList = new ArrayList<>();
		User user = new User();
		user.setName("xiaohua");
		user.setPwd("123456");

		User user1 = new User();
		user1.setName("xiaoming");
		user1.setPwd("123456");
		userList.add(user);
		userList = Arrays.asList(user,user1);
		System.out.println(userService.saveBatch(userList));

	}

    //单个实体插入
	@Test
	public void testServiceSave(){
		User user = new User();
		user.setName("李娜娜");
		user.setPwd("123456");
		System.out.println(userService.save(user));

	}


}

配置文件连接数据库

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

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<artifactId>mybatisplus</artifactId>
		<groupId>com.example</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.example</groupId>
	<artifactId>mybatisgrenerator</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mybatisgrenerator</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</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

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

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.2</version>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.4.1</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>


	</dependencies>

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

</project>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/127552.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SpringBoot——spring-data-jpa之分页查询

    SpringBoot——spring-data-jpa之分页查询SpringBoot——spring-data-jpa之分页查询

    2022年4月23日
    87
  • 阿里云ddns过程记录

    阿里云ddns过程记录申请了阿里云一年的动态域名,收费的,闲置了几个月,用openwrt一直没成功,最近研究asterisk部署,有个公网的ddns还是方便不少,所以把闲置的域名得拾起来了,过程如下1.开启阿里云后台权限(在访问控制菜单中,文章最后有链接)AliyunDNSReadOnlyAccessAliyunDNSFullAccess2.下载脚本运行GitHub-risfeng/aliyun-ddns-shell:阿里云域名解析动态更新IPShell脚本阿里云域名解析动态更新IPSh

    2022年6月6日
    31
  • 18ch_ios14.8 18h17

    18ch_ios14.8 18h1718.2线程和进程18.2.1什么是进程?18.2.1什么是进程?计算机程序只不过是磁盘中可执行的,二进制的数据。它们只有在被读取到内存中,被操作系统调用的时候才开始它们的生命周期。进程(重量级

    2022年8月5日
    7
  • accumulate

    accumulate

    2021年12月8日
    71
  • 空间相关分析(三) 局部莫兰指数的理解与计算「建议收藏」

    空间相关分析(三) 局部莫兰指数的理解与计算「建议收藏」        在上篇中,我们详细地阐述了全局莫兰指数(GlobalMoran’I)的含义以及具体的软件实操方法。今天,就来进一步地说明局部莫兰指数(LocalMoran’I)的含义与计算。        首先说明一下进行局部相关分析的必要性:在全局相关分析中,如果全局莫兰指数显著,我们即可认为在该区域上存在空间相关性。但是,我们还是不知道

    2022年6月25日
    106
  • 数据库之连表查询_数据库怎么查询表的内容

    数据库之连表查询_数据库怎么查询表的内容什么是多表关联查询?有时一个查询结果需要从两个或两个以上表中提取字段数据,此时需要使用的就是多表关联查询。链接查询主要分为三种:内连接、外连接、交叉连接。内连接使用比较运算符(包括=、>、<、<>、>=、<=、!>和!<)进行表间的比较操作,查询与连接条件相匹配的数据。根据所使用的比较方式不同,内连接分为等值连接、自然连接和自连接三种。关键…

    2025年11月17日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号