没有创建任何表,RestController URL 也不起作用



请告诉我做错了什么,代码正在运行没有错误,但我没有得到任何表或工作 restcontroller url

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.usere.entity.UserEntity;
@RestController
@RequestMapping("/usr")
public class Ucontroller {

@RequestMapping("/showall")
public UserEntity showall()
{
return new UserEntity("abc",29);
}
}

POJO或休息模型

import javax.persistence.*;
@Entity
@Table(name = "usrtbl")
public class UserEntity {

@Id
@GeneratedValue
private int uid;

@Column(name = "name")
private String usrname;
@Column(name = "age")
private int age;
public UserEntity(String string, int i) {
// TODO Auto-generated constructor stub
usrname=string;
age=i;
}
//getter setters omitted
}

自动生成的类 @SpringBootApplication 公共类 UserEnittyApplication {

public static void main(String[] args) {
SpringApplication.run(UserEnittyApplication.class, args);
}
}

servlet 初始值设定项(自动生成) public class ServletInitializer extensions SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application) {
return application.sources(UserEnittyApplication.class);
}
}

Pom.xml文件

<?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.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.user</groupId>
<artifactId>UserEnitty</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>UserEnitty</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

----------------------------EOF------------------

应用程序属性

server.port=7777
spring.datasource.url = jdbc:mysql://localhost:3306/poncho
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver


spring.jpa.properties.hibernate.dialect =         
org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true

spring.jpa.hibernate.ddl-auto = update
spring.mvc.view.prefix=/view/    // I tried using a simple controller 
spring.mvc.view.suffix=.jsp      // it didn't work too. 

安慰

2019-08-25 10:41:06.534  INFO 9608 --- [           main] com.usere.demo.UserEnittyApplication     : No active profile set, falling back to default profiles: default
2019-08-25 10:41:08.997  INFO 9608 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-08-25 10:41:09.055  INFO 9608 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 37ms. Found 0 repository interfaces.
2019-08-25 10:41:09.925  INFO 9608 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d899bdb3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-08-25 10:41:10.761  INFO 9608 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7777 (http)
2019-08-25 10:41:10.840  INFO 9608 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-25 10:41:10.841  INFO 9608 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-08-25 10:41:11.205  INFO 9608 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-08-25 10:41:11.206  INFO 9608 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4551 ms
2019-08-25 10:41:11.653  INFO 9608 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-08-25 10:41:12.147  INFO 9608 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-08-25 10:41:12.328  INFO 9608 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-08-25 10:41:12.533  INFO 9608 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.10.Final}
2019-08-25 10:41:12.537  INFO 9608 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-08-25 10:41:12.938  INFO 9608 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-08-25 10:41:13.309  INFO 9608 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-08-25 10:41:13.874  INFO 9608 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-08-25 10:41:14.769  INFO 9608 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-25 10:41:14.954  WARN 9608 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-08-25 10:41:15.610  INFO 9608 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7777 (http) with context path ''
2019-08-25 10:41:15.618  INFO 9608 --- [           main] com.usere.demo.UserEnittyApplication     : Started UserEnittyApplication in 9.922 seconds (JVM running for 13.859)```

创建一个存储库类并调用它,例如UserEntityRepository如下所示:

public interface UserEntityRepository extends JpaRepository<UserEntity, Integer> {
}

现在创建一个服务类并像这样调用它UserEntityService

public interface PlayerService {
List<UserEntity> findAll();
Player findById(int theId);
void save(UserEntity theUserEntity);
void deleteById(int theId);    
}

您不一定需要所有这些方法,但这些是常用的 crud 方法。

然后,您需要创建一个实现服务类的类,例如:

@Service
public class UserEntityServiceImpl implements UserEntityService {
private UserEntityRepository userEntityRepository;
@Autowired
public CompanyBlueprintServiceImpl(UserEntityRepository theUserEntityRepository ) {
userEntityRepository= theUserEntityRepository ;
}
...
@Override
public void save(UserEntity theUserEntity) {
userEntityRepository.save(theUserEntity);
}
}

完成所有这些操作后,您必须在控制器类中使用UserEntityService。因此,您可以从服务调用save()方法,以将实体保存在数据库中。

@RestController
@RequestMapping("/api")
public class Ucontroller {
private UserEntityService userEntityService;
public Ucontroller(UserEntityService theUserEntityService) {
userEntityService = theUserEntityService;
}
@RequestMapping("/showall")
public UserEntity showall()
{
return new UserEntity("abc",29);
}
@PostMapping("/users")
public UserEntity addUser(@RequestBody UserEntity theUserEntity) {
// just in case an id in JSON was pass ... set id to 0
// this is to force a save of new item ... instead of update
theUserEntity.setUid(0);
userEntityService.save(theUserEntity);
return theCompany;
}
}

您所做的只是创建一个UserEntity对象并将其返回到控制器方法showAll()中。 保存用户实体后,您应该会在数据库中看到该行。

很抱歉打扰,但我犯了一个很大的错误,项目包命名不正确在此处输入图像描述

你看到默认生成的包是com.usere.demo,早些时候我将其他包命名为com.usere.controller/entity,这是错误的,我们不应该这样做,我认为发布包名称可能并不重要,说正确的名称应该是com.usere.demo。[任何你想要的],感谢康斯坦丁啤酒和BaDr Amer立即提供帮助并讲述正确的惯例和工作流程,但我犯了一个非常愚蠢的错误。

最新更新