Spring Boot —Mybatis整合
一.导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- servlet依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat的支持.-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--sprinboot 测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
二.设置定义启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
//@MapperScan可以指定要扫描的Mapper类的包的路径
@MapperScan("cn.mapper")
//@ComponentScan告诉Spring 哪个packages 的用注解标识的类 会被spring自动扫描并且装入bean容器。
@ComponentScan("cn")
public class SpringBootSSm {
public static void main( String[] args ) {
SpringApplication.run(SpringBootSSm.class ,args) ;
}
}
三.springboot核心配置文件
application.yml
spring:
datasource: #数据库连接池
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql:///student
mvc:
view:
suffix: .jsp
prefix: /WEB-INF/views/
pagehelper: #分頁
helperDialect: mysql
reasonable: true
params: count=countSql
四.代码编写
特别注意的是 编写代码时候 必须是在启动类为子侄级别,不可超过启动类包所在级别
例如 启动类在cn.leilei.SpringBootSSm 那么代码编写必须是在cn.leilei.xxx包.xxx之下
同样可以使用generator生成domain ,mapper 以及对应的mpper.xml
domain
public class Student {
private Long id;
private String name;
private String nikeName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNikeName() {
return nikeName;
}
public void setNikeName(String nikeName) {
this.nikeName = nikeName;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", nikeName='" + nikeName + '\'' +
'}';
}
}
mapper
Component标签在这 只是特别再次抢到告诉spring你可以扫描他成为bean,防止其他地方Autowired 注入时候报红(假红)没有其他实际作用 ,,
@Component
public interface StudentMapper {
List<Student> selectAll();
}
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="cn.mapper.StudentMapper" >
<resultMap id="BaseResultMap" type="cn.domain.Student" >
<id column="id" property="id" />
<result column="name" property="name" />
<result column="nike_name" property="nikeName" />
</resultMap>
<select id="selectAll" resultMap="BaseResultMap" >
select id, name, nike_name from student
</select>
</mapper>
service
public interface IStudentService {
List<Student> selectAll();
}
service.impl
@Service
@Transactional
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Override
//@Transactional(readOnly = true)
public List<Student> selectAll() {
return studentMapper.selectAll();
}
}
controller
package cn.controller;
import cn.domain.Student;
import cn.service.IStudentService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/stu")
public class StudentController {
@Autowired
private IStudentService studentService ;
@RequestMapping("/list")
public String findall(Model model){
List<Student> students = studentService.selectAll();
model.addAttribute("students", students);
return "student";
}
@RequestMapping("/listpage")
public String findallpage(Model model){
//调用Mybatis自带的分页方法进行分页,,如果有需要则自己根据mybatis分页方法返回对象数据封装为自己的分页对象返回给前台
PageHelper.startPage(0, 3);
List<Student> students = studentService.selectAll();
model.addAttribute("students", students);
return "student";
}
}
webapp/WEB-INF/views/student.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>姓名</td>
<td>昵称</td>
</tr>
<c:forEach items="${students}" var="s" varStatus="st">
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.nikeName}</td>
</tr>
</c:forEach>
</table>
这里只是简单的在webapp集成了下,其他集成或者在resource下集成,后续更新。