Spring Boot 入门集成小实例 无web
程序员文章站
2022-04-15 14:45:27
...
第一步 新建MySQL数据库
第二步 新建springBoot项目
第三步 配置pom.xml
<dependencies>
<!-- JpaRepository -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- QuerydslPredicateExecutor -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<!-- getter setter -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 用以映射实体类 自动生成表 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.5.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 用以生成Quser的插件 -->
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
第四步 新建包类接口 文件目录如下
第五步 配置application.properties
第六步 开始写代码了
SuperEntity
@MappedSuperclass
@Getter
@Setter
public class SuperEntity implements Serializable{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid",strategy = "uuid")
@Column(length = 64)
private String id;
@Column(nullable = true)
private Date createTime;
@Version
private Date version;
}
User
@Entity
@Setter
@Getter
public class User extends SuperEntity{
private String name;
private int age;
}
BaseRepository
@NoRepositoryBean
public interface BaseRepository<T> extends JpaRepository<T,String>,QuerydslPredicateExecutor<T> {
}
UserRepository
public interface UserRepository extends BaseRepository<User> {
List<User> findByAgeIn(String[] ageArr);
}
BaseService
public class BaseService<T> {
@Autowired
protected BaseRepository<T> baseRepository;
public Iterable<T> findAll(Predicate predicate){
return baseRepository.findAll(predicate);
}
public Page<T> findAll(Predicate predicate, Pageable pageable){
return baseRepository.findAll(predicate, pageable);
}
public List<T> findAll(){
return baseRepository.findAll();
}
public <S extends T> S save(S entity){
return baseRepository.save(entity);
}
public boolean exists(Predicate predicate){
return baseRepository.exists(predicate);
}
}
UserService
@Service
@Transactional
public class UserService extends BaseService<User>{
}
ViewDataPage
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ViewDataPage {
private Object rows;
private long records;
private int page;
private int total;
public static ViewDataPage of(Page page){
return new ViewDataPage(page.getContent(),page.getTotalElements(),page.getNumber()+1,page.getTotalPages());
}
}
UserController
@RestController
@RequestMapping("/user")
@Transactional
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public Object findAllList(@QuerydslPredicate(root = User.class)Predicate predicate, String s, Pageable
pageable){
return ViewDataPage.of(userService.findAll(predicate, pageable));
}
}
注意事项
若是lombok报错 则要安装该插件
最后一定要记得运行前在该项目命令行下 执行mvn clean compile
在target下生成Quser文件 项目才能成功运行
项目最后结果
记得自己在数据库中添加数据 在刷新网页下就好了
Navicat Premium 12 这个是真的好用 推荐一下
以上,是不是很简单,Spring Boot真是太强大了,谁用谁喜欢。
上一篇: Linq语句查询数据