我有一个基于this guide的简单项目。我创建了一个简单的REST接口,我想让它使用我的数据库。我将Hibernate添加到依赖项并创建了DAO类。我使用Spring Tool-Suite for IDE。据我所知,我应该添加一些豆子来告诉班级使用什么,但我不明白怎么做。这是我的课程。
Application.java
package com.learnspring.projectfirst;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Marker.java
package com.learnspring.projectfirst;
@Entity
public class Marker {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column
private double longitude;
@Column
private double latitude;
@Column
private String address;
public Marker() {
// Empty constructor
}
public Marker(long id, double longitude, double latitude, String address) {
this.id = id;
this.longitude = longitude;
this.latitude = latitude;
this.address = address;
}
//Getters and Setters
}
MarkerController.java
package com.learnspring.projectfirst.controller;
@Controller
public class MarkerController {
private Logger logger = Logger.getLogger(MarkerController.class.getName());
@Autowired
private MarkerServiceImplementation markerService;
@RequestMapping(value="/markers", method=RequestMethod.GET)
public @ResponseBody List<Marker> getMarkers(@RequestParam(value="city", defaultValue="") String city) {
return this.markerService.getAllMarkers();
}
@RequestMapping(value="/markers/new", method=RequestMethod.POST)
public @ResponseBody Marker addMarker(@RequestBody Marker marker) {
this.markerService.addMarker(marker);
return marker;
}
}
MarkerDaoImplementation.java
package com.learnspring.projectfirst.dao;
@Repository
public class MarkerDaoImplementation implements MarkerDaoInterface {
@Autowired
private SessionFactory sessionFactory;
@Override
public void addMarker(Marker marker) {
this.sessionFactory.getCurrentSession().save(marker);
}
@Override
public void deleteMarker(int markerId) {
this.sessionFactory.getCurrentSession().delete(this.getMarker(markerId));
}
@Override
public Marker getMarker(int markerId) {
return (Marker) this.sessionFactory.getCurrentSession().get(Marker.class, markerId);
}
@Override
public List<Marker> getAllMarkers() {
return this.sessionFactory.getCurrentSession().createQuery("from Marker").list();
}
}
MarkerServiceImplementation.java
package com.learnspring.projectfirst.service;
@Service
public class MarkerServiceImplementation implements MarkerServiceInterface {
@Autowired
private MarkerDaoImplementation markerDao;
@Transactional
public void addMarker(Marker marker) {
this.markerDao.addMarker(marker);
}
@Transactional
public void deleteMarker(int markerId) {
this.markerDao.deleteMarker(markerId);
}
@Transactional
public Marker getMarker(int markerId) {
return this.markerDao.getMarker(markerId);
}
@Transactional
public List<Marker> getAllMarkers() {
return this.markerDao.getAllMarkers();
}
}
这是文件结构:
我知道我应该告诉我的程序数据库名称和使用bean的列,但我不明白如何。如何将java代码链接到bean?对不起,我粘贴了很多代码,我只是想确保你拥有所需的一切。提前谢谢!
答案 0 :(得分:1)
这是您需要的:Spring Boot with MySQL
答案 1 :(得分:0)
答案 2 :(得分:0)
“Marker”类中的注释确定MySQL表和列名(基于类和类变量名)。表名为“marker”,列为“id”,“longitude”,“latitude”,“address”。
您忘记了代码中最重要的部分:弹簧配置。它决定了在注入DAO类之前如何初始化SessionFactory实例。在这里,您必须设置与MySQL服务器的适当连接(例如,通过JNDI资源)