SpringBoot项目总结之Activity

1.初始化数据库

drop database if exists dbactivity;
create database dbactivity default character set utf8;
use dbactivity;
create table tb_activity(
     id bigint primary key auto_increment,
     title varchar(100) not null,
     category varchar(100) not null,
     startTime datetime not null,
     endTime datetime not null,
     remark text,
     state tinyint,
     createdTime datetime not null,
     createdUser varchar(100)
)engine=InnoDB;
insert into tb_activity values (null,'ABS','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'VALIDATE','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'VALIDATE','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'ABS','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'ACCESS','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'SD_ALL','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'DF','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'FG','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'ER','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'EF','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'FFF','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'EEE','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'WWW','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'AAA','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'CCC','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'XXXX','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'WWW','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');
insert into tb_activity values (null,'QQQQ','cuoxiao','2020-02-02 15:00:00','2020-02-03 15:00:00','...',1,now(),'xiaoli');

2.创建项目

2.1创建一个springboot工程
  • 添加依赖

image.png

2.2配置文件
//================配置文件中不能出现中文==================
// 取消console中的默认输出图像
spring.main.banner-mode=off

// 1.配置数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/dbactivity?serverTimezone=GMT%2B8&characterEncoding=utf-8
spring.datasource.username=root //数据库账号
spring.datasource.password=root     //密码

// 2.配置mybatis
#spring mybatis
mybatis.mapper-locations=classpath:/mapper/*/*.xml

// 3.配置日志
#Spring log
logging.level.com.py=debug

// 4.配置服务器
# SpingMVC Tomcat:port
server.port=80
// 默认url
server.servlet.context-path=/

// 5.配置Thymeleaf
# Spring thymeleaf config
spring.thymeleaf.cache=false
// 配置Thymeleaf扫描的前缀和后缀
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html
  • 在pom.xml中加入devtools依赖,可以不用每次修改src目录下的文件后都要重启服务器
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
3.创建所需的目录文档

image.png

4.开始代码
  • 4.1创建com.py.pj.activity.pojo.Activity.java类
  • image.png
package com.py.pj.activity.pojo;

/**
* @author WL
* @version 创建时间:2020-9-1 17:55:40
* @Description ActivityPOJO
*/

import java.util.Date;

public class Activity {

    private Long id;
    private String title;
    private String category;
    private Date startTime;
    private Date endTime;
    private String remark;
    private Integer state;
    private Date createdTime;
    private String createdUser;

    public Long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public String getCreatedUser() {
        return createdUser;
    }

    public void setCreatedUser(String createdUser) {
        this.createdUser = createdUser;
    }

    @Override
    public String toString() {
        return "Activity [id=" + id + ", title=" + title + ", category=" + category + ", startTime=" + startTime
                + ", endTime=" + endTime + ", remark=" + remark + ", state=" + state + ", createdTime=" + createdTime
                + ", createdUser=" + createdUser + "]";
    }

}
  • 4.2创建com.py.pj.activity.dao.ActivityMapper.java接口
package com.py.pj.activity.dao;


import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.py.pj.activity.pojo.Activity;

/**
* @author WL
* @version 创建时间:2020-9-1 18:00:37
* @Description 类描述
*/
@Mapper
public interface ActivityMapper {
    
    @Select("select * from tb_activity")
    List<Activity> findAll();
    
    int insertAc(Activity ac);
    
    @Insert("delete from tb_activity where id=#{id}")
    int deleteById(Integer id);
    
    @Select("select * from tb_activity where id=#{id}")
    Activity findById(Integer id);
    
    int updateById(Activity ac);

}
  • 4.3创建com.py.pj.activity.service.ActivityService.java接口
package com.py.pj.activity.service;

import java.util.List;

import com.py.pj.activity.pojo.Activity;

/**
* @author WL
* @version 创建时间:2020-9-1 18:00:48
* @Description 类描述
*/
public interface ActivityService {
    
    List<Activity> findAll();
    
    int insertAc(Activity ac);
    
    int deleteById(Integer id);
    
    Activity findById(Integer id);
    
    int updateById(Activity ac);

}
  • 4.4创建com.py.pj.activity.serviceImpl.ActivityServiceImpl.java类
package com.py.pj.activity.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.py.pj.activity.dao.ActivityMapper;
import com.py.pj.activity.pojo.Activity;
import com.py.pj.activity.service.ActivityService;

/**
* @author WL
* @version 创建时间:2020-9-1 18:01:12
* @Description 类描述
*/
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    
    @Override
    public List<Activity> findAll() {
        return activityMapper.findAll();
    }

    @Override
    public int insertAc(Activity ac) {
        return activityMapper.insertAc(ac);
    }

    @Override
    public int deleteById(Integer id) {
        return activityMapper.deleteById(id);
    }

    @Override
    public Activity findById(Integer id) {
        return activityMapper.findById(id);
    }

    @Override
    public int updateById(Activity ac) {
        return activityMapper.updateById(ac);
    }

}
  • 4.5创建com.py.pj.activity.controller.ActivityController.java
package com.py.pj.activity.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.py.pj.activity.pojo.Activity;
import com.py.pj.activity.service.ActivityService;

/**
* @author WL
* @version 创建时间:2020-9-1 18:02:25
* @Description 类描述
*/
@Controller
@RequestMapping("/ac/")
public class ActivityController {
    
