lundi 13 avril 2015

How to connect Spring with MySQL database?

I have a simple project, based on this guide. I created a simple REST interface and I want it to use my database. I added Hibernate to the dependencies and created the DAO class. I'm using Spring Tool-Suite for IDE. As far as I understand I should add some beans to tell the classes what to use but I don't understand how. Here are my classes.


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();
}
}


And here is the file structure:


enter image description here


I understand that I should tell my program the database name and the columns using beans but I don't understand how. How can I link the java code to the beans? Sorry I pasted so much code, I just wanted to make sure you have everything needed. Thank you in advance!


Aucun commentaire:

Enregistrer un commentaire