SpringBoot+Oracle+SpringDataJpa+Thymeleaf集成

/ 0 条评论 / 197人阅读

SpringBoot+Thymeleaf+Oracle+SpringDataJpa整合实现查询

环境:
SpringBoot 2.1.7
Oracle驱动:Ojdbc8
Thymeleaf:2.1.6

创建项目

创建项目

其中,lombok:可以为实体类属性生成getter/setter方法,减少代码量(需要在idea安装lombok插件),因为是连接Oracle数据库,所以在创建项目时无法选择Oralce驱动,需要手动导入,加上Ojdbc8在Maven创建搜索不到,可以自行下载jar包,再使用以下命令,部署到本地仓库

mvn install:install-file -Dfile=jar包路径 -DgroupId=groupId -DartifactId=模块名 -Dversion=版本号 -Dpackaging=jar


pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yk</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>12.2.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写application配置文件

因为只是做简单的集成demo,所以配置略微的简单

# 数据库连接
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=crmuser
spring.datasource.password=crmpwd
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

#Jpa配置
#显示SQL语句
spring.jpa.show-sql=true
#显示的SQL语句格式化
spring.jpa.properties.hibernate.format_sql=true

编写Entity,Repository,Service

Entity

package com.yk.demo.entity;
import javax.persistence.Entity;
import javax.persistence.Column;
import lombok.Data;
import javax.persistence.Table;
import javax.persistence.Id;

@Entity
@Data
@Table(name="SYS_USER")
public class SysUser {

  @Id
  @Column(name="USR_ID")
  private String usrId;
  @Column(name="USR_NAME")
  private String usrName;
  @Column(name="USR_PASSWORD")
  private String usrPassword;
  @Column(name="USR_ROLE_ID")
  private String usrRoleId;
  @Column(name="USR_FLAG")
  private String usrFlag;

}

Repository

package com.yk.demo.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.yk.demo.entity.SysUser;

public interface SysUserRepository extends JpaRepository<SysUser, Long>{

}

因为SpringDataJpa是基于hibernate编写的,所以会有自带的增删查改方法,这里只需要继承即可

JpaRepository源码

Service

package com.yk.demo.service;
import com.yk.demo.entity.SysUser;
import java.util.List;

public interface SysUserService{

	 public boolean saveAndUpd(SysUser user);

	 public boolean remove(Long id);

	 public List<SysUser> getAll();

	 public SysUser getById(Long id);

}

Service实现类

package com.yk.demo.service;

import com.yk.demo.entity.SysUser;
import com.yk.demo.dao.SysUserRepository;
import com.yk.demo.service.SysUserService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserRepository userRepository;

    public boolean saveAndUpd(SysUser user) {
        try {
            userRepository.save(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean remove(Long id) {
        try {
            userRepository.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<SysUser> getAll() {
        return userRepository.findAll();
    }

    public SysUser getById(Long id) {
        return userRepository.findById(id).get();
    }

}

Controller

```
package com.yk.demo.controller;
import com.yk.demo.entity.SysUser;
import com.yk.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SysUserController{

	 @Autowired
	 private  SysUserService sysUserService;


	 @RequestMapping("/list.html")
	 public String toList(Model model){
		 List<SysUser> list = sysUserService.getAll();
		 model.addAttribute("list",list);
		 return "list.html";
	 }



}

```

编写页面

  1. 因为Thyemeleaf有个默认的视图解析器,指向的就是这个文件夹下,所以页面往这里面放即可

  2. ThymeleafProperties源码

  3. 编写list.html页面
    在使用th标签之前要先对它进行引入,在标签上,编写一个属性,代码如下:

  4. <html xmlns:th="http://www.thymeleaf.org">

    页面所有代码如下:

  5. <!DOCTYPE html >
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>用户列表</title>
    </head>
    <body>
        <table>
            <tr>
                <th>用户编号</th>
                <th>用户名</th>
                <th>密码</th>
            </tr>
            <tr th:each="user : ${list}" th:object="${user}">
                <td th:text="*{usrId}"></td>
                <td th:text="*{usrName}"></td>
                <td th:text="*{usrPassword}"></td>
            </tr>
        </table>
    </body>
    </html>

    页面效果:

  6. 在这里插入图片描述
    demo下载链接:链接: https://pan.baidu.com/s/16TznkHNi1-6iUq-xVxDirg 提取码: ih8k

Responses /