Springboot+SpringData+jpa
2017-05-18 16:08
429 查看
Springboot+SpringData+jpa
Eclipse中搭建过程:
本案例使用maven方式构建项目
1、 首先在eclipse中创建一个maven工程
2、 修改pom.xml文件,完整pom.xml代码如下:
3、 如果项目中需要连接数据库,还需要添加数据库相关的驱动包
4、 在src/main/resources目录下,新建application.properties或application.yml类型的配置文件(本案例使用的是application.properties这种类型的配置文件),配置文件内容如下:
5、 src/main/java下新建一个包,如:com.lhc.springbootjpa,并新建一个类,如:Application.java,代码如下:
6、 新建bean包,如:com.lhc.springbootjpa.bean,并新建实体类,如Emp.java,实体类中的代码如下:
7、 新建com.lhc.springbootjpa.dao,并新建dao接口IEmpDao.java(因为使用了jpa和springdata,所以只需要建接口,不需要实现类),代码如下:
8、 新建com.lhc.springbootjpa.service,并新建service接口和service实现类
接口IEmpService.java如下:
实现类EmpServiceImpl.java如下:
9、 新建com.lhc.springbootjpa.controller包,并新建一个controller类,如HelloController.java,代码如下:
10、 找到Application.java类,右键run as启动spring boot
11、 打开浏览器,输入http://localhost:springboot配置的端口号/queryEmpAll就可以访问上面的queryEmpAll方法,并且因为是@RestController,那么返回的list集合会自动转换成json,这样页面就可以看到查询的json数据
这样一个基本的整合就完成了,但是查询的数据没有在页面中展示,也就是没有视图层,那么springboot不太推荐使用 jsp这样的动态页面做为视图层(可以使用jsp,但官方不推荐),所以这里可以使用类似thymeleaf这样的模板框架来充当视图层
12、 下面进行thymeleaf的相关配置
1)如果在springboot中,想要使用thymeleaf这样的模板框架,需要先在application.properties中进行配置,需要添加的配置代码如下:
2)在pom.xml中,添加thymeleaf的配置,上面讲到配置的pom.xml中,已经配置过了,这里就不在添加了,但是如果在上面讲到的pom.xml中,没有配置thymeleaf,那么这里必须要添加相应配置,否则会出错
2)这样就可以在HelloController.java中使用springmvc的写法,跳转到某个视图,更改后完整的HelloController.java代码如下:
3) 在src/main/resources中添加一个templates文件夹,并在文件夹中新建一个html文件,如:empList.html(文件前缀名称,要与上面controller方法的返回值一样,文件类型是html),代码内容如下:
4)最后就可以重新启动,在页面上还输入上面讲到的那个地址,次数就会把数据在页面中显示了
Eclipse中搭建过程:
本案例使用maven方式构建项目
1、 首先在eclipse中创建一个maven工程
2、 修改pom.xml文件,完整pom.xml代码如下:
<projectxmlns="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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lhc.springbootjpa</groupId> <artifactId>SpringBoot_jpa_test1</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.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> <!-- Spring boot 引用Thymeleaf模板依赖包(Thymeleaf模板如果不适用,这里也可以不添加这段配置,Thymeleaf模板使用在下面会讲到) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> </project>
3、 如果项目中需要连接数据库,还需要添加数据库相关的驱动包
4、 在src/main/resources目录下,新建application.properties或application.yml类型的配置文件(本案例使用的是application.properties这种类型的配置文件),配置文件内容如下:
#更改springboot端口号(默认是8080) server.port=8089 spring.datasource.driver-class-name=oracle.jdbc.OracleDriver spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.username=scott spring.datasource.password=tiger #jpaconfiguration spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
5、 src/main/java下新建一个包,如:com.lhc.springbootjpa,并新建一个类,如:Application.java,代码如下:
package com.lhc.springbootjpa; import org.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication /** * 要将Application放在最外层,也就是要包含所有子包。 比如你的groupId是com.google,子包就是所谓的com.google.xxx,所以要将Application放在com.google包下。 请参考以下结论:spring-boot会自动加载启动类所在包下及其子包下的所有组件. * @author LHC * */ public class Application { public static voidmain(String[] args) { SpringApplication.run(Application.class,args); } }
6、 新建bean包,如:com.lhc.springbootjpa.bean,并新建实体类,如Emp.java,实体类中的代码如下:
package com.lhc.springbootjpa.bean; importjava.util.Date; importjavax.persistence.Entity; importjavax.persistence.GeneratedValue; importjavax.persistence.Id; importjavax.persistence.Table; @Entity @Table(name="emp2") public class Emp { private int eid; private String ename; private String sex; private Date hire; private float sar; private int did; @Id @GeneratedValue public int getEid() { return eid; } public void setEid(int eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getHire() { return hire; } public void setHire(Date hire) { this.hire = hire; } public float getSar() { return sar; } public void setSar(float sar) { this.sar = sar; } public int getDid() { return did; } public void setDid(int did) { this.did = did; } }
7、 新建com.lhc.springbootjpa.dao,并新建dao接口IEmpDao.java(因为使用了jpa和springdata,所以只需要建接口,不需要实现类),代码如下:
package com.lhc.springbootjpa.dao; importorg.springframework.data.jpa.repository.JpaRepository; importcom.lhc.springbootjpa.bean.Emp; public interface IEmpDao extends JpaRepository<Emp, Integer> { }
8、 新建com.lhc.springbootjpa.service,并新建service接口和service实现类
接口IEmpService.java如下:
package com.lhc.springbootjpa.service; import java.util.List; public interface IEmpService { publicList queryEmpAll(); }
实现类EmpServiceImpl.java如下:
package com.lhc.springbootjpa.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.lhc.springbootjpa.dao.IEmpDao; @Service public class EmpServiceImpl implements IEmpService { @Autowired private IEmpDao empDao; @Override public List queryEmpAll() { // TODO Auto-generated method stub return empDao.findAll(); } }
9、 新建com.lhc.springbootjpa.controller包,并新建一个controller类,如HelloController.java,代码如下:
package com.lhc.springbootjpa.controller; importjava.util.List; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; importcom.lhc.springbootjpa.bean.Emp; importcom.lhc.springbootjpa.service.IEmpService; @RestController public class HelloController { @Autowired privateIEmpService empService; @RequestMapping("/queryEmpAll") publicList queryEmpAll() { List<Emp>empList=empService.queryEmpAll(); for(Emp emp : empList) { System.out.println(emp.getEname()); } returnempList; } }
10、 找到Application.java类,右键run as启动spring boot
11、 打开浏览器,输入http://localhost:springboot配置的端口号/queryEmpAll就可以访问上面的queryEmpAll方法,并且因为是@RestController,那么返回的list集合会自动转换成json,这样页面就可以看到查询的json数据
这样一个基本的整合就完成了,但是查询的数据没有在页面中展示,也就是没有视图层,那么springboot不太推荐使用 jsp这样的动态页面做为视图层(可以使用jsp,但官方不推荐),所以这里可以使用类似thymeleaf这样的模板框架来充当视图层
12、 下面进行thymeleaf的相关配置
1)如果在springboot中,想要使用thymeleaf这样的模板框架,需要先在application.properties中进行配置,需要添加的配置代码如下:
spring.thymeleaf.prefix=classpath:templates/ spring.thymeleaf.surfix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.context-type=text/html spring.thymeleaf.cache=false
2)在pom.xml中,添加thymeleaf的配置,上面讲到配置的pom.xml中,已经配置过了,这里就不在添加了,但是如果在上面讲到的pom.xml中,没有配置thymeleaf,那么这里必须要添加相应配置,否则会出错
2)这样就可以在HelloController.java中使用springmvc的写法,跳转到某个视图,更改后完整的HelloController.java代码如下:
packagecom.lhc.springbootjpa.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.lhc.springbootjpa.bean.Emp; import com.lhc.springbootjpa.service.IEmpService; @Controller public class HelloController { @Autowired private IEmpService empService; @RequestMapping("/queryEmpAll") public String queryEmpAll(Model m) { List<Emp>empList=empService.queryEmpAll(); for (Emp emp : empList) { System.out.println(emp.getEname()); } m.addAttribute(empList); return "empList"; } }
3) 在src/main/resources中添加一个templates文件夹,并在文件夹中新建一个html文件,如:empList.html(文件前缀名称,要与上面controller方法的返回值一样,文件类型是html),代码内容如下:
<!DOCTYPEhtml> <html> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <!-- 页面需要注意,所有的标签都必须要有结束标签,不然做为Thymeleaf模板会出错 --> <body> <table> <tr> <td>id</td> <td>姓名</td> <td>性别</td> </tr> <trth:each="emp:${empList}"> <tdth:text="${emp.eid}"></td> <tdth:text="${emp.ename}"></td> <tdth:text="${emp.sex}"></td> </tr> </table> </body> </html>
4)最后就可以重新启动,在页面上还输入上面讲到的那个地址,次数就会把数据在页面中显示了
相关文章推荐
- 4. Spring Boot 1.2.5,Spring Data JPA多数据源支持
- Spring Boot 下配置SpringDataJpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot+Spring Data JPA+Spring Cache
- 从零开始学spring-boot(2)-集成spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- SpringBoot Data JPA
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- spring boot集成data-jpa
- Spring Boot with Spring Data JPA (1) - Concept
- 74. Spring Data JPA方法定义规范【从零开始学Spring Boot】
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Distributed transactions with multiple databases, Spring Boot, Spring Data JPA and Atomikos
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- springboot(五):spring data jpa的使用
- spring boot 使用 spring data jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa