1.maven引入配置java
<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
#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.javaweb
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * * @author lili */ @Entity public class User { @Id @GeneratedValue private 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接口spring
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.寫controllersql
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 { @Autowired UserRepository userRepository; @RequestMapping("/userall") public List<User> all(){ return userRepository.findAll(); }; }
6.啓動springboot的主類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 here SpringApplication.run(App.class, args); } }
7.啓動成功後 ,訪問localhost:8080/userall 獲得數據格式以下app
[{"id":1,"username":"超級管理員","password":"123456","account":"admin","position":"1","levels":"1","userId":1}]