栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot项目实战系列

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot项目实战系列

先导入所需的html资源,html页面保存到templates文件夹中,其余的css,img,js导入到静态资源文件夹中。

1.首页的实现

        直接调用会丢失首页的css格式,需要进行thymeleaf接管,并且改掉引用的格式

        使用thymeleaf语句之前先导入文件头

xmlns:th="http://www.thymeleaf.org

		
		

链接使用@{},文本使用#{}


			
			

2.模拟数据库的实现

创建实体类pojo

部门类:

package com.jie.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {

    private Integer id;
    private String departmentName;
}

 员工类:

package com.jie.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
@Data

@NoArgsConstructor
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender;
    private Department department;
    private Date birth;

    //将日期改为自动创建
    public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
        this.department = department;
        this.birth = new Date();
    }
}

编写dao层

package com.jie.dao;

import com.jie.pojo.Department;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class DepartmentDao {
    private static Map departments = null;
    static {
      departments = new HashMap();
        departments.put(101,new Department(101,"教学部"));
        departments.put(102,new Department(102,"市场部"));
        departments.put(103,new Department(103,"教研部"));
        departments.put(104,new Department(104,"运营部"));
        departments.put(105,new Department(105,"后勤部"));
    }

    public Collection getDepartments(){
        return departments.values();

    }

    public Department getDepartmentById(Integer id){
        return departments.get(id);
    }
}

package com.jie.dao;

import com.jie.pojo.Department;
import com.jie.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {

    private static Map employees = null;
    @Autowired
    private DepartmentDao departmentDao;
   static {
       employees = new HashMap();
       employees.put(1001,new Employee(1001,"AA","A4536251@qq.com",1,new Department(101,"教学部")));
       employees.put(1002,new Employee(1002,"BB","B4536251@qq.com",0,new Department(102,"市场部")));
       employees.put(1003,new Employee(1003,"CC","C4536251@qq.com",1,new Department(103,"教研部")));
       employees.put(1004,new Employee(1004,"DD","D4536251@qq.com",0,new Department(104,"运营部")));
       employees.put(1005,new Employee(1005,"EE","E4536251@qq.com",1,new Department(105,"后勤部")));

   }
   //增加员工   的实现
   private static Integer initId = 1006;
   public void Save(Employee employee){
       if (employee.getId()==null){
           employee.setId(initId++);
       }
       employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
       employees.put(employee.getId(),employee);
   }
   //查询全部的员工
   public Collection getAll(){
    return employees.values();
   }
//通过id查询员工
   public Employee getEmployeeById(Integer id){

       return employees.get(id);
   }
//通过id删除员工
    public void delete(Integer id){
       employees.remove(id);

    }

}

这样就模拟好了数据库的实现,还有增删改查Template。

连接数据库:

基础配置:

 application.yaml

spring:
  datasource:
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT

package com.gao;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;

@SpringBootTest
class Spring03DataApplicationTests {

    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() {
        System.out.println(dataSource.getClass());
    }

}

JdbcTemplate主要提供以下几类方法:

  • execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

  • update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;

  • query方法及queryForXXX方法:用于执行查询相关语句;

  • call方法:用于执行存储过程、函数相关语句。

package com.gao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/list")
    public List> userLIst(){
        String sql = "select * from user";
        List> list_maps = jdbcTemplate.queryForList(sql);
        return list_maps;
    }
    @GetMapping("/addUser")
    public String addUser(){
        String sql = "insert into user(id,name,pwd) values (5,'就这',456678)";
        jdbcTemplate.update(sql);
        return "成功";
    }
    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") int id){
        String sql = "update user set name=?,pwd=? where id =" +id;
        Object[] object = new Object[2];
        object[0] = "杰杰";
        object[1] = 435567;
        jdbcTemplate.update(sql,object);
        return "ok";
    }
    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") int id){
        String sql = "delete  from user where id=?";
        jdbcTemplate.update(sql,id);
        return "delete ok";
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/299118.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号