一、新建项目:com.saas.plusdemo
二、配置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>3.2.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.saas</groupId><artifactId>plusdemo</artifactId><version>0.0.1-SNAPSHOT</version><name>com.saas.plusdemo</name><description>com.saas.plusdemo</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--MyBatis Plus 依赖--><dependency> <groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.3</version></dependency><dependency> <groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>3.0.3</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>3.0.3</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.3.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</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-extension</artifactId><version>3.5.5</version><scope>compile</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
三、配置application.yml
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.31.132:3306/linq2dbusername: rootpassword: 123456mybatis-plus:configuration:# 取消驼峰命名映射map-underscore-to-camel-case: false# 配置日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
添加一个:Myconfig 非常重要一定要配
package com.saas.plusdemo;import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;@Configuration
public class Myconfig {@Autowired@Qualifier("dataSource")private DataSource dataSource;@Bean//判定配置文件中的配置属性@ConfigurationProperties(prefix = "mybatis-plus")public SqlSessionFactory sqlSessionFactory() throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();//设置数据源sqlSessionFactoryBean.setDataSource(dataSource);return sqlSessionFactoryBean.getObject();}
}
四、添加实体类Product
package com.saas.plusdemo;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("product")
public class Product {private String id;private String name;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
添加数据库访问仓储:IProductRepository
package com.saas.plusdemo.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.saas.plusdemo.Product;public interface IProductRepository extends BaseMapper<Product> {
}
添加到容器Application
package com.saas.plusdemo;import com.saas.plusdemo.mapper.IProductRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan({"com.saas.plusdemo.mapper"})
public class Application {public static void main(String[] args) {ConfigurableApplicationContext ctx= SpringApplication.run(Application.class, args);var bean= ctx.getBean(IProductRepository.class);}}
测试:
package com.saas.plusdemo;import com.saas.plusdemo.mapper.IProductRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class ApplicationTests {@Autowiredprivate IProductRepository productRepository;@Testvoid contextLoads() {Product product= productRepository.selectById("7cde8fb3-eff9-38e9-5722-3a09e672b985");}}