Spring整合Mybatis
发布人:shili8
发布时间:2025-01-10 07:28
阅读次数:0
**Spring整合Mybatis**
在实际开发中,Spring和Mybatis是两种非常常用的框架。Spring提供了一个完整的企业级应用程序框架,而Mybatis则是一个基于Java的持久层框架。两者结合起来,可以实现更高效、更灵活的数据访问。
###1.依赖配置首先,我们需要在pom.xml文件中添加必要的依赖:
xml<dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.20</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <!-- Mybatis-Spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> </dependencies>
###2. 配置文件接下来,我们需要配置Spring和Mybatis的相关信息。我们可以在application.properties文件中添加以下内容:
propertiesspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456mybatis.type-aliases-package=com.example.demo.entity
###3. 实体类和Mapper接口在实体类中,我们需要定义对应的JavaBean:
java// User.javapublic class User { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
在Mapper接口中,我们需要定义对应的SQL语句:
java// UserMapper.javapublic interface UserMapper { ListselectAll(); }
###4. Mybatis配置我们需要在Spring配置文件中添加Mybatis相关的配置:
xml<!-- mybatis-config.xml --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config3.0//EN" " /><configuration> <settings> <setting name="cacheEnabled" value="true"/> </settings> <typeAliases> <typeAlias type="com.example.demo.entity.User" alias="User"/> </typeAliases> <mappers> <mapper class="com.example.demo.mapper.UserMapper"/> </mappers> </configuration>
###5. Spring配置我们需要在Spring配置文件中添加相关的Bean定义:
java// application-context.xml<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" /> xmlns:xsi=" /> xsi:schemaLocation=" /> <!-- DataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${spring.datasource.driver-class-name}"/> <property name="url" value="${spring.datasource.url}"/> <property name="username" value="${spring.datasource.username}"/> <property name="password" value="${spring.datasource.password}"/> </bean> <!-- Mybatis --> <bean id="mybatis" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- Mapper --> <bean id="userMapper" class="com.example.demo.mapper.UserMapper"> <property name="sqlSessionFactory" ref="mybatis"/> </bean> </beans>
###6. 测试最后,我们可以在测试类中使用Spring和Mybatis来进行数据访问:
java// TestUser.java@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:application-context.xml"}) public class TestUser { @Autowired private UserMapper userMapper; @Test public void testSelectAll() { Listusers = userMapper.selectAll(); System.out.println(users); } }
通过以上的配置和测试,我们可以看到Spring和Mybatis是如何结合起来实现数据访问的。