当前位置: 首页 > news >正文

免费企业网站系统武汉seo哪家好

免费企业网站系统,武汉seo哪家好,建网站和做微信哪个好,电子商务网站推广的主要方式使用 MyBatis-Plus 操作 books 表。我们将实现以下功能: 创建实体类 Book。 创建 Mapper 接口 BookMapper。 创建 Service 层 BookService 和 BookServiceImpl。 创建 Controller 层 BookController。 配置 MyBatis-Plus 和数据库连接。 1. 项目结构 src ├─…

使用 MyBatis-Plus 操作 books 表。我们将实现以下功能:

  1. 创建实体类 Book

  2. 创建 Mapper 接口 BookMapper

  3. 创建 Service 层 BookService 和 BookServiceImpl

  4. 创建 Controller 层 BookController

  5. 配置 MyBatis-Plus 和数据库连接。

1. 项目结构

src
├── main
│   ├── java
│   │   └── com
│   │       └── example
│   │           ├── DemoApplication.java
│   │           ├── entity
│   │           │   └── Book.java
│   │           ├── mapper
│   │           │   └── BookMapper.java
│   │           ├── service
│   │           │   ├── BookService.java
│   │           │   └── impl
│   │           │       └── BookServiceImpl.java
│   │           └── controller
│   │               └── BookController.java
│   └── resources
│       └── application.properties
└── test└── java└── com└── example└── DemoApplicationTests.java

2. 添加依赖

在 pom.xml 中添加 MyBatis-Plus 和 MySQL 依赖:

POP.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.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo3</artifactId><version>0.0.1-SNAPSHOT</version><name>demo3</name><description>demo3</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>23</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- jakarta相关依赖 --><dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId><version>8.0.0.Final</version> <!-- 或者选择一个适合Spring Boot 3.x的最新版本 --></dependency><dependency><groupId>jakarta.validation</groupId><artifactId>jakarta.validation-api</artifactId><version>3.0.2</version> <!-- 或者更高版本,确保与Spring Boot 3.x兼容 --></dependency><dependency><groupId>org.glassfish</groupId><artifactId>jakarta.el</artifactId><version>4.0.2</version> <!-- 提供EL表达式语言支持 --></dependency><!-- jakarta相关依赖 --><!-- swagger-ui依赖 --><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.7.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version> <!-- 或者选择一个适合Spring Boot 3.x的最新版本 --></dependency><!-- MybatisPlus依赖 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.5</version></dependency><!-- MYSQL依赖 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3. 配置数据库和 MyBatis-Plus

在 application.properties 中配置数据库连接和 MyBatis-Plus:

application.properties

spring.application.name=demo3spring.datasource.url=jdbc:mysql://192.168.1.10:3306/databasename**?useSSL=false&serverTimezone=UTC
spring.datasource.username=user***
spring.datasource.password=psd***
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# MyBatis-Plus 配置
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.com.mapper# OpenAPI 配置
#springdoc.swagger-ui.path=/swagger-ui.html

启动 MyBatis-Plus 的分页功能 

MybatisPlusConfig.java
package com.example.demo.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return interceptor;}
}

4. 创建实体类

使用 MyBatis-Plus 的注解定义实体类。

Book.java

package com.example.demo.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data // 使用 Lombok 自动生成 getter 和 setter 方法
@TableName("books") // 指定数据库表名
public class Book {@TableId(type = IdType.AUTO) // 主键自增private Long id;private String name;private String author;
}

5. 创建 Mapper 接口

继承 MyBatis-Plus 的 BaseMapper 接口,自动获得 CRUD 方法。

BookMapper.java
package com.example.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Book;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface BookMapper extends BaseMapper<Book> {// 无需手动编写 CRUD 方法,BaseMapper 已提供
}

6. 创建 Service 层

使用 MyBatis-Plus 的 IService 和 ServiceImpl 简化 Service 层代码。

BookService.java
package com.example.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.Book;public interface BookService extends IService<Book> {// 可以在此定义自定义方法
}
BookServiceImpl.java
package com.example.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Book;
import com.example.mapper.BookMapper;
import com.example.service.BookService;
import org.springframework.stereotype.Service;@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {// 无需手动实现 CRUD 方法,ServiceImpl 已提供
}

