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

自己做网站步骤 域名成都网站外包公司

自己做网站步骤 域名,成都网站外包公司,邯郸做移动网站多少钱,这么做国外网站的国内镜像站目录 一、前言 二、创建项目 创建MySQL数据库和表 创建springboot项目 本文总体代码结构图预览 三、编写代码 (一)新建实体层属性类 (二)新建数据层mapper接口 (三)新建mapper的映射SQL&#xff08…

目录

一、前言

二、创建项目

创建MySQL数据库和表

创建springboot项目

本文总体代码结构图预览

三、编写代码

 (一)新建实体层属性类

(二)新建数据层mapper接口

(三)新建mapper的映射SQL(xml)

(四)新建服务层接口

(五)新建服务层的实现类

 (六)控制层类

(七)配置文件加入扫描xml文件

四、运行代码

 查询全部

根据ID查询 

根据ID删除

 新增

修改

五、代码获取


一、前言

在之前使用了mybatis-plus做项目,感觉不是很方便,特别是涉及到多表查询,虽然MP不用写SQL语句,但是缺点就是不够灵活。做做一些小demo就差不多了。 真正项目里面使用最多的还是mybatis,根据自己的需要写SQL语句。中小型项目sql语句用注解可能会方便一点,大型的项目用xml会更好一些,所以我这里直接演示用的是xml形式

二、创建项目

创建MySQL数据库和表

创建MySQL数据库和创建表的详细步骤(navicat)_mysql navicat新建数据库_云边的快乐猫的博客-CSDN博客

创建springboot项目

使用这个文章里面的方式二创建springboot项目

IDEA创建SpringBoot项目的两个方式详细步骤(2023)_云边的快乐猫的博客-CSDN博客

源码链接:https://pan.baidu.com/s/1TA9QvOG8rRzen6CROvpIeQ?pwd=jiu1

本文总体代码结构图预览

👆按照上面的方式创建springboot项目。这里面的东西都会有的

总体配置文件(仅供参考,可跳过)

