Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

Metronic Spring Starterkit with MySQL


Here are instructions on how you can get started with MySQL in Metronic SpringBoot starterkit.


  1. Install MySQL on your machine.

  2. You can use the default root and no password user to login to your MySQL console by running mysql -u root -p

  3. Login to MySQL console and create your database and tables.

  4. In file src/main/resources/application.properties add database properties.

    spring.datasource.url=jdbc:mysql://localhost:3306/db-name
    spring.datasource.username=your-user-name
    spring.datasource.password=your-passowrd
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    </pre
    >
    Make sure to change db-name with your database name and update your username and password.</li>
    <li>Add dependencies below to <strong>starterkit/pom.xml</strong>.
    <pre lang="xml">
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>


  5. Execute mvn clean install.

  6. This is all configuration you need to do to get started with database.

  7. Now you can add your entity, repository, and controller classes to display your database data in markup.

    Entity examples:

    package com.theme.starterkit.models;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;

    @Entity
    public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    public String first_name;
    public String last_name;
    }


    Repository example:

    package com.theme.starterkit.repository;

    import com.theme.starterkit.models.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;

    @Repository
    public interface UsersRepository extends JpaRepository<User, Long> {
    User findById(long id);
    }
    Controller example.
    package com.theme.starterkit.controller;

    import com.theme.starterkit.repository.UsersRepository;
    import com.theme.starterkit.models.Users;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;

    import com.theme.starterkit.libs.KTTheme;
    import com.theme.starterkit.libs.config.KTThemeBaseConfig;

    @Controller
    public class UserController {
    @Autowired
    private KTTheme theme;

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable long id, Model model) {
    User user = usersRepository.findById(id);
    model.addAttribute("user", user);
    return theme.getPageView("user", "user-details");
    }
    }


    Then you can add user-detail.html inside resources/templates/pages/user with the following code.

    <div layout:fragment="content">
    <h1>User Details</h1>
    <div th:if="${user}">
    <p>User ID: <span th:text="${user.id}" /></p>
    <p>Name: <span th:text="${user.first_name}" /></p>
    <p>Email: <span th:text="${user.last_name}" /></p>
    </div>
    <div th:unless="${user}">
    <p>User not found</p>
    </div>
    </div>



Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (0)