    @Autowired
    private ActivityService activityService;
    
    @RequestMapping("index")
    public String indexPage(Model model) {
        List<Activity> lists = activityService.findAll();
        model.addAttribute("lists", lists);
        return "index";
    }

    @RequestMapping("toInsert")
    public String toAdd() {
        return "ac-add";
    }
    
    //增
    @RequestMapping("addAc")
    public String doAdd(Activity ac) {
        ac.setStartTime(new Date());
        ac.setEndTime(new Date());
        ac.setCreatedTime(new Date());
        activityService.insertAc(ac);
        return "redirect:/ac/index";
    }
    
    //删除
    @RequestMapping("doDelete")
    public String doDelete(Integer id) {
        activityService.deleteById(id);
        return "redirect:/ac/index";        
    }
    
    @RequestMapping("doUpdate")
    public String findByid(Integer id, Model model) {
        Activity ac = activityService.findById(id);
        model.addAttribute("ac",ac);
        return "ac-update";
        
    }
    
    //修改
    @RequestMapping("updateAc")
    public String doUpdate(Activity ac) {
        ac.setStartTime(new Date());
        ac.setEndTime(new Date());
        ac.setCreatedTime(new Date());
        activityService.updateById(ac);
        return "redirect:/ac/index";
    }
    
}
  • 4.6创建mapper/activity/ActivityMapper.xml
  • image.png
<?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.py.pj.activity.dao.ActivityMapper">

    <insert id="insertAc">
        insert into tb_activity
        values(null,#{title},#{category},
        #{startTime},#{endTime},#{remark},#{state},#{createdTime},#{createdUser})
    </insert>

    <update id="updateById">
        update tb_activity set
        title=#{title},category=#{category},
        startTime=#{startTime},endTime=#{endTime},
        remark=#{remark},state=#{state},
        createdTime=#{createdTime},createdUser=#{createdUser}
        where id=#{id}
    </update>

</mapper>
  • 4.7创建templates/pages/*.html
  • image.png
  • 4.7.1 index.html(默认界面)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
    table tr td{
        text-align: center;
        }
    
</style>


</head>
<body>
    <h1>Index Page</h1>
    <table border="2px solid red">
    
        <tr>
            <a th:href="toInsert">-Add-</a>
        </tr>
        <tr>
            <th>id</th>
            <th>title</th>
            <th>category</th>
            <th>startTime</th>
            <th>endTime</th>
            <th>remark</th>
            <th>state</th>
            <th>createdTime</th>
            <th>createdUser</th>
            <th th:colspan="2">option</th>
        </tr>
        <tr th:each="list :${lists}">
            <td th:text="${list.id}">1</td>
            <td th:text="${list.title}">2</td>
            <td th:text="${list.category}">3</td>
            <td th:text="${#dates.format(list.startTime, 'dd/MM/yyyy HH:mm')}">4</td>
            <td th:text="${#dates.format(list.endTime, 'dd/MM/yyyy HH:mm')}">5</td>
            <td th:text="${list.remark}" width="22px">6</td>
            <td th:text="${list.state}">7</td>
            <td th:text="${#dates.format(list.createdTime, 'dd/MM/yyyy HH:mm')}">8</td>
            <td th:text="${list.createdUser}">9</td>
            <td><a href="#" th:href="@{/ac/doDelete(id=${list.id})}">delete</a></td>
            <td><a href="#" th:href="@{/ac/doUpdate(id=${list.id})}">update</a></td>
        </tr>
    </table>
</body>
</html>

4.7.2 ac-add (添加页面)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    
    <h1>Add Page</h1>
    <form th:action="@{/ac/addAc}">

        <table border="1px solid">

            <tr>
                <td>title</td>
                <td><input type="text" name="title" ></td>
            </tr>
            <tr>
                <td>category</td>
                <td><input type="text" name="category" ></td>
            </tr>

            <tr>
                <td>remark</td>
                <td><textarea rows="1" cols="22" name="remark" ></textarea></td>
            </tr>
            <tr>
                <td>state</td>
                <td><input type="text" name="state" ></td>
            </tr>
            <tr>
                <td>createdUser</td>
                <td><input type="text" name="createdUser" ></td>
            </tr>
            <tr >
                <td colspan="2"><input type="submit"></input></td>
            </tr>



        </table>
    </form>

</body>
</html>

4.7.3 ac-update.html (更新页面)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h1>Update Page</h1>
    <form th:action="@{/ac/updateAc}">

        <table border="1px solid">
            <tr>
                <input type="hidden" name="id" th:value="${ac.id}">
            </tr>
            <tr>
                <td>title</td>
                <td><input type="text" name="title" th:value="${ac.title}"></td>
            </tr>
            <tr>
                <td>category</td>
                <td><input type="text" name="category"
                    th:value="${ac.category}"></td>
            </tr>

            <tr>
                <td>remark</td>
                <td><textarea rows="1" cols="22" name="remark"
                        th:text="${ac.remark}">
                </textarea></td>
            </tr>
            <tr>
                <td>state</td>
                <td><input type="text" name="state" th:value="${ac.state}"></td>
            </tr>
            <tr>
                <td>createdUser</td>
                <td><input type="text" name="createdUser"
                    th:value="${ac.createdUser}"></td>
            </tr>
            <tr>
                <td colspan="2">
                <input type="submit"></input>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

流浪成疯
7 声望3 粉丝

学习