In step 5, we will be defining how the datasource will be loaded and
how our application connects to MySQL. The above snippet is the bare
minimum configuration we need to connect to MySQL. Here we provide two
beans:
****LocalContainerEntityManagerFactoryBean****
This gives us an handle over the EntityManagerFactory configurations and allows us to do customizations. It also allows us to inject the PersistenceContext in our components as below:
@PersistenceContextprivateEntityManager em;
****DataSource****
Here we return an instance of the `DriverManagerDataSource`. It is a simple implementation of the standard JDBC DataSource interface, configuring a plain old JDBC Driver via bean properties, and returning a new Connection for every getConnection call. Note that I recommend to use this strictly for testing purposes as there are better alternatives like `BasicDataSource` available. Refer [here](https://mytechrepo.wordpress.com/2015/02/17/drivermanagerdatasource-vs-basicdatasource/) for more details
**Create an Application class**
src/main/java/hello/Application.java
@SpringBootApplicationpublicclassApplication{privatestaticfinalLogger log =LoggerFactory.getLogger(Application.class);@AutowiredprivateCustomerRepository repository;publicstaticvoidmain(String[] args){SpringApplication.run(TestCoreApplication.class, args);}@BeanpublicCommandLineRunnerdemo(){return(args)->{// save a couple of customers
repository.save(newCustomer("Jack","Bauer"));
repository.save(newCustomer("Chloe","O'Brian"));
repository.save(newCustomer("Kim","Bauer"));
repository.save(newCustomer("David","Palmer"));
repository.save(newCustomer("Michelle","Dessler"));// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");for(Customer customer : repository.findAll()){
log.info(customer.toString());}
log.info("");// fetch an individual customer by IDCustomer customer = repository.findOne(1L);
log.info("Customer found with findOne(1L):");
log.info("--------------------------------");
log.info(customer.toString());
log.info("");// fetch customers by last name
log.info("Customer found with findByLastName('Bauer'):");
log.info("--------------------------------------------");for(Customer bauer : repository.findByLastName("Bauer")){
log.info(bauer.toString());}
log.info("");};}
}
Running the application
If you are using an IDE like STS, you can simply right click your project -> Run As -> Gradle (STS) Build...
In the tasks list, type bootRun and Run.
If you are using gradle on command line, you can simply run the application as follows:
./gradlew bootRun
You should see something like this:
==Customers found withfindAll():Customer[id=1, firstName='Jack', lastName='Bauer']Customer[id=2, firstName='Chloe', lastName='O'Brian']Customer[id=3, firstName='Kim', lastName='Bauer']Customer[id=4, firstName='David', lastName='Palmer']Customer[id=5, firstName='Michelle', lastName='Dessler']==Customer found withfindOne(1L):Customer[id=1, firstName='Jack', lastName='Bauer']==Customer found withfindByLastName('Bauer'):Customer[id=1, firstName='Jack', lastName='Bauer']Customer[id=3, firstName='Kim', lastName='Bauer']
Remarks
As a pre-requisite, make sure that MySQL is already running on port 3306 and has your database created.