Java持久层和控制器的封装,javassm框架教程

本文介绍了如何在SpringBoot项目中进行持久层和控制器的封装,包括使用反射工具类获取实体类,创建通用的CommonRepository接口、CommonService类以及CommonController。还展示了如何实现分页查询、动态条件查询,并提供了相关的工具类如TablePageable和AjaxResult。
摘要由CSDN通过智能技术生成
<?xml version="1.0" encoding="UTF-8"?>

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.2.RELEASE

com.mcy

springboot-demo

0.0.1-SNAPSHOT

springboot-demo

Demo project for Spring Boot

<java.version>1.8</java.version>

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-devtools

runtime

true

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

在application.properties文件中配置数据库的链接。

spring.datasource.url=jdbc:mysql://localhost:3306/springbootdemo?serverTimezone=GMT%2B8

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.database-platform=org.hibernat

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

e.dialect.MySQL5InnoDBDialect

spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update

spring.jpa.hibernate.use-new-id-generator-mappings=false

spring.servlet.multipart.max-request-size=2048MB

spring.servlet.multipart.max-file-size=2048MB

server.port=8080

在写代码之前,我们先来看一下项目的目录结构。

代码实现

对持久层的封装,我们首先需要确定的就是找到对应的实体类,之后才能对其进行操作,可以通过反射机制找到当前的实体类。

新建一个反射工具类GenericsUtils,这个类很重要,可以帮助我们找到对应的实体类。

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

public class GenericsUtils {

/**

  • 通过反射,获得定义Class时声明的父类的范型参数的类型. 如public BookManager extends

  • GenricManager

  • @param clazz The class to introspect

  • @return the first generic declaration, or Object.class if cannot be determined

*/

public static Class getSuperClassGenricType(Class clazz) {

return getSuperClassGenricType(clazz, 0);

}

/**

  • 通过反射,获得定义Class时声明的父类的范型参数的类型. 如public BookManager extends GenricManager

  • @param clazz clazz The class to introspect

  • @

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值