首先pom文件
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.alibaba</groupId> <artifactId>springboot2xdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot2xdemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl-framework-starter</artifactId> <version>1.1.15.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
其次是当前文件夹所有相关文件
首先是HomeController的代码
package com.alibaba.springboot2xdemo.controller; import com.alibaba.springboot2xdemo.models.Users; import com.alibaba.springboot2xdemo.repository.UsersRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @SpringBootApplication @RestController @RequestMapping("/home") public class HomeController { @Autowired private UsersRepository usersRepository ; @RequestMapping("/getAll") public List<Users> getAll(){ List<Users> listUsers = (List<Users>) usersRepository.findAll(); return listUsers; } }
其次是Users的代码
package com.alibaba.springboot2xdemo.models; import javax.persistence.*; @Entity @Table(name="users") public class Users { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name="name") private String name; @Column(name="password") private String password; @Column(name="age") private int age; @Column(name="city") private String city; @Column(name="address") private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
接着是UsersRepository的代码
package com.alibaba.springboot2xdemo.repository; import com.alibaba.springboot2xdemo.models.Users; import org.beetl.sql.core.annotatoin.Param; import org.springframework.context.annotation.Bean; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; import java.util.List; public interface UsersRepository extends CrudRepository<Users,Integer> { @Query("select u from Users u where u.name=?1") public List<Users> getUsersByName(@Param("name") String name); @Query("select u from Users u where u.name=?1 and u.password=?2") public List<Users> getUsersByNameAndPassword(@Param("name") String name,@Param("password") String password); @Query("select u from Users u where u.id=?1") public Users getUsersById(int id); }
然后是Springboot2xdemoApplication.java的代码
package com.alibaba.springboot2xdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Springboot2xdemoApplication { public static void main(String[] args) { SpringApplication.run(Springboot2xdemoApplication.class, args); } }
最后是配置文件
spring.application.name=springBoot2xDemo server.port=8088 spring.datasource.url=jdbc:mysql://lukairui.rwlb.rds.aliyuncs.com/test_db1?useSSL=false spring.datasource.username=flink_admin spring.datasource.password=xxxxx spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
-----------附件是数据库的建表语句-------------------
/* Navicat Premium Data Transfer Source Server : lukairui.rwlb.rds.aliyuncs.com Source Server Type : MySQL Source Server Version : 80013 Source Host : lukairui.rwlb.rds.aliyuncs.com:3306 Source Schema : test_db1 Target Server Type : MySQL Target Server Version : 80013 File Encoding : 65001 Date: 16/03/2021 18:35:24 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for users -- ---------------------------- DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `city` varchar(255) DEFAULT NULL, `address` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; -- ---------------------------- -- Records of users -- ---------------------------- BEGIN; INSERT INTO `users` VALUES (1, 'lukairui', 'ksdf', 11, 'hangzhou', 'adhfu'); INSERT INTO `users` VALUES (2, 'tianlu', 'difur', 23, 'beijing', 'adir'); INSERT INTO `users` VALUES (3, 'lsdfk', 'iru', 11, 'peking', 'difur'); COMMIT; SET FOREIGN_KEY_CHECKS = 1;