SpringBoot+MyBatis整合開發

一、配置pom.xml文件

引入相關包(引用mybatis包和springBoot整合的包)
 <!-- 整合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>
整個pom.xml
<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層和方法一是一致的

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