SpringBoot增删改查

SpringBoot增删改查

开发工具基于IDEA + Maven + Navicat for MySQL

环境配置

配置Maven

​ 下载Maven后配置环境变量

	MAVEN_HOME=upath
	%JAVA_HOME%\bin
	%MAVEN_HOME%\bin

​ 创建Maven本地仓库,并在%MAVEN_HOME%\conf\setting.xml路径下修改的路径为本地仓库路径

IDEA配置Maven

​ 文件-设置-构建-构建工具-Maven

  • Maven home directory :Maven路径
  • User settings file :%MAVEN_HOME%\conf\setting.xml
  • Local repository :本地仓库路径

项目制作

创建项目

  1. 创建Spring Initializr项目
  2. Dependecies选择
    • Web选项勾选Web
    • Template Engines选项勾选Thymeleaf
    • SQL选项勾选MySQL+JDBC+Mybatis
  3. 完成创建后的pom文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.3.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.pratice.persondemogroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.0.0version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.45version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

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

project>

创建数据库

​ 使用Navicat for MySQL创建数据库springdemo并使用该数据库创建persons

use springdemo;
create table persons(
    id int(3) primary key not null auto_increment,
    name varchar(50),
    gender varchar(10),
    age int(3)
);

创建JavaBean

​ 完成数据库创建后,在我们的项目中创建对应的JavaBean对象

​ src-main-java-uProjectName在该目录下创建一个名为pojo的package

​ 创建完成后在pojo包下创建一个名为Person的java类

public class Person {
     
    private int id;
    private String name;
    private String gender;
    private int age;

    public int getId() {
     
        return id;
    }

    public void setId(int id) {
     
        this.id = id;
    }

    public String getName() {
     
        return name;
    }

    public void setName(String name) {
     
        this.name = name;
    }

    public String getGender() {
     
        return gender;
    }

    public void setGender(String gender) {
     
        this.gender = gender;
    }

    public int getAge() {
     
        return age;
    }

    public void setAge(int age) {
     
        this.age = age;
    }
}

三层架构

mapper数据访问层

  1. 在properties文件中配置数据库驱动
spring.datasource.url=uUrl
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=uPwd
  1. 数据访问层实现 接口+配置

    使用注释语句实现数据库数据操作

@Mapper
public interface PersonMapper {
     
    @Select("select * from persons")
    List<Person> getAllPersons();

    @Select("select * from persons where id=#{id}")
    Person getPersonByID(int id);

    @Insert("insert into persons(name,gender,age) values(#{name},#{gender},#{age})")
    int addPerson(Person person);

    @Update("update persons set name=#{name},gender=#{gender},age=#{age} where id=#{id}")
    int updatePerson(Person person);

    @Delete("delete from persons where id=#{id}")
    int deletePersonByID(int id);
}

service业务逻辑层

  1. Service接口
public interface PersonService {
     
    List<Person> getAllPersons();

    Person getPersonByID(int id);

    int addPerson(Person person);

    int updatePerson(Person person);

    int deletePersonByID(int id);
}
  1. Service接口实现类

    Autowired注解使用控制反转,由Spring自动按类型注入bean对象

    在Service层使用数据访问层mapper中的基本数据库操作组合实现业务逻辑操作。

@Service
public class PersonServiceImpl implements PersonService {
     
    @Autowired
    private PersonMapper personMapper;
    @Override
    public List<Person> getAllPersons() {
     
        return personMapper.getAllPersons();
    }

    @Override
    public Person getPersonByID(int id) {
     
        return personMapper.getPersonByID(id);
    }

    @Override
    public int addPerson(Person person) {
     
        return personMapper.addPerson(person);
    }

    @Override
    public int updatePerson(Person person) {
     
        return personMapper.updatePerson(person);
    }

    @Override
    public int deletePersonByID(int id) {
     
        return personMapper.deletePersonByID(id);
    }
}

