1、介紹java
1.springboot是spring項目的總結+整合mysql
當咱們搭smm,ssh,ssjdbc等組合框架時,各類配置不勝其煩,不只是配置問題,在添加各類依賴時也是讓人頭疼,關鍵有些jar包之間還會出現衝突,讓你的項目出現難以解決的問題。基於這種狀況,springboot橫空出世,在考慮到Struts控制層框架有漏洞,springboot放棄(大多數企業一樣如此)了Struts,轉而代之是springMVC,不過,springboot是自動集成springMVC的,不須要任何配置,不須要任何依賴,直接使用各類控制層註解。springboot是springcloud的基礎,是開啓微服務時代的鑰匙。web
2、新建springboot工程spring
1. 使用idea2019新建project,選擇spring Initializr,nextsql
2. 填寫座標信息,next數據庫
3. web選擇Spring Web Starter,SQL選擇Spring Data JPA、MySQL Driver,nextspringboot
4. 填寫項目名已經存放位置,finishapp
3、項目構建框架
1. pom.xmlssh
springboot工程默認,包含spring-boot-starter-web、spring-boot-starter-test、spring-boot-starter-data-jpa以及mysql驅動
2. 業務實現
實現一個用戶擁有多部手機的業務
3. 配置文件
application.properties
######################################################## ###數據庫鏈接信息 ######################################################## #鏈接地址 spring.datasource.url=jdbc:mysql://localhost:3306/springboot_data_jpa?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
#useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC 設置時區,否則可能會報錯
#數據庫帳戶
spring.datasource.username=root
#數據庫密碼
spring.datasource.password=root
#數據庫驅動
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
########################################################
### Java Persistence Api JPA相關配置
########################################################
#指定數據庫類型
spring.jpa.database=mysql
#控制檯打印sql
spring.jpa.show-sql=true
#建表策略,這裏用update,即根據實體更新表結構
spring.jpa.hibernate.ddl-auto=update
#表中字段命名策略,這裏要引入hibernate的核心包,否則這個命名策略會報錯
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
#方言
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
4. entity實體類
package club.xcreeper.springboot_spring_data_jpa.entity; import javax.persistence.*; @Entity @Table(name = "phone") public class Phone { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String brand; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
package club.xcreeper.springboot_spring_data_jpa.entity; import javax.persistence.*; import java.util.List; @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String username; private String password; @OneToMany(targetEntity = Phone.class) @JoinColumn(name = "user_id") private List<Phone> phones; public List<Phone> getPhones() { return phones; } public void setPhones(List<Phone> phones) { this.phones = phones; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
5. dao層
package club.xcreeper.springboot_spring_data_jpa.dao; import club.xcreeper.springboot_spring_data_jpa.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import java.io.Serializable; public interface UserDao extends JpaRepository<User, Serializable> { User findByUsernameAndPassword(String username,String password); }
6. service層
package club.xcreeper.springboot_spring_data_jpa.service; import club.xcreeper.springboot_spring_data_jpa.entity.User; public interface UserService { User findByUsernameAndPassword(String username,String password); }
package club.xcreeper.springboot_spring_data_jpa.service.impl; import club.xcreeper.springboot_spring_data_jpa.dao.UserDao; import club.xcreeper.springboot_spring_data_jpa.entity.User; import club.xcreeper.springboot_spring_data_jpa.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public User findByUsernameAndPassword(String username, String password) { return userDao.findByUsernameAndPassword(username,password); } }
7. controller層
package club.xcreeper.springboot_spring_data_jpa.controller; import club.xcreeper.springboot_spring_data_jpa.entity.User; import club.xcreeper.springboot_spring_data_jpa.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping(value = "/getOne",params = {"username","password","username!=","password!="})//這裏屬性要用value而不能用name,name不起做用 public User getUser(String username,String password) { return userService.findByUsernameAndPassword(username,password); } }
8. 啓動程序後,數據庫表生成,須要添加數據
user表 phone表
9. postman測試