Spring Boot Mybatis 配置

本文介绍如何在SpringBoot项目中配置MyBatis,并通过具体示例展示数据库操作过程,包括配置文件详解及CRUD接口实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Spring Boot Mybatis 配置

resource/mybatis-config.xml

配置数据库连接方式(jdbc驱动),注册mapper,

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="false"/>
<setting name="localCacheScope" value="STATEMENT"/>
<setting name="useGeneratedKeys" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://80.76.180.95" />
<property name="username" value="root" />
<property name="password" value="Password_12#$" />
</dataSource>
</environment>
</environments>


<mappers>
<mapper resource="mapper/TestMapper.xml"/>
<mapper class="com.deep.infra.persistence.sql.mapper.UserMapper"/>
<mapper class="com.deep.infra.persistence.sql.mapper.AgentMapper"/>
<mapper class="com.deep.infra.persistence.sql.mapper.RoleMapper" />


<mapper class="com.deep.infra.persistence.sql.mapper.NoticeMapper"/>
<mapper resource="mapper/BreedingPlanMapper.xml"/>
<mapper resource="mapper/DiagnosisPlanMapper.xml"/>
<mapper resource="mapper/NutritionPlanMapper.xml"/>
<!--<mapper resource="mapper/NoticePlanMapper.xml"/>-->


<mapper resource="mapper/PicMapper.xml"/>
<mapper resource="mapper/MessageMapper.xml"/>
<mapper resource="mapper/GenealogicalFilesMapperxml.xml" />
<mapper resource="mapper/DisinfectFilesMapper.xml" />
<mapper resource="mapper/ImmunePlanMapper.xml"/>
<mapper resource="mapper/RepellentPlanMapper.xml"/>
<mapper resource="mapper/GenealogicalFilesTransMapper.xml"/>
<mapper resource="mapper/DisinfectionArchivesMapper.xml" />
<mapper resource="mapper/OperationFileMapper.xml" />
<mapper resource="mapper/VideoPublish.xml"/>
<mapper resource="mapper/TypeBriefMapper.xml"/>
<mapper resource="mapper/EnvironmentTraceMapper.xml"/>


</mappers>
</configuration>

  
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="false"/>
<setting name="localCacheScope" value="STATEMENT"/>
<setting name="useGeneratedKeys" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://80.76.180.95" />
<property name="username" value="root" />
<property name="password" value="Password_12#$" />
</dataSource>
</environment>
</environments>


<mappers>
<mapper resource="mapper/TestMapper.xml"/>
<mapper class="com.deep.infra.persistence.sql.mapper.UserMapper"/>
<mapper class="com.deep.infra.persistence.sql.mapper.AgentMapper"/>
<mapper class="com.deep.infra.persistence.sql.mapper.RoleMapper" />


<mapper class="com.deep.infra.persistence.sql.mapper.NoticeMapper"/>
<mapper resource="mapper/BreedingPlanMapper.xml"/>
<mapper resource="mapper/DiagnosisPlanMapper.xml"/>
<mapper resource="mapper/NutritionPlanMapper.xml"/>
<!--<mapper resource="mapper/NoticePlanMapper.xml"/>-->


<mapper resource="mapper/PicMapper.xml"/>
<mapper resource="mapper/MessageMapper.xml"/>
<mapper resource="mapper/GenealogicalFilesMapperxml.xml" />
<mapper resource="mapper/DisinfectFilesMapper.xml" />
<mapper resource="mapper/ImmunePlanMapper.xml"/>
<mapper resource="mapper/RepellentPlanMapper.xml"/>
<mapper resource="mapper/GenealogicalFilesTransMapper.xml"/>
<mapper resource="mapper/DisinfectionArchivesMapper.xml" />
<mapper resource="mapper/OperationFileMapper.xml" />
<mapper resource="mapper/VideoPublish.xml"/>
<mapper resource="mapper/TypeBriefMapper.xml"/>
<mapper resource="mapper/EnvironmentTraceMapper.xml"/>


</mappers>
</configuration>

src/infra.persistence.sql/mapper/TestMapper.java
实现对数据库操作的CRUD接口

  


src/domain/service/TestService.java

实例化CRUD接口
package com.deep.domain.service;
import com.deep.domain.model.TestModel;
import com.deep.infra.persistence.sql.mapper.TestMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * Created by zhongrui on 2018/2/1.
 */
@Service
public class TestService {
  @Resource
  private TestMapper testMapper;

  public TestModel getTestModel(String index) {
    TestModel model = this.testMapper.getTestModelById(index);
    return model;
  }

}


src/domain/model/TestModel.java

用于接收前端信息后对数据库进行操作

package com.deep.domain.model;
public class TestModel {
  private int id;
  private String message;

  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }

}



Spring Boot MyBatis是一个用于构建基于Spring BootMyBatis的Java应用程序的框架。在使用Spring Boot MyBatis配置SQL Server数据库时,你需要考虑以下步骤: 1. 添加依赖:在你的`pom.xml`文件中,添加Spring Boot Starter Data JPA、Spring Boot Starter JDBC和MyBatis依赖。 ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>版本号</version> </dependency> </dependencies> ``` 请注意,将MyBatis的版本与你的Spring Boot版本保持一致。 2. 配置数据库连接:在`application.properties`或`application.yml`文件中,配置数据库连接信息,包括URL、用户名和密码。 ```properties spring.datasource.url=jdbc:sqlserver://服务器地址:端口号;数据库名 spring.datasource.username=数据库用户名 spring.datasource.password=数据库密码 ``` 3. 创建数据模型(Model):为SQL Server表创建Java类,表示数据模型。使用@Table注解标记类与数据库表的映射关系。 4. 创建Mapper接口:为SQL Server表创建接口,使用@Mapper注解标记。在接口中定义SQL语句的执行方法。 5. 配置MyBatis:在`application.properties`文件中,配置MyBatis配置信息,包括SqlSessionFactoryBean的bean定义和数据源配置。 ```properties mybatis.configuration.environment.sqlserver_dialect=sqlserver mybatis-config.xml中配置数据源信息等其他相关配置 ``` 6. 映射数据模型到实体类:在Mapper接口中定义与数据模型对应的查询方法,使用@Select注解标记SQL语句。使用@Result注解指定查询结果映射到实体类的哪个属性。 7. 使用Repository:使用Spring Data JPA提供的Repository接口,通过注解方式调用Mapper接口中的方法,实现数据的CRUD操作。 8. 运行应用程序:构建并运行Spring Boot应用程序,通过Repository接口调用Mapper接口中的方法,执行SQL语句并获取数据。 以上是使用Spring Boot MyBatis配置SQL Server的基本步骤。请根据你的具体需求进行相应的调整和配置。确保根据实际情况提供正确的数据库连接信息和SQL语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值