server:port: 80
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/dndata?serverTimezone=GMT%2b8username: rootpassword: 123456mybatis:mapper-locations: classpath:mapping/*.xmltype-aliases-package: com.example.jiu.entity

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.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.15</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>Jiu</artifactId><version>0.0.1-SNAPSHOT</version><name>Jiu</name><description>Jiu</description><properties><java.version>11</java.version></properties><dependencies><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.3.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter-test</artifactId><version>2.3.1</version><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.16</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

三、编写代码

 (一)新建实体层属性类

1.新建一个entity包,里面再创建一个和数据库表名对应的类(驼峰命名)

类名:User

这个类是和数据库表里面的字段对应上的,用Date注解里面包含了实体属性常用的get、set构造函数这些,具体可以去自行了解一下。 

package com.example.jiu.entity;
import lombok.Data;
//使用@Data注解
@Data
public class User {private Integer id;private String username;private String password;
}

(二)新建数据层mapper接口

2.新建一个mapper包,里面创建一个数据层的接口

接口名:UserMapper

这里面的方法都是自定义的,是最开始的数据层的方法语句,后续的命名调用都根据这里的来

package com.example.jiu.mapper;
import com.example.jiu.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;@Mapper
public interface UserMapper {//自定义查询全部的方法List<User> findAll();//根据id查询User findById(Integer id);//自定义根据删除的方法int deleteUserById(Integer id);//自定义增加的方法int addUser(User user);//自定义修改的方法int updateUser(User user);
}

(三)新建mapper的映射SQL(xml)

3.resources目录下新建一个mapping包,里面创建一个xml文件

这个xml映射文件:UserMapper.xml

mapper标签里面对应的路径是mapper包里面对应映射接口的位置

SQL语句标签里面的id对应mapper接口里面定义的方法名

如果是查询的语句用resultType,后面跟着实体属性类的位置

其他的增删改用parameterType,后面跟着的是代表要往这个SQL里面传入什么,一般只有删除是int类型,其他的也都是实体属性类位置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--这里面是映射mapper接口里面的,在这里编写sql语句-->
<mapper namespace="com.example.jiu.mapper.UserMapper"><!--namespace:对应mapper接口的全部路径--><!--映射查询方法--><select id="findAll" resultType="com.example.jiu.entity.User"><!--参数一:接口对应的方法名    参数二:封装类的详细地址-->select * from user;<!--要执行的sql语句--></select><select id="findById" resultType="com.example.jiu.entity.User">select * from user where id =#{id}</select><!--映射删除的方法--><delete id="deleteUserById" parameterType="java.lang.Integer">delete from user where id = #{id}</delete><!-- 这里映射增加的方法   --><insert id="addUser" parameterType="com.example.jiu.entity.User">insert into user (username,password) values (#{username},#{password})</insert><!-- 这里定义修改的方法   --><update id="updateUser" parameterType="com.example.jiu.entity.User">update user set username = #{username},password = #{password} where id = #{id}</update></mapper>

(四)新建服务层接口

4.新建一个service包,这里是服务层,主要就是编写逻辑代码的,拿mapper层的数据来编写逻辑代码,然后再交给控制层去调用。

 服务层接口:UserService

这个接口里面的方法和mapper层接口里面的方法一样,这个接口存在的意义是为了降低耦合度

package com.example.jiu.service;
import com.example.jiu.entity.User;
import java.util.List;//这里写的和mapper接口里面的一样
public interface UserService {List<User> findAll();User findById(Integer id);int deleteUserById(Integer id);int addUser(User user);int updateUser(User user);
}

(五)新建服务层的实现类

5.在service包下再建立一个Impl包(包名首字母大写的),里面写上服务类接口的实现类

服务层实现类名称:UserServiceImpl

这里是实现了服务层的接口,并把mapper数据层注入到里面,里面的方法都是用快捷键去实现的,return后面跟着的全局变量.方法

package com.example.jiu.service.Impl;import com.example.jiu.entity.User;
import com.example.jiu.mapper.UserMapper;
import com.example.jiu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;import java.util.List;@Service
public class UserServiceImpl implements UserService {//调用mapper的接口用做全局变量private final UserMapper userMapper;//代表当这个实现类被实例化就会自动找到mapper接口,并注入其中@Autowiredpublic UserServiceImpl(UserMapper userMapper) {this.userMapper = userMapper;}//这里都是用快捷键去实现生成接口里面的方法的。不过return后面跟着的要自己写,全局变量.方法//查询全部@Overridepublic List<User> findAll() {return userMapper.findAll();}//根据id查询@Overridepublic User findById(Integer id) {return userMapper.findById(id);}//根据id删除@Overridepublic int deleteUserById(Integer id) {return userMapper.deleteUserById(id);}//增加@Overridepublic int addUser(@RequestBody User user) {return userMapper.addUser(user);}//修改@Overridepublic int updateUser(User user) {return userMapper.updateUser(user);}
}

 (六)控制层类

6.新建一个controller包,里面创建对应的控制类

控制层类名:UserController

这里面要自动注入服务层的实现类供下面的方法调用

方法名字可以自定义

package com.example.jiu.controller;
import com.example.jiu.entity.User;
import com.example.jiu.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserServiceImpl userServiceImpl;//查询全部的方法,调用服务层@GetMappingpublic List<User> findAll(){return userServiceImpl.findAll();}//根据id查询@GetMapping("/{id}")public User findById(@PathVariable Integer id){return userServiceImpl.findById(id);}//根据id删除,调用服务层@DeleteMapping("/{id}")public int delete(@PathVariable Integer id){return userServiceImpl.deleteUserById(id);}//增加的方法,调用服务层@PostMappingpublic int add(@RequestBody User user){return userServiceImpl.addUser(user);}//修改的方法,调用服务层@PutMappingpublic int put(@RequestBody User user){return userServiceImpl.updateUser(user);}}

(七)配置文件加入扫描xml文件

7.在yml配置文件里面加入这个,代表可以把xml的文件加载扫描到

第一个是代表扫描到xml文件的包路径里面

第二个是代表实体类的包路径

mybatis:mapper-locations: classpath:mapping/*.xmltype-aliases-package: com.example.jiu.entity

四、运行代码

这里用的是postman测试

postman测试后端增删改查_云边的快乐猫的博客-CSDN博客

 查询全部

get请求。就可以直接网页上面运行 

http://localhost:80/user

根据ID查询 

get请求。例如查询id为1的就这样可以查询 

http://localhost:80/user/1

根据ID删除

delete请求。例如删除id为8的那行数据,删除成功返回1

http://localhost:80/user/8

 新增

 post请求。例如添加一个数据进去,由于数据库里面的id是设置为自增的,这里就不用添加id

http://localhost:80/user

修改

put请求,往里面传入数据,用json格式,修改id为9的数据,修改成功返回1 

http://localhost:80/user

五、代码获取

链接:https://pan.baidu.com/s/18Cy1RluCx04_9TRi4nsiVg?pwd=jiux 
提取码:jiux

http://www.yayakq.cn/news/892913/

相关文章:

  • 网站建设建网站年轻的母亲网站建设介绍推广用语
  • wordpress站点标题副标题换行ios认证 东莞网站建设
  • 湖南营销型网站建设 皆来磐石网络关闭微信小程序入口
  • 做系统简单还是网站简单网站建设销售招聘
  • seo网站排名的软件德宏做网站
  • 电商类网站建设需要多少钱为什么我的网站备案通过还是显示未备案
  • 网站开发和移动开发WordPress卡密充值怎么用
  • 烟台做网站的价格建站收入
  • 企业建设好一个网站后_如何进行网站推广?免费域名注册网站哪个好
  • 卖建材的网站广州今天新闻
  • 成都企业网站seo技术响应式布局网站实例
  • 湖北省住房和城乡建设厅门户网站wordpress对接公众号源码
  • 如何做盗版小说网站安徽六安职业技术学院
  • 网站开发商品管理表字段折叠分类目录模板wordpress
  • 昌网站建设wordpress 内容页面
  • 江门网站制作方案定制网络推广培训哪里好
  • 制作网站域名需要多少钱泉州百度seo公司
  • 海外seo网站建设专业深圳网站建设公司
  • windows软件开发台州网站优化
  • 网站排名张家港电子商务网站建设与管理试卷6
  • 哪里有专业做网站的wordpress centos6
  • 湖南省建设信息网站网站建设哪里学
  • 电商网站介绍谷歌搜索引擎下载
  • 环保局网站建设申请做网站运营工资多少
  • 无锡做推广的网站毕业设计做网站大小有什么要求
  • 建旅游网站多少钱做水果网站特点分析
  • 建设银行城东支行网站建站
  • 盐城网站建设小程序公司物联网平台软件开发
  • 开发手机网站步骤网站后台怎么修改密码
  • 什么网站可以做推广python如何调用wordpress