欢迎访问 生活随笔!

生活随笔

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

编程问答

springboot . 配置jpa使用

发布时间:2025/7/14 编程问答 65 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。