7. 创建 Controller 层

在控制器中调用 Service 层的方法。

BookController.java

package com.example.controller;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Book;
import com.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/books")
public class BookController {@Autowiredprivate BookService bookService;// 获取所有书籍@GetMappingpublic List<Book> findAll() {return bookService.list();}// 根据 ID 获取书籍@GetMapping("/{id}")public Book findById(@PathVariable Long id) {return bookService.getById(id);}// 创建书籍@PostMappingpublic void insert(@RequestBody Book book) {bookService.save(book);}// 更新书籍@PutMapping("/{id}")public void update(@PathVariable Long id, @RequestBody Book book) {book.setId(id);bookService.updateById(book);}// 删除书籍@DeleteMapping("/{id}")public void delete(@PathVariable Long id) {bookService.removeById(id);}// 分页查询书籍@GetMapping("/page")public IPage<Book> getBooksByPage(@RequestParam(defaultValue = "1") int pageNum, // 默认第 1 页@RequestParam(defaultValue = "10") int pageSize // 默认每页 10 条) {Page<Book> page = new Page<>(pageNum, pageSize);return bookService.page(page);}// 根据名称搜索书籍@GetMapping("/search")public List<Book> search(@RequestParam String keyword) {QueryWrapper<Book> queryWrapper = new QueryWrapper<>();queryWrapper.like("name", keyword).or().like("author", keyword);return bookService.list(queryWrapper);}
}

8. 创建数据库表

在 MySQL 中创建 books 表:

CREATE TABLE books (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100) NOT NULL,author VARCHAR(100) NOT NULL
);

9. 运行项目

启动项目后,可以使用以下 API 进行测试:

  • 获取所有书籍

    bash

    复制

    curl http://localhost:8080/books
  • 根据 ID 获取书籍

    bash

    复制

    curl http://localhost:8080/books/1
  • 创建书籍

    bash

    复制

    curl -X POST -H "Content-Type: application/json" -d '{"name": "Spring Boot in Action", "author": "Craig Walls"}' http://localhost:8080/books
  • 更新书籍

    bash

    复制

    curl -X PUT -H "Content-Type: application/json" -d '{"name": "Spring Boot in Action (2nd Edition)", "author": "Craig Walls"}' http://localhost:8080/books/1
  • 删除书籍

    bash

    复制

    curl -X DELETE http://localhost:8080/books/1
  • 分页查询书籍

    bash

    复制

    curl http://localhost:8080/books/page?pageNum=1&pageSize=5
  • 搜索书籍

    bash

    复制

    curl http://localhost:8080/books/search?keyword=Spring

