springboot . 配置jpa使用
生活随笔
收集整理的這篇文章主要介紹了
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-drop3.寫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使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS iOS-System-Servi
- 下一篇: BeautifulSoup的高级应用 之