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