controller + 页面 表示层

  • RequestMapping(“url”) 指定路径
  • Map 键值对形式,对页面传参
  • return “url” 返回template中的路径
  • 页面代码中使用过加载模板引擎
  1. 查找所有记录

    controller方法实现

        @RequestMapping("/list")
        public String getAllPersons(Map<String,Object> map){
           
            List<Person> list=personService.getAllPersons();
            map.put("persons", list);
            return "person/list";
        }
    

    页面实现

    
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>人员信息表title>
    head>
    <body>
        <a th:href="@{/add}">添加记录a>
        <table border="1px">
            <tr>
                <td colspan="6" align="center">人员信息表td>
            tr>
            <tr>
                <td>IDtd>
                <td>姓名td>
                <td>性别td>
                <td>年龄td>
            tr>
            <tr th:each="person:${persons}">
                <td th:text="${person.id}">IDtd>
                <td th:text="${person.name}">姓名td>
                <td th:text="${person.gender}">性别td>
                <td th:text="${person.age}">年龄td>
            tr>
        table>
    body>
    html>
    
  2. 添加记录

    controller方法实现

        @GetMapping("/add")
        public String addPerson(){
           
            return "person/add";
        }
    
        @PostMapping("/add")
        public String addPerson(Person person){
           
            personService.addPerson(person);
            return "redirect:/list";
        }
    

    页面实现

    
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>添加记录title>
    head>
    <body>
        <form th:action="@{/add}" th:method="post">
            <table border="1px">
                <tr>
                    <td colspan="2" align="center">添加记录td>
                tr>
                <tr>
                    <td>姓名td>
                    <td>
                        <input type="text" name="name"/>
                    td>
                tr>
                <tr>
                    <td>性别td>
                    <td>
                        <select name="gender">
                            <option>请选择option>
                            <option value="">option>
                            <option value="">option>
                        select>
                    td>
                tr>
                <tr>
                    <td>年龄td>
                    <td>
                        <input type="text" name="age"/>
                    td>
                tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" value="添加"/>
                    td>
                tr>
            table>
        form>
    body>
    html>
    
  3. 修改记录

    controller方法实现

        @GetMapping("/update/{id}")
        public String updatePerson(@PathVariable("id") int id,Map<String,Person> map){
           
            Person person = personService.getPersonByID(id);
            map.put("person", person);
            return "person/update";
        }
    
        @PostMapping("/update")
        public String updatePerson(Person person){
           
            personService.updatePerson(person);
            return "redirect:/list";
        }
    

    页面实现

    
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>修改记录title>
    head>
    <body>
        <form th:action="@{/update}" th:method="post">
            <input type="hidden" name="id" th:value="${person.id}"/>
            <table border="1px">
                <tr>
                    <td colspan="2" align="center">修改记录td>
                tr>
                <tr>
                    <td>IDtd>
                    <td th:text="${person.id}">td>
                tr>
                <tr>
                    <td>姓名td>
                    <td>
                        <input type="text" name="name" th:value="${person.name}"/>
                    td>
                tr>
                <tr>
                    <td>性别td>
                    <td>
                        <select name="gender">
                            <option value="" th:selected="${person.gender}==''">option>
                            <option value="" th:selected="${person.gender}==''">option>
                        select>
                    td>
                tr>
                <tr>
                    <td>年龄td>
                    <td>
                        <input type="text" name="age" th:value="${person.age}"/>
                    td>
                tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" value="修改"/>
                    td>
                tr>
            table>
        form>
    body>
    html>
    
  4. 删除记录

    controller方法实现

        @GetMapping("/delete/{id}")
        public String deletePerson(@PathVariable("id") int id){
           
            personService.deletePersonByID(id);
            return "redirect:/list";
        }
    
  5. 完整controller类代码

@Controller
public class PersonController {
     
    @Autowired
    private PersonService personService;
    @RequestMapping("/list")
    public String getAllPersons(Map<String,Object> map){
     
        List<Person> list=personService.getAllPersons();
        map.put("persons", list);
        return "person/list";
    }

    @GetMapping("/add")
    public String addPerson(){
     
        return "person/add";
    }

    @PostMapping("/add")
    public String addPerson(Person person){
     
        personService.addPerson(person);
        return "redirect:/list";
    }

    @GetMapping("/update/{id}")
    public String updatePerson(@PathVariable("id") int id,Map<String,Person> map){
     
        Person person = personService.getPersonByID(id);
        map.put("person", person);
        return "person/update";
    }

    @PostMapping("/update")
    public String updatePerson(Person person){
     
        personService.updatePerson(person);
        return "redirect:/list";
    }

    @GetMapping("/delete/{id}")
    public String deletePerson(@PathVariable("id") int id){
     
        personService.deletePersonByID(id);
        return "redirect:/list";
    }
}
  1. 完整list页面代码

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>人员信息表title>
head>
<body>
    <a th:href="@{/add}">添加记录a>
    <table border="1px">
        <tr>
            <td colspan="6" align="center">人员信息表td>
        tr>
        <tr>
            <td>IDtd>
            <td>姓名td>
            <td>性别td>
            <td>年龄td>
            <td>修改td>
            <td>删除td>
        tr>
        <tr th:each="person:${persons}">
            <td th:text="${person.id}">IDtd>
            <td th:text="${person.name}">姓名td>
            <td th:text="${person.gender}">性别td>
            <td th:text="${person.age}">年龄td>
            <td><a th:href="@{/update/}+${person.id}">修改a>td>
            <td><a th:href="@{/delete/}+${person.id}" onclick="return confirm('确定要删除该记录吗?')">删除a>td>
        tr>
    table>
body>
html>

打包项目

  • 项目右侧选择Maven Projects
  • 选择Lifecycle-package
  • 右键选择Run Maven Build
  • 在项目的target包中便可以找到打包后项目的jar包
  • 在命令行中运行java -jar xxx.jar便可以实现脱离开发环境运行

你可能感兴趣的:(Java学习,Web开发,SpringBoot)