Spring connect to Mysql Database simply (Part 1)

Monday, November 30th, 2015 (0 Comment)

In this article, we will develop simple spring project with select, insert, update, delete (crud) mysql database (You need pactice with HelloWorld project part 1 and part 2 before continute). We create basic page as below image:

crud page with mysql

crud page with mysql

1. Install mysql database

You can click here to install mysql with XAMPP, and then you create database name is “springdb” and create ROOM table (you can not create table, config hibernate will auto create table in config below)

script create manual table in mysql:

2. Create spring project by eclipse

You create spring project with struct as follow:

project's struct

project’s struct

  • entity package: mapping object with database’s table
  • dao package: execute sql statements (select, insert, update, delete) into database
  • service package: to calculate business rule if any before call dao package
  • controller package: manage layout, many action in page screen

You need to config maven with porn.xml file

Room.java: room object, mapping with room table in database

  • @Entity: syntax to define object is table
  • @Table(name = “ROOM”): mapping with name table is ROOM
  • @Id: primary key
  • @Column(name = “ID”): mapping with name column is ID
  • @GeneratedValue(strategy = GenerationType.AUTO): key auto gen in mysql database

RoomDAO.java:

  • @Repository: define annotate to use room class with variable @Autowired (exa: @Autowired private RoomDAO roomDAO in RoomService.java file)
  • @Autowired: link to spring bean together

RoomService:java:

  • @Service: define annotate to  checkpoint Service in business class
  • @Transactional: add to config file transaction of spring <tx:annotation-driven/>

RoomController.java:

You create room.jsp page to mange insert/update/delete room info

In index.jsp, modify to redirect to room page

Finally, you modify config files to build and run success project

servlet-config-context.xml:

servlet-config-jdbc.xml: config connect to mysql database

  • username and password: you need modify your information if any
  • hibernate.hbm2ddl.auto have 3 value:
    • create: auto drop all table and auto create tables into mysql database from all class of Entity package
    • update: update add column in database if any change in class Entity
    • none: nothing to do
    • the first run project, should set value is create to auto generate tables in database. After that set value is update or none to don’t lost data so be drop table.

web.xml

Right click on project name and select Build Project.

Publish to ther server and Run with url http://localhost:8080/springmvc

Tags: , , ,

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

Leave a Reply

Your email address will not be published. Required fields are marked *