1. 程式人生 > >SpringBoot+MyBatis整合開發

SpringBoot+MyBatis整合開發

<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>
		<!-- 匯入MySQL資料庫包 -->
		<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>

方式一:基於MyBatis註解的方式整合

1.在配置檔案application.yml中新增關於資料來源的配置

#配置資料來源
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///test
    username: root
    password: 123
#配置springmvc的檢視
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp
#定義服務埠號
server:
  port: 90 

2.定義資料訪問的介面及使用註解方式相關SQL語句

package com.mingde.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.mingde.po.Student;

public interface StudentMapper {

	//查詢所有學生
	@Select("select * from student")
	public List<Student> findAll()throws Exception;
	
}

3、在啟動檔案所在類中新增@MapperScan註解,用於掃描Mapper包中的所有Mapper介面

package com.mingde;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.mingde.mapper") //掃描mapper包,進行mapper的對映
public class SpringBootApplicationDemo {

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

4.定義Service層

StudentService.java
package com.mingde.servlet;

import java.util.List;

import com.mingde.po.Student;

public interface StudentService {

	public List<Student> findAll()throws Exception;
	
}

StudentServiceImpl.java
package com.mingde.servlet.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mingde.mapper.StudentMapper;
import com.mingde.po.Student;
import com.mingde.servlet.StudentService;

@Service("studentService")
public class StudentServiceImpl implements StudentService {

	@Autowired  //也可用@Resource
	private StudentMapper studentMapper;

	@Override
	public List<Student> findAll() throws Exception {
		return studentMapper.findAll();
	}	
}

5.定義Controller層

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.mapper.StudentMapper;
import com.mingde.po.Student;
import com.mingde.servlet.StudentService;

@Controller
@RequestMapping("student")
public class StudentController {

	@Autowired
	private StudentService studentService;
	@Autowired
	private StudentMapper studentMapper;
	
	
	@RequestMapping("list")
	public ModelAndView list()throws Exception{
		List<Student> findAll = studentService.findAll();
		return new ModelAndView("list","findAll",findAll);
	}
}


方法二:基於MyBatis 的mappe.XML檔案的方式整合

1.配置application.yml檔案

#配置資料來源
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///test
    username: root
    password: 123
#配置springmvc的檢視
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp
#定義服務埠號
server:
  port: 90 
#定義MyBatis的配置
mybatis:
  #定義MyBatis實體類的別名
  type-aliases-package: com.mingde.po
  #下面定義的是MyBatis的全域性配置檔案
  config-location: classpath:mybatis/myBatisConfig.xml
  #定義mapper檔案所在的位置
  mapper-locations: classpath:mybatis/mapper/*.xml

2.定義mybatis的全域性配置以及mapper.xml檔案

MyBatis的全域性配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

mapper.xml的配置檔案
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.mingde.mapper.StudentMapper">
	<select id="findStudents" resultType="com.mingde.po.Student" >
		select * from student
	</select>
</mapper>

3.定義mapper介面

package com.mingde.mapper;

import java.util.List;
import com.mingde.po.Student;

public interface StudentMapper {

	public List<Student> findStudents()throws Exception;
	
}

4.定義Service層和Controller層和方法一是一致的