SpringBoot学习使用(与mybatis plus整合)

以一个简单的Demo为例搭建一个简单的SpringBoot项目

File–>new–>project

如上选择
若没有Spring Assistant选项要在plugins里安装该插件


选择各种需要的选项后,next,选web项目,取名等等这里就不写了。。。

项目建好了结构大概是这样的。(这里我懒得创新的了,还有自己写的一些文件就忽略吧)反正没有example里的那4个文件夹,还有resources里的mapper和static和templates。

然后,打开pom文件加依赖–

这个看起来很乱,不知道为什么粘贴过来就不排版了。

<?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 http://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.1.RELEASE</version>  
 <relativePath/> <!-- lookup parent from repository -->  
  </parent>  
 <groupId>com.example</groupId>  
 <artifactId>springbootdemo</artifactId>  
 <version>0.0.1-SNAPSHOT</version>  
 <name>SpringBootDemo</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>mysql</groupId>  
 <artifactId>mysql-connector-java</artifactId>  
 <scope>runtime</scope>  
 </dependency> <dependency> <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-starter-test</artifactId>  
 <scope>test</scope>  
 </dependency> <dependency> <groupId>com.baomidou</groupId>  
 <artifactId>mybatis-plus-boot-starter</artifactId>  
 <version>2.3</version>  
 </dependency> <dependency> <groupId>org.apache.velocity</groupId>  
 <artifactId>velocity</artifactId>  
 <version>1.7</version>  
 </dependency> <dependency> <groupId>com.baomidou</groupId>  
 <artifactId>mybatis-plus</artifactId>  
 <version>2.3</version>  
 </dependency> </dependencies>  
 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>  
 <artifactId>spring-boot-maven-plugin</artifactId>  
 </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory>  
 <filtering>true</filtering>  
 </resource> </resources> </build>  
</project>

**这里有些包不配置的话后面会出错,像我就在mybatis—plus包上折腾了很久,网上很多人都说只引入mybatis-plus-boot-starter包就行,其实不对,一定要引入mybatis-plus的包。虽然你只使用mybatis-plus-boot-starter正常使用mybatis-plus封装好的mapper方法不会报错,但是当你想自定义xml文件,写语句时,使用时就会一直报如下错误

#org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
这个错误网上的解释大多都是xml文件名字或者namespace不匹配,但是这样是解决不了这个问题的。只有把mybatis plus包引入后,在springboot的application.properties里加入**

mybatis-plus.mapper-locations=classpath*:/mapper/*.xml

完美解决。

另外,对于mybatis plus的版本,也有很多问题,当我使用3.0以上版本的时候,很多类会找不到,比如自动生成器的AutoGenerator。这里我懒得去找新版本该怎么使用,下次再找吧。

还有那个velocity的依赖是模版引擎的依赖,不添加后来运行也会报错

https://blog.csdn.net/qq_39609151/article/details/82855305


利用mybatis plus的生成器生成(地址CLICK)各层代码后,我们把mapper的xml文件放入resource目录下(新建mapper目录放入)。

在mapper接口类上加上注解@Mapper
或者在springBoot启动类上加上注解扫描mapper,如下

/*@ComponentScan  
@EnableAutoConfiguration*/  
@SpringBootApplication  
@MapperScan("com.example.mapper")  
public class SpringBootDemoApplication {  

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

}

在application.properties里配置数据库和其他的属性

如下

server.port=8080  

spring.datasource.url=jdbc:mysql://localhost:3306/mpdemo?useUnicode=true&useSSL=false&characterEncoding=utf8&&serverTimezone=GMT  
spring.datasource.username=root  
spring.datasource.password=olonn  
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver  

