spring boot integrates spring Data JPA and freemarker

1. Introduction to spring Data JPA

Is an alternative to hibernate a framework for the role of the database.

2. Integration

1. Import dependency

  

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>

2. Create pojo object

3. Create dao layer

package com.offcn.dao;

import com.offcn.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserDao extends JpaRepository<User,Integer> {
}

There is no need to write some simple sql statements after integration.

4. Configure scan path in configuration class

package com.offcn;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.offcn.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
}

5. Database information is required in the application.properties configuration file

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=123

6. freemarker can be used for later echo data

1. Import dependency

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

2. Create templates folder under resources, under which you can write pages, similar to HTML style.

  

<html>
    <head>
        <title>user surface</title>
    </head>
    <body>
        <table border="1px">
            <thead>
                <tr>
                    <td>id</td>
                    <td>User name</td>
                    <td>Password</td>
                </tr>
            </thead>
            <tbody>
                <#list userList as user>
                    <tr>
                        <td>${user.uid}</td>
                        <td>${user.uname}</td>
                        <td>${user.pwd}</td>
                    </tr>
                </#list>

            </tbody>
        </table>
    </body>
</html>

Tags: Java Spring MySQL Database JDBC

Posted on Tue, 05 Nov 2019 08:35:35 -0800 by soniared2002