pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bbsdj</groupId> <artifactId>springboot01</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>springboot01 Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> </dependencies> <build> <finalName>springboot01</finalName> </build> </project>
application.properties,springboot会自动加载
spring.datasource.username=root spring.datasource.password=root spring.datasource.url=jdbc:mysql:///demo?useUnicode=true&characterEncoding=UTF-8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update
实体类 Employee,springboot启动后会自动创建数据库表
package com.bbsdj.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Employee { private Integer id; private String name; private Integer age; @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
dao层
package com.bbsdj.dao; import com.bbsdj.entity.Employee; import org.springframework.data.repository.Repository; public interface EmployeeDao extends Repository<Employee,Integer> { /** * 根据id查找雇员 * @param id 输入的id * @return */ public Employee findById(Integer id); }
最后SpringBoot启动类
package com.bbsdj.web; import com.bbsdj.dao.EmployeeDao; import com.bbsdj.entity.Employee; import com.bbsdj.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @EnableAutoConfiguration @EnableJpaRepositories(basePackages = "com.bbsdj.dao") @EntityScan(basePackages = "com.bbsdj.entity") @ComponentScan("com.bbsdj") public class App { @Autowired private EmployeeDao employeeDao; @RequestMapping("/getName/{id}") @ResponseBody public String getName( @PathVariable("id") Integer id){ Employee employee = employeeDao.findById(id); if(employee == null){ return "没有查到数据."; } return employee.getName(); } public static void main(String[] args){ SpringApplication.run(App.class,args); } }
如果service、dao都在同一个包下面,就不需要EnableJpaRepositories、EntityScan、ComponentScan注解,如果没有在同一个包下面就需要加上这几个注解,因为springboot只会扫描当前以及子包下面的类,如果同级的类默认是不会扫描的。
另外一种解决方案就是把app启动类放到父包下面,如:com.demo.web、com.demo.dao、com.demo.service,就可以把app启动类放到com.demo下面。这样就解决了包扫描不到的情况。