SpringBoot+SpringData整合開發

一、配置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;


@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentDao studentDao;
	
	@Override
	public 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 {

	@Autowired
	private 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;

@SpringBootApplication
public class SpringBootDemo {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemo.class, args);
	}
	
}

大致結構圖



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章