mysqljava
<!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
springdatajpamysql
<!-- springdata jpa依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
######################################################## ###datasource ######################################################## spring.datasource.url = jdbc:mysql://localhost:3306/springboot spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10 ######################################################## ### Java Persistence Api ######################################################## # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy #[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy] spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
import org.springframework.data.repository.CrudRepository; import com.xujie.pojo.Target; public interface TargetRepository extends CrudRepository<Target,Integer>{ }
spring.jpa.hibernate.ddl-auto = update
的話,jpa能夠直接根據entity建立表;示例:web
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity(name="target") public class Target { @Id @GeneratedValue private int tid; @Column(name="tname",length=20) private String tname; public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } @Override public String toString() { return "Target [tid=" + tid + ", tname=" + tname + "]"; } }
import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.xujie.pojo.Target; import com.xujie.repository.TargetRepository; import com.xujie.service.TargetService; @Service public class TargetServiceimpl implements TargetService { // @Resource是根據名稱注入 @Autowired是根據類型注入 @Resource private TargetRepository targetRepository; //添加 @Override public void save(Target target) { this.targetRepository.save(target); } //刪除 @Override public void delete(Integer id) { this.targetRepository.delete(id); } //查詢 @Override public List<Target> findAll(){ return (List<Target>) this.targetRepository.findAll(); } }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.xujie.pojo.Target; import com.xujie.service.TargetService; @RestController public class TargetController { @Autowired private TargetService targetService; //添加 @GetMapping("/save") public void save() { Target target = new Target(); target.setTname("減肥"); this.targetService.save(target); } //刪除 @GetMapping("/delete/{id}") public void delete(@PathVariable Integer id) { this.targetService.delete(id); } //查詢全部 @GetMapping("/findAll") public List<Target> findAll() { return this.targetService.findAll(); } }