SpringBoot+SpringData整合开发

来源:互联网 发布:java 计算逻辑表达式 编辑:程序博客网 时间:2024/05/17 08:41

一、配置pom.xml文件

引入相关的jar包

<!-- springdata和springboot的依赖包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>

整个的pom.xml(根据自身项目所需进行jar包的添加)

<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>org.springboot.sample</groupId>  <artifactId>spring-boot-sample</artifactId>  <packaging>war</packaging>  <version>1.0.0.0-SNAPSHOT</version>  <name>spring-boot-sample Maven Webapp</name>     <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.8.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>     </dependency>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-test</artifactId>         <scope>test</scope>      </dependency>            <!-- 添加对JSP支持的依赖包 -->      <dependency>              <groupId>org.apache.tomcat.embed</groupId>  <artifactId>tomcat-embed-jasper</artifactId>              <scope>provided</scope>             </dependency>      <dependency>         <groupId>javax.servlet</groupId>         <artifactId>jstl</artifactId>      </dependency>      <!-- 支持热布署 -->     <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional> </dependency>  <!-- 整合mybatis --><!-- <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version></dependency> --><!-- springdata和springboot的依赖包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>     </dependencies>     <repositories>          <repository>             <id>Central</id>               <name>Central</name>               <url>http://repo1.maven.org/maven2</url>           </repository>          <repository>             <id>mvnrepository</id>               <name>mvnrepository</name>               <url>http://mvnrepository.com/</url>           </repository>             <repository>             <id>sonatype</id>               <name>sonatype</name>               <url>http://www.sonatype.org/nexus/</url>           </repository></repositories>           <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>         </project>


二、定义接口

package com.mingde.dao;import org.springframework.data.repository.PagingAndSortingRepository;import com.mingde.po.Student;public interface StudentDao extends PagingAndSortingRepository<Student, Integer> {}

三、定义Service层

StudentService

package com.mingde.services;import java.util.List;import com.mingde.po.Student;public interface StudentService {public List<Student> findAll()throws Exception;}

StudentServiceImpl

package com.mingde.services.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.mingde.dao.StudentDao;import com.mingde.po.Student;import com.mingde.services.StudentService;@Servicepublic class StudentServiceImpl implements StudentService {@Autowiredprivate StudentDao studentDao;@Overridepublic List<Student> findAll() throws Exception {return (List<Student>) studentDao.findAll();}}


四、定义控制层

package com.mingde.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.mingde.po.Student;import com.mingde.services.StudentService;@Controller@RequestMapping("/stu")public class StudentController {@Autowiredprivate StudentService studentService;@RequestMapping("/list")public ModelAndView list()throws Exception{List<Student> list = (List<Student>) studentService.findAll();return new ModelAndView("stu/list", "list",list);}}


五、SpringBoot的配置文件:

#配置数据源spring:  datasource:    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql:///test    username: root    password: 123#配置SpringMVC视图  mvc:    view:      prefix: /WEB-INF/      suffix: .jsp#切换不同的配置文件#  profiles:#    active: dev#如果当前目录(resources)及其下面的config目录中都有application.yml文件#则config目录下的优先级要高于当前目录resources中的优先级  server:    port: 90


六、定义启动器

package com.mingde;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringBootDemo {public static void main(String[] args) {SpringApplication.run(SpringBootDemo.class, args);}}

大致结构图



原创粉丝点击