步骤 1: 添加依赖
<!-- Spring核心依赖 -->
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>你的Spring版本</version>
</dependency> <!-- MyBatis依赖 -->
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>你的MyBatis版本</version>
</dependency> <!-- MyBatis-Spring整合依赖 -->
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>你的mybatis-spring版本</version>
</dependency> <!-- 数据库连接依赖,例如MySQL -->
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>你的MySQL Connector版本</version>
</dependency>
步骤 2: 配置数据源和MyBatis
在Spring配置文件中(例如applicationContext.xml),配置数据源、SqlSessionFactory和Mapper扫描。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="..."> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/your_database"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 配置mapper文件的位置 --> <property name="mapperLocations" value="classpath*:mapper/*.xml"/> </bean> <!-- 配置Mapper扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.demo.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 开启事务注解 --> <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
步骤 3: 创建实体类
package com.example.demo.entity; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private String email; // 省略getter和setter方法 // 必须提供getter和setter方法,这里为了简洁省略了 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } // 可以添加toString方法方便调试 @Override public String toString() { return "User [id=" + id + ", name=" + name + ", email=" + email + "]"; }
}
步骤 4: 创建Mapper接口和Mapper XML
创建一个Mapper接口,并在对应的XML文件中定义SQL语句。
UserMapper.java
package com.example.demo.mapper; import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper // 使用@Mapper注解告诉Spring这是一个MyBatis的Mapper接口
public interface UserMapper { User selectById(Integer id); List<User> selectAll(); int insert(User user); int update(User user); int deleteById(Integer id);
}
UserMapper.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.example.demo.mapper.UserMapper"> <!-- 根据ID查询用户 --> <select id="selectById" resultType="com.example.demo.entity.User"> SELECT * FROM user WHERE id = #{id} </select> <!-- 查询所有用户 --> <select id="selectAll" resultType="com.example.demo.entity.User"> SELECT * FROM user </select> <!-- 插入用户 --> <insert id="insert" parameterType="com.example.demo.entity.User"> INSERT INTO user (name, email) VALUES (#{name}, #{email}) </insert> <!-- 更新用户 --> <update id="update" parameterType="com.example.demo.entity.User"> UPDATE user SET name = #{name}, email = #{email} WHERE id = #{id} </update> <!-- 根据ID删除用户 --> <delete id="deleteById" parameterType="java.lang.Integer"> DELETE FROM user WHERE id = #{id} </delete> </mapper>
步骤 5: 在服务层使用Mapper
在服务类中注入Mapper,并调用其方法来执行CRUD操作。
UserService.java
package com.example.demo.service; import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Integer id) { return userMapper.selectById(id); } public void addUser(User user) { userMapper.insert(user); } public void updateUser(User user) { userMapper.update(user); } public void deleteUser(Integer id) { userMapper.deleteById(id); } // 可以添加其他业务逻辑方法
}