android studio实现员工管理系统_SpringBoot员工管理系统(附完整代码及实现教程)...

本博客详细介绍了如何使用SpringBoot和Android Studio搭建一个员工管理系统,包括环境搭建、首页实现、页面国际化、登录功能、拦截器、展示员工信息、增加、修改和删除功能,以及404页面定制和注销操作。项目提供了完整的代码和实现教程。
摘要由CSDN通过智能技术生成

01862879424cb9cbdf0d213a132ab7d5.png

本员工管理系统基于狂神老师的SpringBoot教程:https://www.bilibili.com/video/BV1PE411i7CV?p=20

项目成效图

c850cad4ebc7bde1201a4f239159a720.png

(一)环境搭建

1. 新建一个SpringBoot项目

c8fe6a949a51bdf75d1dd29a3a1caa76.png

2b1e0d941374560f55b346590771348f.png

选择配件时勾选SpringWebThymeleaf

fee845102c88e1f7fe603e5c03dd60f6.png

点击next,然后finish创建完成即可

2. 导入静态资源

首先创建不存在的 静态资源目录 publicresources

6050ca34b184a412534add4ad184e926.png

html静态资源放置templates目录下

d4555e9b313979f8abe3c8a1de4138c1.png

asserts目录下的cssimgjs等静态资源放置static目录下

596c5dcd4b9c037208457231c56e47b2.png

3. 模拟数据库

1. 创建数据库实体类

在主程序同级目录下新建 pojo包,用来存放实体类
pojo包下创建一个部门表 Department和一个员工表 Employee

eae6a6f75809dbb358b5f399e59e6874.png

为了方便,我们导入lombok

<dependency>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
</dependency>

部门表

package com.zsr.pojo;

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

//部门表
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {
    
    private Integer id;
    private String departmentName;
}

员工表

package com.zsr.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;

//员工表
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
    
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender;//0:女 1:男
    private Department department;
    private Date date;
}

2. 编写dao层(模拟数据)

在主程序同级目录下新建 dao
然后分别编写 DepartmentDaoEmployeeDao,并在其中模拟数据库的数据

1e021a322075cc0918c24b1b7e4b0827.png

DepartmentDao

package com.zsr.dao;

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

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//注册到IOC容器中
@Repository
public class DepartmentDao {
    
    //模拟数据库中的数据
    private static Map<Integer, Department> departments = null;

    static {
    
        departments = new HashMap<>();//创建一个部门表
        departments.put(1, new Department(1, "技术部"));
        departments.put(2, new Department(2, "市场部"));
        departments.put(3, new Department(3, "调研部"));
        departments.put(4, new Department(4, "后勤部"));
        departments.put(5, new Department(5, "运营部"));
    }

    //获得部门的所有信息
    public Collection<Department> departments() {
    
        return departments.values();
    }

    //通过id得到部门
    public Department getDepartmentById(int id) {
    
        return departments.get(id);
    }
}

EmployeeDao:

package com.zsr.dao;

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

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//注册到IOC容器中
@Repository
public class EmployeeDao {
    
    //模拟数据库中员工表的数据
    static private Map<Integer, Employee> employees;
    
    @Autowired//自动
    private DepartmentDao departmentDao;

    static {
    
        employees = new HashMap<>();//创建一个员工表
        employees.put(1, new Employee(1, "zsr", "1234@qq.com", 1, new Department(1, "技术部"), new Date()));
        employees.put(2, new Employee(2, "lyr", "1345@qq.com", 1, new Department(2, "市场部"), new Date()));
        employees.put(3, new Employee(3, "gcc", "5665@qq.com", 0, new Department(3, "调研部"), new Date()));
        employees.put(4, new Employee(4, "zyx", "7688@qq.com", 1, new Department(4, "后勤部"), new Date()));
        employees.put(5, new Employee(5, "zch", "8089@qq.com", 1, new Department(5, "运营部"), new Date()));
    }

    //主键自增
    private static Integer initialID = 6;

    //增加一个员工
    public void addEmployee(Employee employee) {
    
        if (employee.getId() == null)
            employee.setId(initialID);
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    //查询全部员工信息
    public Collection<Employee> getAllEmployees() {
    
        return employees.values();
    }

    //通过id查询员工
    public Employee getEmployeeByID(Integer id) {
    
        return employees.get(id);
    }

    //通过id删除员工
    public void deleteEmployeeByID(int id) {
    
        employees.remove(id);
    }
}

(二)首页实现

在主程序同级目录下新建 config包用来存放自己的配置类
在其中新建一个自己的配置类 MyMvcConfig,进行视图跳转

6d3ba9d0c4156caec3a4f660444ed2d4.png
package com.zsr.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }
}

我们启动主程序访问测试一下,访问localhost:8080/或者locahost:8080/index.html

出现以下页面则成功

b768621a5ecee7f3ee7625d1ab8aa476.png
上述测试可以看到页面有图片没有加载出来,且没有css和js的样式,这就是因为我们 html页面中静态资源引入的语法出了问题,在SpringBoot中,推荐使用 Thymeleaf作为模板引擎,我们将其中的语法改为 Thymeleaf,所有页面的静态资源都需要使用其接管

注意所有html都需要引入Thymeleaf命名空间

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

然后修改所有页面静态资源的引入,使用@{...} 链接表达式

例如index.html中:

注意:第一个/代表项目的classpath,也就是这里的resources目录

b8086c668e73494eefdd6567b27c09c7.png

其他页面亦是如此,再次测试访问,正确显示页面

d5c4429023bc84634eae579f62949428.png

(三)页面国际化

1. 统一properties编码

首先在IDEA中统一设置properties的编码为 UTF-8

