日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot . 配置jpa使用

發布時間:2025/7/14 编程问答 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot . 配置jpa使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

1.maven引入配置

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- MYSQL --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies>

2.prpperties配置

#mysql spring.datasource.url=jdbc:mysql://localhost:3306/ncdg spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #jpa spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop

3.寫entity.java

import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;/**** @author lili*/ @Entitypublic class User {@Id@GeneratedValueprivate long id;private String username;private String password;private String account;private String position;private String levels;private Long userId;/*** @return the id*/public long getId() {return id;}/*** @param id the id to set*/public void setId(long id) {this.id = id;}/*** @return the username*/public String getUsername() {return username;}/*** @param username the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the password*/public String getPassword() {return password;}/*** @param password the password to set*/public void setPassword(String password) {this.password = password;}/*** @return the account*/public String getAccount() {return account;}/*** @param account the account to set*/public void setAccount(String account) {this.account = account;}/*** @return the position*/public String getPosition() {return position;}/*** @param position the position to set*/public void setPosition(String position) {this.position = position;}/*** @return the levels*/public String getLevels() {return levels;}/*** @param levels the levels to set*/public void setLevels(String levels) {this.levels = levels;}/*** @return the userId*/public Long getUserId() {return userId;}/*** @param userId the userId to set*/public void setUserId(Long userId) {this.userId = userId;}};

?

4.寫接口repository接口

import com.mycompany.ncdg.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param;/**** @author lili*/ public interface UserRepository extends JpaRepository<User, Long> {@Query("from User u where u.username=:username")User findUser(@Param("username") String username);}

5.寫controller

import com.mycompany.ncdg.entity.User; import com.mycompany.ncdg.repository.UserRepository; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/**** @author lili*/ @RestController public class UserController {@AutowiredUserRepository userRepository;@RequestMapping("/userall")public List<User> all(){return userRepository.findAll();}; }

6.啟動springboot的主類

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/**** @author lili*/ @SpringBootApplication public class App {/*** @param args the command line arguments*/public static void main(String[] args) {// TODO code application logic hereSpringApplication.run(App.class, args);}}

?

?

?

7.啟動成功后 ,訪問localhost:8080/userall? ?得到數據格式如下

[{"id":1,"username":"超級管理員","password":"123456","account":"admin","position":"1","levels":"1","userId":1}]

?

轉載于:https://my.oschina.net/u/2428630/blog/1612116

總結

以上是生活随笔為你收集整理的springboot . 配置jpa使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。