步骤:
1.新建springboot项目:
添加依赖(这里是所有的依赖):
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yty</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--添加数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--添加mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.6.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<!-- 如果不加,那么打包的时候mapper文件不会被加载进来 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- src/main/resources下的指定资源放行 -->
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
2.在bean包下建实体类
package com.yty.demo.bean;
public class Appauthor {
private int id;
private String real_name;
private String nick_name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getReal_name() {
return real_name;
}
public void setReal_name(String real_name) {
this.real_name = real_name;
}
public String getNick_name() {
return nick_name;
}
public void setNick_name(String nick_name) {
this.nick_name = nick_name;
}
@Override
public String toString() {
return "Appauthor{" +
"id=" + id +
", real_name='" + real_name + '\'' +
", nick_name='" + nick_name + '\'' +
'}';
}
}
3.在mapper包下建mapper接口(个人写法不同,有的是dao接口)
package com.yty.demo.mapper;
import com.yty.demo.bean.Appauthor;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import java.util.List;
public interface HelloMapper {
//@Select("select * from APPauthor")
List<Appauthor> sayHello();
}
4.在service下建service接口
package com.yty.demo.service;
import org.springframework.stereotype.Service;
@Service
public interface HelloService {
String sayHello();
}
5.新建service的实现类serviceImpl
package com.yty.demo.service.impl;
import com.yty.demo.mapper.HelloMapper;
import com.yty.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
//泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Component
public class HelloServiceImpl implements HelloService {
@Autowired(required = false)//表示需要注入的时候不需要存在
private HelloMapper helloMapper;
@Override
public String sayHello() {
return "Hello World!"+helloMapper.sayHello().size();
//return "Hello World!";
}
}
6.建controller类
package com.yty.demo.controller;
import com.yty.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/")
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("/helloWorld")
@ResponseBody
public String helloWorld(){
System.out.println("后台....");
return helloService.sayHello();
}
@RequestMapping("returnIndex")
public String returnIndex(){
System.out.println("跳转到index页面!");
return "index";
}
@RequestMapping("rerurnModelandView")
public ModelAndView rerurnModelandView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("modelandview");
return modelAndView;
}
}
7.目录结果截图
踩坑记录:
一.由于把xml文件放在resources目录下然后修改了pom.xml文件导致加载不到application.properties文件,所以无法连接数据库,修改得端口也不生效。最终解决了该问题。
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
二.把mapper得xml文件放在resources目录下不能加载得问题。(该问题耗费了一天的时间)
1.首先最重要的是xml文件必须要和对应的java文件放在同级目录。springboot目录结构为src-》main-》java和rc-》main-》resours,这算是同一级别。所以比如我们Java目录为com.yty.demo.mapper,所以对应的xml文件也要在resours目录下建立相同的目录,也就是com.yty.demo.mapper这个目录来存放xml文件。
2.然后再yml或者properties配置文件中加引导目录,我是在yml中加的,如:
mapper-locations: classpath:com/yty/demo/mapper/*.xml
最终大功告成。