之前在做微信小程序的后台,用java 写的程序,依靠springboot是在eclipse完成的,最近想要在IDEA来实现,因此自己又搭建了Demo,捋顺springboot整合Mybatis,实现接口成功返回数据。这篇是写给springboot整合Mybatis入门的同学,如果帮助到你们了,希望你们能点个赞。
2.创建springboot项目
点击next
java Version改成8,点击Next。
选上Web,JDBC,Mybatis,Mysql Driver这几个依赖。springboot整合mybatis 的时候,jdbc的依赖也一定要加上,否则后面会出现ERROR with creating name with “XXXX”的报错。点击Next,出现的界面是让你选文件的存放位置,点击FInish。
点击demo,点击pom.xml,查看目前的依赖。如图所示:
<?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.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
接下来我们要创建四个包
如图所示,分别点击package,命名为controller,entity,mapper,service。进入entity,创建一个类,名为User,定义四个变量。
代码:
package com.example.demo.entity;
public class User {
private Integer id;
private String name;
private String city;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
进入maper文件夹,创建UserMapper接口。注意:这里要加上@Mappper注解,告诉Springboot这是一个mapper接口,定义一个方法findAll。
代码:
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> findAll();
}
接下来就进入service文件夹,创建一个UserService 的接口。
代码:
package com.example.demo.service;
import com.example.demo.entity.User;
import java.util.List;
public interface UserService {
List<User> findAll();
}
创建UserService接口的实现类UserServiceImp,在实现类里面要注意要添加@Service注解,springboot的启动类才能扫描到:
这里提示了报错,不着急,点击红色的小灯泡,根据提示去操作。
把沟取消掉就不报错了。
编写UserController类:
代码:
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userservice;
@RequestMapping("/findAll")
public List<User> findAll(){
List<User> list = new ArrayList<>();
list=userservice.findAll();
return list;
}
}
至此,这四层编写好了。接下来去修改配置。先修改下application.propertity的名称,改成,application.yml。改成yml格式更加清晰,看起来简洁。
代码:
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://127.0.0.1/inquiry?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#mybatis的相关配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.hirain.entity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
这是配置,在url那块,inquiry是我的数据库名称,你们改成自己的就行。在driver-class-name这块里,我本地下载的mysql数据库的版本是5.6,所以我填的是com.mysql.jdbc.Driver。如果你们的数据库是6以上,就写成com.mysql.cj.jdbc.Driver。解释一下mybatis的配置信息:mapper-locations: 是mapper文件的存放位置,用来写sql语句的。type-aliases-package是实体类所在包的别名。如果版本是6以下的数据库,去pom.xml文件里面添加mysql驱动的版本,我之前没有添加版本,所以后面出现了不少的bug。
添加以上这一行,即可。
根据mapper-locations的配置,在resource 的资源文件夹下面,创建UserMapper.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.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="city" jdbcType="VARCHAR" property="city" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<select id="findAll" resultType="com.example.demo.entity.User">
SELECT * FROM user
</select>
</mapper>
打开Navicat创建数据库,名称叫做inquiry。再创建表,名称叫做user:
这是去浏览器输入url:localhost:8081/findAll。报错了。
去查看原因
原来mapper包没有被扫描到。在启动类上加上@MapperScan(“com.example.demo.mapper”),指定springboot去扫描mapper所在的包。
再次启动项目,然后访问:
ok。访问到了。springboot整合Mybatis的过程中,容易出现的问题就是注解,注解千万别少了,不然很容易报错