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

创建SpringBoot项目(使用阿里云代理)

第一个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依赖包:

 <!--        引入mybaits mysql依赖包-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

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

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

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>
    <groupId>hnucm.example</groupId>
    <artifactId>SpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot</name>
    <description>SpringBoot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--        引入mybaits mysql依赖包-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.hnucm.springboot.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

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<Person> findAllPersonIdcard();
    public List<Person> findAllPersonIdcard2();
    public List<Person> 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<Person> 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{

    @Autowired
    PersonMapper personMapper;
    @Autowired
    IdcardMapper idcardMapper;
//    @Override
//    public List<Person> findAllPerson() {
//        return personMapper.findAllPerson();
//    }

   @Transactional
    @Override
    public int deletePersonById(Integer id,Integer idcardid) {
       idcardMapper.deleteIdcardById(idcardid);
       return personMapper.deletePersonById(id);
   }
    @Override
    public int addPerson(Person person) {
       int result1= idcardMapper.addIdcard(person.getIdcardid());
        int result2=  personMapper.addPerson(person);
        if(result2>0&&result1>0){
         return result1;}
        return 0;
    }

    @Override
    public int updatePerson(Person person) {
        return personMapper.updatePerson(person);
    }
    @Override
    public List<Person> 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 {
    @Autowired
    PersonService 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语句

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hnucm.springboot.dao.IdcardMapper">
   <select id="findIdcardById"
           resultType="com.hnucm.springboot.pojo.Idcard"
           parameterType="Integer">
       select * from idcard where id =#{id}
   </select>

    <insert id="addIdcard" parameterType="com.hnucm.springboot.pojo.Idcard"
    useGeneratedKeys="true"
    keyProperty="id">
        insert into idcard(stuid,classname) values (#{stuid},#{classname})
    </insert>
    <delete id="deleteIdcardById" parameterType="Integer">
        delete from idcard where id =#{id}
    </delete>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hnucm.springboot.dao.PersonMapper">
    <select id="findAllPerson" resultType="com.hnucm.springboot.pojo.Person">
        select * from person
    </select>
    <delete id="deletePersonById" parameterType="Integer">
        delete from person where id=#{id}
    </delete>
    <insert id="addPerson" parameterType="com.hnucm.springboot.pojo.Person">
insert into person(name,age,idcardid) values (#{name},#{age},#{idcardid.id})
    </insert>
    <update id="updatePerson" parameterType="com.hnucm.springboot.pojo.Person">
        update person
        set name=#{name},age=#{age}
        where id=#{id}
    </update>
    <select id="findAllPersonIdcard2" resultMap="personIdcard2">
        select * from person
    </select>
    <resultMap id="personIdcard2" type="com.hnucm.springboot.pojo.Person">
        <id column="id" property="id"></id>
        <result column="age" property="age"></result>
        <result column="name" property="name"></result>
        <association property="idcardid"
                     javaType="com.hnucm.springboot.pojo.Idcard"
                     column="idcardid"
                     select="com.hnucm.springboot.dao.IdcardMapper.findIdcardById">
        </association>
    </resultMap>

</mapper>

 16.templates 三个页面

addPerson.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>增加用户</h1>
<form th:action="@{/addPersoncommit}" method="post">
  <p>用户名:<input type="text" name="name"><p>
  <p> 年龄:<input type="text" name="age"><p>
    <p> 学号:<input type="text" name="idcardid.stuid"><p>
    <p> 班级:<input type="text" name="idcardid.classname"><p>
  <p><input type="submit" value="提交"><p>
</form>
</body>
</html>

 personlist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <tr>
        <td>用户id</td>
        <td>用户姓名</td>
        <td>用户年龄</td>
        <td>学号</td>
        <td>班级</td>
        <td>删除</td>
        <td>更新</td>
    </tr>
    <tr th:each="person:${personlist}">
        <td th:text="${person.id}"></td>
        <td th:text="${person.name}"></td>
        <td th:text="${person.age}"></td>
        <td th:text="${person.idcardid.stuid}"></td>
        <td th:text="${person.idcardid.classname}"></td>
        <td>
            <a th:href="@{deletepersonbyid(id=${person.id},idcardid=${person.idcardid.id})}">删除用户</a>
        </td>
        <td>
            <a th:href="@{updateperson(id=${person.id},age=${person.age},name=${person.name})}">更新用户</a>
        </td>
    </tr>
</table>
</body>
</html>

 updateperson.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>更新用户</h1>
<form th:action="@{/updatePersoncommit}" method="post">
  <p>id:<input type="text" name="id" th:value="${id}"><p>
  <p>用户名:<input type="text" name="name" th:value="${name}"><p>
  <p> 年龄:<input type="text" name="age" th:value="${age}"><p>
  <p><input type="submit" value="提交"><p>
</form>
</body>
</html>

17.运行成功 

 

 

 

相关文章:

  • 【Unity Shader】Unity中利用GrabPass实现玻璃效果
  • python实现简易数独小游戏
  • 还未入职,这位将来的博导为学生规划了一条高效学习之路
  • 车联网_网络管理ECU状态转换
  • “舔狗机器人”
  • 服务虚拟化HoverFly入门扫盲
  • 踩坑记录——USB键盘睡眠唤醒
  • 【MySQL】官网学习 order by 优化
  • 二叉树广度优先搜索、深度优先搜索(前序、中序、后序)遍历,动图详解-Java/Kotlin双版本代码
  • 【解包裹】基于最小二乘法实现解包裹附matlab代码
  • vim如何进行批量化注释及取消,也在1024表明自己算十分之一的程序员
  • 1024程序员节|【MySQL从入门到精通】【高级篇】(二十七)外连接和内连接如何进行查询优化呢?join的原理了解一波
  • 57.(前端)删除用户操作
  • 手动搭建K8S环境
  • ESP32-C3入门教程 网络篇⑦——基于esp_http_client实现HTTP的POST/GET/PUT/DELETE请求服务器的第三种方法
  • 分享一款快速APP功能测试工具
  • [译] 怎样写一个基础的编译器
  • Angular2开发踩坑系列-生产环境编译
  • Bytom交易说明(账户管理模式)
  • CentOS 7 防火墙操作
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • GitUp, 你不可错过的秀外慧中的git工具
  • Javascript设计模式学习之Observer(观察者)模式
  • k8s 面向应用开发者的基础命令
  • MySQL数据库运维之数据恢复
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • Vue.js 移动端适配之 vw 解决方案
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 使用docker-compose进行多节点部署
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • 如何正确理解,内页权重高于首页?
  • #数学建模# 线性规划问题的Matlab求解
  • $.ajax()方法详解
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (2)MFC+openGL单文档框架glFrame
  • (52)只出现一次的数字III
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (ZT)出版业改革:该死的死,该生的生
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (三)docker:Dockerfile构建容器运行jar包
  • (转)eclipse内存溢出设置 -Xms212m -Xmx804m -XX:PermSize=250M -XX:MaxPermSize=356m
  • (转载)hibernate缓存
  • .aanva
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .net6+aspose.words导出word并转pdf
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • [14]内置对象
  • [20171106]配置客户端连接注意.txt
  • [C++从入门到精通] 14.虚函数、纯虚函数和虚析构(virtual)
  • [C++随笔录] 红黑树
  • [CSS3备忘] transform animation 等
  • [ESP32] 编码旋钮驱动
  • [GN] Vue3.2 快速上手 ---- 核心语法2