目录
前言:给大家分析下我的Java期末项目在IDEA中写出应用管理系统
技术要求:Springboot +JPA+thymeleaf+MySQL
1. application.properties中写入数据库配置连接
2..controller层AppController类中写入
前言:给大家分析下我的Java期末项目在IDEA中写出应用管理系统
成果展示:
开发过程:
软件:IDEA
技术要求:Springboot +JPA+thymeleaf+MySQL
项目框架:
1. application.properties中写入数据库配置连接
2..controller层AppController类中写入
public class AppController {
@Autowired
private AppService appService;
//首页页面
@GetMapping("/")
public String index(Model model){
return findPaginated(1,"name","asc", model);
}
//新增应用
@GetMapping("/showNewAppForm")
public String showNewAppForm(Model model){
//建模绑定表单数据
App app = new App();
model.addAttribute("app",app);
return "new_app";
}
//新增应用页
@PostMapping("/addApp")
public String addApp(@ModelAttribute("app") App app){
//将应用信息保存到数据库
appService.addApp(app);
return "redirect:/";
}
//更新应用信息
@GetMapping("/updateApp/{id}")
public String updateApp(@PathVariable(value = "id") long id, Model model){
//从AppService中获取应用
App app = appService.getAppById(id);
model.addAttribute("app", app);
return "update_app";
}
//删除应用id的应用信息
@GetMapping("/deleteApp/{id}")
public String deleteApp(@PathVariable(value = "id") long id){
this.appService.deleteAppById(id);
return "redirect:/";
}
//获取分页数据
@GetMapping("/page/{pageNo}")
public String findPaginated(@PathVariable(value = "pageNo") int pageNo, //页数
@RequestParam("sortField") String sortField, //排序的字段
@RequestParam("sortDir") String sortDir, //排序的方式,升序或降序
Model model){
int pageSize = 6; //每页6条数据
Page<App> page = appService.findPaginated(pageNo, pageSize, sortField, sortDir);
List<App> listApp = page.getContent();
model.addAttribute("currentPage", pageNo);
model.addAttribute("totalPages", page.getTotalPages());
model.addAttribute("totalItems", page.getTotalElements());
model.addAttribute("sortField", sortField);
model.addAttribute("sortDir", sortDir);
model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");
model.addAttribute("listApp",listApp);
return "index";
}
@Autowired
private AppRepository appRepository;
//获取所有应用
@GetMapping("/apps")
public String getApp(Model model){
List<App> apps = appRepository.findAll();
model.addAttribute("apps",apps);
return "search";
}
//查询指定的公司
@PostMapping("/apps/search")
public String searchApp(@RequestParam("company") String company, Model model) {
List<App> apps = appRepository.findByCompany(company);
model.addAttribute("apps",apps);
return "search";}
}
2.在mdoel层中写入App类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name = "apps")
public class App {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "应用名")
private String name;
@Column(name = "公司")
private String company;
@Column(name = "创始人")
private String founder;
@Column(name = "应用类型")
private String appType;
}
3.repository层中AppRepository
public interface AppRepository extends JpaRepository<App, Long> {
List<App> findByCompany(String company);
}
4.service层中
AppServicelmpl代码如下:
public class AppServiceImpl implements AppService {
//注入接口
@Autowired
private AppRepository appRepository;
//获取所有应用信息
@Override
public List<App> getAllApp() {
return appRepository.findAll();
}
//新增应用信息
@Override
public void addApp(App app) {
this.appRepository.save(app);
}
//更新应用信息
@Override
public void updateApp(App app) {
appRepository.saveAndFlush(app);
}
//获取指定id的应用信息
@Override
public App getAppById(long id) {
//调用数据访问层查找指定id的应用,返回Optional对象
Optional<App> optional = appRepository.findById(id);
App app = null;
if (optional.isPresent()){ //如果指定的id存在
app = optional.get(); //从Optional对象中获取应用
}else { //否则提示找不到应用
throw new RuntimeException("找不到应用id :" + id);
}
return app;
}
//删除指定id的应用信息
@Override
public void deleteAppById(long id) {
this.appRepository.deleteById(id);
}
//分页
@Override
public Page<App> findPaginated(int pageNo, int pageSize, String sortField, String sortDirection) {
//设置排序参数,升序ASC;降序DESC
Sort sort = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name())
? Sort.by(sortField).ascending()
: Sort.by(sortField).descending();
//根据页号/每页记录数/排序依据返回某指定页面数据
Pageable pageable = PageRequest.of(pageNo - 1,pageSize, sort);
return this.appRepository.findAll(pageable);
}
//获取指定的公司
@Override
public App findByDeveloper(String company) {
return (App) appRepository.findByCompany(company);
}
}
AppService接口代码:
public interface AppService {
//获取所有应用信息
List<App> getAllApp();
//新增应用信息
void addApp(App app);
//更新应用信息
void updateApp(App app);
//获取指定id的应用信息
App getAppById(long id);
//删除指定id的应用信息
void deleteAppById(long id);
//分页
Page<App> findPaginated(int pageNo, int pageSize, String sor