spring boot++jpa+ mysql +maven

項目結構圖:css

image

1、 添加mysql 配置html

1 在pom.xml中添加 maven依賴java

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

2 在appliation.properties 文件中添加 配置項mysql

#DB Configuration:
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/dbtest?characterEncoding=utf8&useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456

二 、添加 jpa maven依賴 web

<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

在  appliation.properties 文件中添加 配置項spring

#JPA Configuration
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

三 、 新建實體類 Accountsql

package com.example.demo.domain;

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

@Entity
public class Account {
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getLogin_name() {
        return login_name;
    }

    public void setLogin_name(String login_name) {
        this.login_name = login_name;
    }

    public String getLogin_pwd() {
        return login_pwd;
    }

    public void setLogin_pwd(String login_pwd) {
        this.login_pwd = login_pwd;
    }

    public String getReal_name() {
        return real_name;
    }

    public void setReal_name(String real_name) {
        this.real_name = real_name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
     private  long id;
     private  String login_name;
     private  String login_pwd;
     private  String real_name;

}

@entity 實體標記註解api

@Id   id主鍵標記註解app

4、建立AccountRepository 接口 繼承自JPARepositorydom

package com.example.demo.dao;

import com.example.demo.domain.Account;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface AccountRepository extends JpaRepository<Account,Long> {
     List<Account> findAllByOrderByIdDesc();

}

JPA 會自動生成AccountRepository 的實現類

5、 建立IAccountService 服務接口及實現類

IAccountService.java 接口

package com.example.demo.service;

import com.example.demo.domain.Account;

import java.util.List;

public interface IAccountService {
List<Account> findAll();
Account findAccountByName(String login_name);

void Create(Account model);
}

AccountService 實現類 ,服務類,必須標記@Service 註解,經過@autowired 自動注入 accountRepository 接口

package com.example.demo.service.impl;

import com.example.demo.dao.AccountRepository;
import com.example.demo.domain.Account;
import com.example.demo.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountService implements IAccountService {
    @Autowired
    AccountRepository accountRepository ;

    @Override
    public List<Account> findAll() {
        return this.accountRepository.findAllByOrderByIdDesc();
    }

    @Override
    public Account findAccountByName(String login_name) {
        return null;
    }

    @Override
    public void Create(Account model) {
        this.accountRepository.save(model);
    }
}

6、 AccountController 類

package com.example.demo.controller;

import com.example.demo.dao.AccountRepository;
import com.example.demo.domain.Account;
import com.example.demo.service.IAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "Account api",description ="api of account")
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
     IAccountService  accountService ;

      @ApiOperation(value="account index list", notes = "帳戶列表信息")
      @RequestMapping(value = "/index",method = RequestMethod.GET)
      public List<Account> index(){
          return this.accountService.findAll();
      }

    @ApiOperation(value="create a account", notes = "a account name")

    @RequestMapping(value = "/create",method = RequestMethod.POST)
      public  void create(
            @ApiParam( name="model",value = "input a account entity") @RequestBody  Account model)
      {
            this.accountService.Create(model);
      }
}

http://localhost:8080/account/index   訪問全部帳號,返回結果:

[{"id":7,"login_name":"admin","login_pwd":"admin","real_name":"jack"}]

http://localhost:8080/account/create    接口post 建立帳號,返回結果: (這裏應該作一個統一的包裝類進行返回)

相關文章
相關標籤/搜索