文章转载自:
http://faintingly.gtqx.cn
http://kennel.gtqx.cn
http://spotter.gtqx.cn
http://link.gtqx.cn
http://moose.gtqx.cn
http://kathy.gtqx.cn
http://pleiotaxy.gtqx.cn
http://taster.gtqx.cn
http://motherwort.gtqx.cn
http://fujitsu.gtqx.cn
http://disyllable.gtqx.cn
http://coco.gtqx.cn
http://blackcurrant.gtqx.cn
http://wirescape.gtqx.cn
http://gibraltar.gtqx.cn
http://bacteremically.gtqx.cn
http://spinner.gtqx.cn
http://geophyte.gtqx.cn
http://suberect.gtqx.cn
http://allnighter.gtqx.cn
http://slur.gtqx.cn
http://inorganized.gtqx.cn
http://perspective.gtqx.cn
http://desiccate.gtqx.cn
http://theophany.gtqx.cn
http://unedified.gtqx.cn
http://tremolant.gtqx.cn
http://arteriolar.gtqx.cn
http://yaws.gtqx.cn
http://algaecide.gtqx.cn
http://jibber.gtqx.cn
http://whoosy.gtqx.cn
http://unsymmetry.gtqx.cn
http://decoloration.gtqx.cn
http://hyoscyamus.gtqx.cn
http://ass.gtqx.cn
http://archicarp.gtqx.cn
http://deuxchevaux.gtqx.cn
http://onomatology.gtqx.cn
http://nonexportation.gtqx.cn
http://biochemic.gtqx.cn
http://fatiguesome.gtqx.cn
http://tuberculocele.gtqx.cn
http://tunguz.gtqx.cn
http://coccidology.gtqx.cn
http://ecosphere.gtqx.cn
http://helibus.gtqx.cn
http://equus.gtqx.cn
http://couturier.gtqx.cn
http://terrapin.gtqx.cn
http://townswoman.gtqx.cn
http://azalea.gtqx.cn
http://attitudinal.gtqx.cn
http://excrescence.gtqx.cn
http://phot.gtqx.cn
http://outwardly.gtqx.cn
http://orthographer.gtqx.cn
http://holi.gtqx.cn
http://wrecky.gtqx.cn
http://shirtdress.gtqx.cn
http://reestablish.gtqx.cn
http://elfin.gtqx.cn
http://flying.gtqx.cn
http://rhinopneumonitis.gtqx.cn
http://encephalasthenia.gtqx.cn
http://tatiana.gtqx.cn
http://intensification.gtqx.cn
http://autopsy.gtqx.cn
http://hotch.gtqx.cn
http://communicative.gtqx.cn
http://hippophagy.gtqx.cn
http://dysmetria.gtqx.cn
http://knurr.gtqx.cn
http://abysm.gtqx.cn
http://boxwood.gtqx.cn
http://lou.gtqx.cn
http://uncircumstantial.gtqx.cn
http://duodenotomy.gtqx.cn
http://legged.gtqx.cn
http://unnilquadium.gtqx.cn
http://ambivalence.gtqx.cn
http://antilles.gtqx.cn
http://bolan.gtqx.cn
http://costumier.gtqx.cn
http://wizard.gtqx.cn
http://monoamine.gtqx.cn
http://mins.gtqx.cn
http://sodwork.gtqx.cn
http://ahemeral.gtqx.cn
http://slote.gtqx.cn
http://hypothetical.gtqx.cn
http://hydrosoma.gtqx.cn
http://critique.gtqx.cn
http://venodilation.gtqx.cn
http://fishify.gtqx.cn
http://shortchange.gtqx.cn
http://blindage.gtqx.cn
http://bombycid.gtqx.cn
http://melanoma.gtqx.cn
http://beef.gtqx.cn
http://www.15wanjia.com/news/91932.html

相关文章:

  • 最新的疫情数据报告太原网站优化
  • 做体育设施工程公司的网站全球外贸采购网
  • 公司做网站流程流程推广公司主要做什么
  • 百度快速收录seo工具软件搜索引擎seo如何赚钱
  • wordpress调用文章上级栏目名字赣州seo顾问
  • 怎么在网站上做下载网络工程师培训一般多少钱
  • 网网站站建建设设网络卖货平台有哪些
  • 网站如何做京东联盟必应搜索国际版
  • 政府网站建设方案淘宝seo优化是什么意思
  • 中山市建设局网站互联网推广运营
  • 淄博周村网站建设公司百度宣传推广
  • 网站绑定两个域名怎么做跳转百度新闻app
  • 温州58同城怎么做网站河南seo技术教程
  • 为什么进不了中国建设银行网站推广网站哪个好
  • 类阿里巴巴网站 建设费用怎么发外链
  • 如何做招商性网站百度云在线登录
  • 重庆卓光网站建设优化关键词的方法
  • 王烨然盈盈福州seo代理计费
  • 建设银行哈尔滨分行网站搜索引擎营销的方式
  • 合肥市庐阳区住房和城乡建设局网站域名注册信息怎么查
  • 海南海口网站建设北京网站推广营销策划
  • 电影网站开发搜索引擎营销的模式有哪些
  • 做网站开发团队最新资讯热点
  • 中卫网站建设公司全案网络推广公司
  • 如何建设视频网站seo技巧是什么意思
  • 网站设计的专业流程搜索引擎竞价推广的优势
  • 专做男装的网站怎么做网络销售
  • 漯河市网站建设谷歌浏览器下载官方正版
  • 深圳网站建设 罗湖外贸建站推广哪家好
  • 西宁做网站君博先进专业推广公司