我正在尝试开发一个跨平台的应用程序,前端使用Cordova并在back_end中使用Spring。 基本上,我从数据库和生成类(模型)之间的链接开始。这是我的应用程序属性:
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = updateUnable to find column with logical name: program_id
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
# Server
server.port=8080
endpoints.cors.allowed-origins=*
如果我在进入后端之前以JSON格式获取列表,我想尝试一下。所以我做了一个例子来获取运算符列表,类的运算符被声明为:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest.model;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author gate11
*/
@Entity
@Table(catalog = "db_suivi", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Operator.findAll", query = "SELECT o FROM Operator o")
, @NamedQuery(name = "Operator.findById", query = "SELECT o FROM Operator o WHERE o.id = :id")
, @NamedQuery(name = "Operator.findByOperatorFName", query = "SELECT o FROM Operator o WHERE o.operatorFName = :operatorFName")
, @NamedQuery(name = "Operator.findByOperatorLName", query = "SELECT o FROM Operator o WHERE o.operatorLName = :operatorLName")})
public class Operator implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
private Long id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
private String operatorFName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
private String operatorLName;
@JoinTable(name = "BadgeOperator", joinColumns = {
@JoinColumn(name = "idOp", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "idBad", referencedColumnName = "id")})
@ManyToMany(fetch = FetchType.LAZY)
private List<BadgePoint> badgePointList;
@ManyToMany(mappedBy = "operatorList", fetch = FetchType.LAZY)
private List<Zone> zoneList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "operator", fetch = FetchType.LAZY)
private List<TimePass> timePassList;
public Operator() {
}
public Operator(Long id) {
this.id = id;
}
public Operator(Long id, String operatorFName, String operatorLName) {
this.id = id;
this.operatorFName = operatorFName;
this.operatorLName = operatorLName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOperatorFName() {
return operatorFName;
}
public void setOperatorFName(String operatorFName) {
this.operatorFName = operatorFName;
}
public String getOperatorLName() {
return operatorLName;
}
public void setOperatorLName(String operatorLName) {
this.operatorLName = operatorLName;
}
@XmlTransient
public List<BadgePoint> getBadgePointList() {
return badgePointList;
}
public void setBadgePointList(List<BadgePoint> badgePointList) {
this.badgePointList = badgePointList;
}
@XmlTransient
public List<Zone> getZoneList() {
return zoneList;
}
public void setZoneList(List<Zone> zoneList) {
this.zoneList = zoneList;
}
@XmlTransient
public List<TimePass> getTimePassList() {
return timePassList;
}
public void setTimePassList(List<TimePass> timePassList) {
this.timePassList = timePassList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Operator)) {
return false;
}
Operator other = (Operator) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.rest.Operator[ id=" + id + " ]";
}
}
我添加了一个实现JpaRepository的接口(存储库):
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest.repository;
import com.rest.model.Operator;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
/**
*
* @author gate11
*/
public interface OperatorRepository extends JpaRepository<Operator, Long>{
@Override
List<Operator> findAll();
}
And as needed, also a controller for the class Operator:
package com.rest.rest;
import com.rest.model.Operator;
import com.rest.service.OperatorService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE)
public class OperatorController {
@Autowired
private OperatorService operatorService;
@CrossOrigin
@RequestMapping(method = GET, value = "/operator/all")
public List<Operator> loadAll() {
return this.operatorService.findAll();
}
}
接口服务:
package com.rest.service;
import com.rest.model.Operator;
import java.util.List;
/**
*
* @author gate11
*/
public interface OperatorService {
List<Operator> findAll();
}
服务实施还:
package com.rest.serviceImp;
import com.rest.model.Operator;
import com.rest.repository.OperatorRepository;
import com.rest.service.OperatorService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author gate11
*/
@Service
public class OperatorServiceImp implements OperatorService{
@Autowired
private OperatorRepository operatorRepository;
@Override
public List<Operator> findAll() {
List<Operator> operator = operatorRepository.findAll();
return operator;
}
}
当我启动应用程序时,日志:
2018-03-05 14:34:47.427 INFO 10768 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/css/**'], Ant [pattern='/js/**'], Ant [pattern='/images/**'], Ant [pattern='/webjars/**'], Ant [pattern='/**/favicon.ico'], Ant [pattern='/error']]], []
2018-03-05 14:34:47.565 INFO 10768 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/h2-console/**'], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@183bb809, org.springframework.security.web.context.SecurityContextPersistenceFilter@593fdbbe, org.springframework.security.web.header.HeaderWriterFilter@2d784ff4, org.springframework.security.web.authentication.logout.LogoutFilter@55ef8d65, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@3ba7a547, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@12e1efcb, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@57449117, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5cba96fc, org.springframework.security.web.session.SessionManagementFilter@5403d62a, org.springframework.security.web.access.ExceptionTranslationFilter@4817a878, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@36126980]
2018-03-05 14:34:47.573 INFO 10768 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/**']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@70c855ab, org.springframework.security.web.context.SecurityContextPersistenceFilter@1eef0aa6, org.springframework.security.web.header.HeaderWriterFilter@537d0675, org.springframework.security.web.authentication.logout.LogoutFilter@7d1011c1, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@1b4c8eff, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@73c2d36e, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@48372dcd, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3cfcbfa3, org.springframework.security.web.session.SessionManagementFilter@4befcfad, org.springframework.security.web.access.ExceptionTranslationFilter@4ed4729f, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@dffb1e0]
2018-03-05 14:34:47.807 INFO 10768 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2018-03-05 14:34:47.900 INFO 10768 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-03-05 14:34:48.000 INFO 10768 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-03-05 14:34:48.008 INFO 10768 --- [ restartedMain] com.rest.Application : Started Application in 10.365 seconds (JVM running for 11.067)
2018-03-05 14:42:28.257 INFO 10768 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-03-05 14:42:28.257 INFO 10768 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-03-05 14:42:28.279 INFO 10768 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 21 ms
然后,我应该去localhost:8080 / api / operator / all来获取运行列表,就像我以前那样,但它要求我使用用户名密码,我尝试了db用户的所有内容(root,root)但不起作用。 我甚至在应用程序中都没有连接页面。
如果您有任何想法,请随时提出建议,我正如您所见,使用Mac。 感谢。