本文主要是介绍SpringBoot与MyBatis简单整合,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
步骤一、配置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.6.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shen</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
步骤二、实体类
package com.shen.springbootmybatis.model;
public class User {
private int id;
private String username;
private String password;
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
步骤三、XxxMapper接口类
package com.shen.springbootmybatis.mapper;
import com.shen.springbootmybatis.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserXmlMapper {
public List<User> findAll();
}
步骤四、XxxMapper接口映射的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.shen.springbootmybatis.mapper.UserXmlMapper">
<resultMap id="returnType" type="com.shen.springbootmybatis.model.User">
<id property="id" column="ID"/>
<result property="username" column="USERNAME"/>
<result property="password" column="PASSWORD"/>
</resultMap>
<select id="findAll" resultMap="returnType">
select * from tb_user
</select>
</mapper>
步骤五、application.yml
spring:
# 数据源信息配置
datasource:
url: jdbc:mysql://192.168.137.5:3306/dbtmp?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis
mybatis:
# mapper 映射文件路径
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.shen.springbootmybatis.model
# config-location: --指定mybatis核心配置文件
步骤六、单元测试
package com.shen.springbootmybatis;
import com.shen.springbootmybatis.mapper.UserMapper;
import com.shen.springbootmybatis.mapper.UserXmlMapper;
import com.shen.springbootmybatis.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private UserXmlMapper userXmlMapper;
@Test
public void testFindAll() {
List<User> users = userMapper.findAll();
System.out.println(users);
}
@Test
public void testFindAllXml() {
List<User> users = userXmlMapper.findAll();
System.out.println(users);
}
}
这篇关于SpringBoot与MyBatis简单整合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!