实测:Springboot整合Mybatis,一次成功绝对可靠
开发环境准备
- IDEA
- MySQL
建表语句
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
创建项目,请按图索骥
通过以上方式创建的项目自动导入整合mybatis所需的mybatis场景启动器、MySQL数据驱动、jdbc驱动,以及spingmvc场景启动器,详情可查看项目的pom.xml文件
<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>
开始代码编写
要严格遵守以下的代码目录结构编写,红框标注更是重中之重,一定要保证UserMapper与它的xml映射映射文件在同一名称的目录下。(其实直接将UserMapper.xml与UserMapper直接写在同一包也可,但是打包时会出现以下问题,在这里就不演示了)
一、编写配置文件application.properties
#配置数据源
server.port=8060
spring.datasource.url=jdbc:mysql://localhost:3306/hk?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
#Mybatis配置
#配置扫描mapper配置文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#配置别名 包路径为实体类路径
mybatis.type-aliases-package=com.yanqinghai.demo.bean
二、创建实体类User
package com.yanqinghai.demo.bean;
import java.io.Serializable;
/**
* @author 作者
* @version 1.0
* @Description:TODO
* @date 2020/9/15 9:16
*/
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String password;
public User() {
}
public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
创建UserMapper接口,接口上添加@Mapper注解
package com.yanqinghai.demo.mapper;
import com.yanqinghai.demo.bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
- @author 作者
- @version 1.0
- @Description:TODO
- @date 2020/9/15 9:21
*/
@Repository
@Mapper //扫描mapper接口,生成接口的代理对象
public interface UserMapper {
User findById(Long id);
List<User> findAll();
}
创建UserMapper.xml映射文件
- 创建与UserMapper相同的目录结构 com/yanqinghai/demo/mapper 多级文件夹之间用 / 进行分割即可创建出对应的相同目录结构
- 编写UserMapper.xml代码,这里要注意的是名称空间namespace和封装结果类型 type
<?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.yanqinghai.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.yanqinghai.demo.bean.User" >
<result column="id" property="id" />
<result column="name" property="name" />
<result column="password" property="password" />
</resultMap>
<select id="findById" resultMap="BaseResultMap">
select * from user where id = #{id}
</select>
<select id="findAll" resultMap="BaseResultMap">
select * from user
</select>
</mapper>
由于为了测试方便并没有创建service 以及 serviceImpl,编写controller层代码,UserController
package com.yanqinghai.demo.contoller;
import com.yanqinghai.demo.bean.User;
import com.yanqinghai.demo.mapper.UserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 作者
* @version 1.0
* @Description:TODO
* @date 2020/9/15 9:22
*/
@RestController
public class UserController {
@Resource
private UserMapper userMapper;
@GetMapping("byId")
public User findUser(@RequestParam(value = "id") Long id){
return userMapper.findById(id);
}
@GetMapping("byAll")
public List<User> findAll(){
return userMapper.findAll();
}
}
启动主程序进行测试
package com.yanqinghai.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}