SpringBoot中使用Mybatis
程序员文章站
2024-01-07 21:19:40
...
pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<!--测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
springBoot启动类配置
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author springBoot启动类
*/
@SpringBootApplication
@MapperScan("cn.bufanli.mybatis.mapper") //扫描mapper接口位置
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class,args);
}
}
数据源配置类
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* @author 配置类
*/
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {
@Bean
public DataSource getDataSource(JdbcProperties jdbc){
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl(jdbc.getUrl());
dataSource.setDriverClassName(jdbc.getDriverClassName());
dataSource.setUsername(jdbc.getUsername());
dataSource.setPassword(jdbc.getPassword());
return dataSource;
}
}
/**
* @author 这是一个jdbc配置类
* prefix yml 里面的前缀 他会自动寻找位置并且赋值
*/
@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
private String url;
private String driverClassName;
private String username;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
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;
}
}
mapper接口类
import cn.bufanli.mybatis.pojo.Order;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper // mybatis组件注解
public interface MybatisMapper {
/**
* 查询订单详情方法
* @return
*/
// @Select("select * from order")
List<Order> getOrderList();
}
配置yml文件
server:
port: 8089 # 服务端口号
spring:
application:
name: mybatis-service # 服务名称
jdbc: # 数据源链接信息
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/estore
username: root
password: mypassword
mybatis:
type-aliases-package: cn.bufanli.mybatis.pojo # 别名扫描宝
mapper-locations: classpath:mappers/*.xml # 映射文件的路径扫描
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="cn.bufanli.mybatis.mapper.MybatisMapper">
<select id="getOrderList" resultType="order">
select * from orders
</select>
</mapper>
整体结构
推荐阅读