05db3b3063e6b5348a226da06ca3a5c6.png

2. 编写i18n国际化资源文件

resources目录下新建一个 i18n包,其中放置国际化相关的配置

5710c35b3f6d96cd96f9fa4c6568eed9.png

其中新建三个配置文件,用来配置语言:

  • login.properties:无语言配置时候生效
  • login_en_US.properties:英文生效
  • login_zh_CN.properties:中文生效

命名方式是下划线的组合:文件名_语言_国家.properties;

以此方式命名,IDEA会帮我们识别这是个国际化配置包,自动绑定在一起转换成如下的模式:

0fc625cf1e3c2bfaca4a6d2c04f700f7.png

绑定在一起后,我们想要添加更过语言配置,只需要在大的资源包右键添加到该绑定配置文件即可

a36235a11ea3e7cdd4cf1c589cc8c460.png

此时只需要输入区域名即可创建成功,比如输入en_US,就会自动识别

35a0e1cdac31ed1b6a5fb0ce2255c76e.png

然后打开英文或者中文语言的配置文件,点击Resource Bundle进入可视化编辑页面

a5b01e01d53587612cab8cf3583d3879.png

进入到可视化编辑页面后,点击加号,添加属性,首先新建一个login.tip代表首页中的提示

c99c253d5913616c365de542322b54c1.png

然后对该提示分别做三种情况的语言配置,在三个对应的输入框输入即可(注意:IDEA2020.1可能无法保存,建议直接在配置文件中编写

7e390c9b4abfc1ac1a42e66e6eb26fa2.png

接下来再配置所有要转换语言的变量(注意:IDEA2020.1可能无法保存,建议直接在配置文件中编写

37de39613c42409286f868b24c03c570.png

然后打开三个配置文件的查看其中的文本内容,可以看到已经做好了全部的配置

login.properties

login.tip=请登录
login.password=密码
login.remember=记住我
login.btn=登录
login.username=用户名

login_en_US.properties

login.tip=Please sign in
login.password=password
login.remember=remember me
login.btn=login
login.username=username

login_zh_CN.properties

login.tip=请登录
login.password=密码
login.remember=记住我
login.btn=登录
login.username=用户名

3. 配置国际化资源文件名称

在Spring程序中,国际化主要是通过 ResourceBundleMessageSource这个类来实现的
Spring Boot通过 MessageSourceAutoConfiguration为我们自动配置好了管理国际化资源文件的组件

我们在IDEA中查看以下MessageSourceAutoConfiguration

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
public class MessageSourceAutoConfiguration {
    

	private static final Resource[] NO_RESOURCES = {};

	@Bean
	@ConfigurationProperties(prefix = "spring.messages")
	public MessageSourceProperties messageSourceProperties() {
    
		return new MessageSourceProperties();
	}

	@Bean
	public MessageSource messageSource(MessageSourceProperties properties) {
    
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		if (StringUtils.hasText(properties.getBasename())) {
    
			messageSource.setBasenames(StringUtils
					.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
		}
		if (properties.getEncoding() != null) {
    
	
设计一个Spring Boot为基础的Android电子书阅读器系统涉及多个步骤,包括前端应用开发、后端API设计以及数据持久化等。以下是一个简化的概述: 1. **需求分析**:确定功能需求,如用户登录、书籍浏览、搜索、下载、阅读进度跟踪等。 2. **架构设计**: - **前端**: 使用Android Studio创建一个原生应用程序,使用MVP(Model-View-Presenter)架构模式或MVVM(Model-View-ViewModel)。 - **后端**: Spring Boot提供RESTful API,处理用户的请求并操作数据库。 3. **Spring Boot设置**: - 创建Spring Boot项目,并添加Spring Web、Spring Data JPA等依赖。 - 配置Spring Security用于身份验证和授权。 4. **数据模型设计**:定义Book、Chapter、Page等实体类,映射到数据库表。 5. **API设计**: - 设计CRUD操作相关的API,如获取图书列表、创建新书、更新章节信息等。 6. **数据库访问**: - 数据库选择,例如MySQL或MongoDB,通过JPA或Repository接口进行操作。 7. **服务层**:编写业务逻辑,处理复杂的查询和事务管理。 8. **前端界面**:利用RecyclerView展示图书列表,WebView加载书籍内容,保存用户的阅读位置等。 9. **集成测试**:编写单元测试和集成测试,保证各部分功能正常。 10. **部署**:将Spring Boot应用部署到服务器,如Docker容器或云平台。 以下是关键代码片段示例(简化版): ```java // Model (实体) public class Book { @Id Long id; String title; List<Chapter> chapters; } // Repository (接口) public interface BookRepository extends JpaRepository<Book, Long> { List<Book> findByTitle(String title); } // Controller (API) @RestController @RequestMapping("/api/books") public class BookController { private final BookRepository bookRepository; public BookController(BookRepository bookRepository) { this.bookRepository = bookRepository; } @GetMapping("/{title}") public ResponseEntity<Book> getBook(@PathVariable("title") String title) { Book book = bookRepository.findByTitle(title).orElseThrow(() -> new ResourceNotFoundException()); return ResponseEntity.ok(book); } } // Android端代码示例: // Activity or Fragment private void loadBook(String title) { Retrofit retrofit = ... // 创建Retrofit客户端 Call<Book> call = retrofit.create(BookApi.class).getBook(title); call.enqueue(new Callback<Book>() { @Override public void onResponse(Call<Book> call, Response<Book> response) { if (!response.isSuccessful()) { // Handle error } else { // Load book into the WebView webView.loadData(response.body().getContent(), "text/html", null); } } @Override public void onFailure(Call<Book> call, Throwable t) { // Handle network failure } }); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值