创建SpringBoot项目(使用阿里云代理)
创始人
2024-03-30 19:39:56
0

第一个SpringBoot项目

目录

1.更改服务器URL

2. 点击下一步​编辑

 3.勾选三项,点击创建​编辑

  4.等待同步完成(将近5分钟)在等待的过程中创建数据库​编辑

6.mysql创建class3数据库

 7.引入mybatis mysql依赖包

8.创建包(如图所示)controller  dao pojo service utils ​编辑

 9.在resources中创建mappers文件夹

 10.application.properties配置文件中写入,更改连接地址、用户名和密码

11.创建实体类

 12.dao层

 13. service层

 14.controller层

15.mappers sql语句

 16.templates 三个页面

17.运行成功 


https://start.aliyun.com

1.更改服务器URL

2. 点击下一步

 3.勾选三项,点击创建

  4.等待同步完成(将近5分钟)在等待的过程中创建数据库

6.mysql创建class3数据库

 创建person、idcard表

sql语句:

create table idcard(
id int  not null primary key AUTO_INCREMENT,
stuid varchar(15),
classname varchar(15)
);
create table person(
id int not null primary key AUTO_INCREMENT,
age int,
name varchar(15),
idcard int not null,
foreign key(idcard) references idcard(id)
);
INSERT INTO idcard VALUES(1,2,"一班"),(2,3,"二班"),(3,4,"三班");
INSERT INTO person VALUES(1,18,"小红",1),(2,19,"小华",2),(3,20,"小明",3);

 7.引入mybatis mysql依赖包

 mybatis mysql依赖包:

 org.mybatis.spring.bootmybatis-spring-boot-starter2.2.2mysqlmysql-connector-javaorg.springframework.bootspring-boot-starter-thymeleaf

pom.xml中完整代码


4.0.0hnucm.exampleSpringBoot0.0.1-SNAPSHOTSpringBootSpringBoot1.8UTF-8UTF-82.3.7.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-devtoolsruntimetrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engineorg.springframework.bootspring-boot-starter-thymeleaforg.mybatis.spring.bootmybatis-spring-boot-starter2.2.2mysqlmysql-connector-javaorg.springframework.bootspring-boot-dependencies${spring-boot.version}pomimportorg.apache.maven.pluginsmaven-compiler-plugin3.8.11.81.8UTF-8org.springframework.bootspring-boot-maven-plugin2.3.7.RELEASEcom.hnucm.springboot.Applicationrepackagerepackage

8.创建包(如图所示)controller  dao pojo service utils 

 9.在resources中创建mappers文件夹

 10.application.properties配置文件中写入,更改连接地址、用户名和密码

spring.application.name=SpringBoot
server.port=8080# 数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/class3?characterEncoding=utf8&serverTimezone=UTC
# 用户名和密码
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.name=defaultDataSource# mybatis配置 sql写到配置文件
# 实体类包
mybatis.type-aliases-package=com.hnucm.springboot.pojo
# sql配置文件xml路径
mybatis.mapper-locations=classpath:mappers/*.xml# 日志输出路径
logging.level.com.hnucm.springboot=debug

11.创建实体类

package com.hnucm.springboot.pojo;import lombok.Data;@Data
public class Idcard {private int id;private String stuid;private String classname;}
package com.hnucm.springboot.pojo;import lombok.Data;@Data
public class Person {private int  id;private int   age;private String  name;private Idcard idcardid;
}

 12.dao层

package com.hnucm.springboot.dao;import com.hnucm.springboot.pojo.Idcard;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface IdcardMapper {public Idcard findIdcardById(Integer id);public int addIdcard(Idcard idcard);public int deleteIdcardById(Integer id);
}
package com.hnucm.springboot.dao;import com.hnucm.springboot.pojo.Person;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface PersonMapper {public List findAllPersonIdcard();public List findAllPersonIdcard2();public List findAllPerson();public int deletePersonById(Integer id);public int addPerson(Person person);public int updatePerson(Person person);
}

 13. service层

package com.hnucm.springboot.service;import com.hnucm.springboot.pojo.Person;import java.util.List;public interface PersonService {public List findAllPerson();public int deletePersonById(Integer id,Integer idcardid);public int addPerson(Person person);public int updatePerson(Person person);
}
package com.hnucm.springboot.service;import com.hnucm.springboot.dao.IdcardMapper;
import com.hnucm.springboot.dao.PersonMapper;
import com.hnucm.springboot.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.List;@Service
public class PersonServiceImpl implements PersonService{@AutowiredPersonMapper personMapper;@AutowiredIdcardMapper idcardMapper;
//    @Override
//    public List findAllPerson() {
//        return personMapper.findAllPerson();
//    }@Transactional@Overridepublic int deletePersonById(Integer id,Integer idcardid) {idcardMapper.deleteIdcardById(idcardid);return personMapper.deletePersonById(id);}@Overridepublic int addPerson(Person person) {int result1= idcardMapper.addIdcard(person.getIdcardid());int result2=  personMapper.addPerson(person);if(result2>0&&result1>0){return result1;}return 0;}@Overridepublic int updatePerson(Person person) {return personMapper.updatePerson(person);}@Overridepublic List findAllPerson(){return personMapper.findAllPersonIdcard2();}
}

 14.controller层

package com.hnucm.springboot.controller;import com.hnucm.springboot.pojo.Person;
import com.hnucm.springboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class PersonController {@AutowiredPersonService personService;@RequestMapping("personlist")public String personlist(Model model){model.addAttribute("personlist",personService.findAllPerson());return "personlist.html";}@RequestMapping("deletepersonbyid")public String deletepersonbyid(int id,int idcardid){personService.deletePersonById(id,idcardid);return "redirect:/personlist";}@RequestMapping("addPerson")public String addPerson(){return "addPerson.html";}@RequestMapping("addPersoncommit")public String addPersoncommit(Person person){personService.addPerson(person);return "redirect:/personlist";}@RequestMapping("updateperson")public String addPersoncommit(Model model,Person person){model.addAttribute("id",person.getId());model.addAttribute("name",person.getName());model.addAttribute("age",person.getAge());return "updateperson.html";}@RequestMapping("updatePersoncommit")public String updatePersoncommit(Person person){personService.updatePerson(person);return "redirect:/personlist";}
}

15.mappers sql语句



insert into idcard(stuid,classname) values (#{stuid},#{classname})delete from idcard where id =#{id}


delete from person where id=#{id}
insert into person(name,age,idcardid) values (#{name},#{age},#{idcardid.id})update personset name=#{name},age=#{age}where id=#{id}

 16.templates 三个页面

addPerson.html



Title


增加用户

用户名:

年龄:

学号:

班级:

 personlist.html



Title


用户id用户姓名用户年龄学号班级删除更新
删除用户更新用户

 updateperson.html



Title


更新用户

id:

用户名:

年龄:

17.运行成功 

 

 

 

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...