1.我们要想在maven工程中使用Springboot 需要导入一个父类依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
</parent>
2.我们还需要导入一个Springboot的启动器
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
</dependencies>
3.导入springdata Jpa 需要的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
注意:导入的依赖别忘记数据库连接驱动依赖
需要我们自己建一个配置文件来存放我们需要的数据库信息
在 src/main/resources 下添加 application.properties 配置文件,内容如下:
注意:配置文件的名字最好不要更改(这个名字是规范的,默认识别这个名字 如果修改 , 需要我们自己设置(自行百度))
#需要的数据库信息
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/taotao
spring.datasource.username=root
spring.datasource.password=root
#JPA的参数信息 (使用的Hibernate实现)
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingSt
rategy
下面是一个完整的案例:
4.1 创建实体类
import javax.persistence.Entity;
import javax.persistence.Id;
//注意实体类的名字和标的名字不一样的时候需要配置另外一个注解
@Table("表名")
@Entity
public class User {
@Id
// 这个地方还需要设置主键生成策略
//属性名和表中的字段名不一样时候还需要使用注解来进行映射(@column(“表中字段名”))
private Long id;
private String userName;
private String password;
private String name;
//添加 get 和 set 方法
}
4.2 创建 DAO 接口
//注意“”我们dao层一定要继承JpaRepository两个参数
一个是实体类 ,另外一个主键的类型
import org.springframework.data.jpa.repository.JpaRepository;
import cn.itcast.info.pojo.User;
public interface UserDao extends JpaRepository<User, Long> {
}
4.3 创建业务逻辑接口
import java.util.List;
import cn.itcast.info.pojo.User;
public interface UserService {
List<User> findAll();
}
4.4 创建业务逻辑实现类
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.itcast.info.dao.UserDao;
import cn.itcast.info.pojo.User;
import cn.itcast.info.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
List<User> list = this.userDao.findAll();
return list;
}
}
4.5 创建 Controller
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;
import cn.itcast.info.pojo.User;
import cn.itcast.info.service.UserService;
@RestController
@RequestMapping("user")
public class UserControlelr {
@Autowired
private UserService userService;
@RequestMapping("list")
public List<User> queryUserAll() {
List<User> list = this.userService.findAll();
return list;
}
}
46 创建引导类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
总结:Springboot 整合 Springboot JPA 可以使我们不用关注sql语句的编写,让我们更关注于 逻辑的处理,特别注意 实体类一定要的映射做好,不然会出错!!