mybatis-plus.mapper-locations=classpath*:/mapper/*.xml  
#mybatis-plus.type-aliases-package=com....

这里我们用的mybatis plus就不需要别名了,另外前面引入了mybatis plus 的依赖,才能写成mybatis-plus…的形式,不然就用mybatis…(这样不能自定义xml内容)


我们在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.example.mapper.UserMapper">  

  <!-- 开启二级缓存 -->  
<!--    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>-->  

 <!-- 通用查询映射结果 -->  <resultMap id="BaseResultMap" type="com.example.beans.User">  
 <id column="id" property="id" />  
 <result column="name" property="name" />  
 <result column="birth" property="birth" />  
 <result column="age" property="age" />  
 <result column="dog" property="dog" />  
 </resultMap>  
  <!-- 通用查询结果列 -->  
  <sql id="Base_Column_List">  
  id, name, birth, age, dog  
    </sql>  

 <sql id="My_Column_List">  
  name, birth, age, dog  
    </sql>  

 <select id="selectFour" resultType="com.example.beans.User">  
  SELECT * from USER where id=4  
    </select>  

 <insert id="insertOneCo">  
  insert into USER(<include refid="My_Column_List"/>)  
          value(#{name},#{birth},#{age},#{dog})  
    </insert>  

</mapper>

Mapper层

package com.example.mapper;  

import com.example.beans.User;  
import com.baomidou.mybatisplus.mapper.BaseMapper;  
import org.apache.ibatis.annotations.Mapper;  

/**  
 * <p>  
  *  Mapper 接口  
 * </p>  
  *  
 * @author xfff  
 * @since 2019-01-14  
 */  
@Mapper  
public interface UserMapper extends BaseMapper<User> {  

    User selectFour();  
 void insertOneCo(User user);  
}

Service层:

package com.example.service;  

import com.example.beans.User;  
import com.baomidou.mybatisplus.service.IService;  

/**  
 * <p>  
  *  服务类  
 * </p>  
  *  
 * @author xfff  
 * @since 2019-01-14  
 */public interface UserService extends IService<User> {  
    User selectFour();  
}

实现类:

package com.example.service.impl;  

import com.example.beans.User;  
import com.example.mapper.UserMapper;  
import com.example.service.UserService;  
import com.baomidou.mybatisplus.service.impl.ServiceImpl;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  

/**  
 * <p>  
  *  服务实现类  
 * </p>  
  *  
 * @author xfff  
 * @since 2019-01-14  
 */@Service  
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {  
    @Autowired  
  private UserMapper userMapper;  


  @Override  
  public User selectFour() {  
        User user=userMapper.selectFour();  
 return user;  
  }  
}

Controller:

package com.example.controller;  


import com.example.beans.User;  
import com.example.mapper.UserMapper;  
import com.example.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.RequestMapping;  

import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.ResponseBody;  
import org.springframework.web.bind.annotation.RestController;  

import java.util.Date;  

/**  
 * <p>  
  *  前端控制器  
 * </p>  
  *  
 * @author xfff  
 * @since 2019-01-14  
 */@RestController  
@RequestMapping("/user")  
public class UserController {  

    @Autowired  
  private UserService userService;  

  @Autowired  
  private UserMapper userMapper;  

  @RequestMapping("/getU")  
    @ResponseBody  
  public User user(){  
        return userService.selectById(4);  
  }  

    @RequestMapping("/sf")  
    @ResponseBody  
  public User selectFour(){  
        return userService.selectFour();  
  }  

    @RequestMapping("/haha")  
    @ResponseBody  
  public String haha(){  
        User auser=new User();  
  auser.setAge(14);  
  auser.setBirth(new Date());  
  auser.setName("HHAHAHA");  
  auser.setDog("OTOT");  
  userMapper.insertOneCo(auser);  
 return "OKOK";  
  }  

}

运行SpringBoot启动类,该类要放在和其他包同级或上级下。

相比于SSM框架,搭建一个项目springboot快很多


另外,Controller上的注解若为RestController的话,返回的就是像json和普通类型数据,Controller可以返回页面。
具体参考

https://blog.csdn.net/wuzengwen18914044177/article/details/78342517

另外在springboot项目中,resource下的static目录为静态目录,可以直接在地址栏输入对应的地址访问。


继续学习深入吧~