其他数据库集成
2026/7/16大约 3 分钟
其他数据库集成
除了 JPA,Spring Boot 还支持 MyBatis、Redis、MongoDB、Elasticsearch 等多种数据源。
MyBatis
依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.entity
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Mapper 接口
@Mapper
public interface UserMapper {
@Select("SELECT * FROM users WHERE id = #{id}")
User findById(Long id);
@Select("SELECT * FROM users")
List<User> findAll();
@Insert("INSERT INTO users(name, email, age) VALUES(#{name}, #{email}, #{age})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insert(User user);
@Update("UPDATE users SET name=#{name}, email=#{email} WHERE id=#{id}")
int update(User user);
@Delete("DELETE FROM users WHERE id=#{id}")
int deleteById(Long id);
// 复杂查询用 XML
List<User> search(@Param("keyword") String keyword, @Param("age") Integer age);
}
XML Mapper
<!-- resources/mapper/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.mapper.UserMapper">
<resultMap id="userMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="createdAt" column="created_at"/>
</resultMap>
<select id="search" resultMap="userMap">
SELECT * FROM users
<where>
<if test="keyword != null and keyword != ''">
AND (name LIKE CONCAT('%', #{keyword}, '%')
OR email LIKE CONCAT('%', #{keyword}, '%'))
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
ORDER BY created_at DESC
</select>
</mapper>
Redis
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
配置
spring:
data:
redis:
host: localhost
port: 6379
password:
database: 0
timeout: 5000ms
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
使用 RedisTemplate
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// JSON 序列化
Jackson2JsonRedisSerializer<Object> serializer =
new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.activateDefaultTyping(
LazyLoadingMixin.getDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL));
serializer.setObjectMapper(mapper);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}
常用操作
@Service
@RequiredArgsConstructor
public class CacheService {
private final RedisTemplate<String, Object> redisTemplate;
// String 操作
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public void set(String key, Object value, long timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, value, timeout, unit);
}
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
// List 操作
public void push(String key, Object value) {
redisTemplate.opsForList().leftPush(key, value);
}
public Object pop(String key) {
return redisTemplate.opsForList().rightPop(key);
}
// Set 操作
public void add(String key, Object... values) {
redisTemplate.opsForSet().add(key, values);
}
public Set<Object> members(String key) {
return redisTemplate.opsForSet().members(key);
}
// Hash 操作
public void put(String key, String hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
public Object get(String key, String hashKey) {
return redisTemplate.opsForHash().get(key, hashKey);
}
// 删除
public void delete(String key) {
redisTemplate.delete(key);
}
public boolean expire(String key, long timeout, TimeUnit unit) {
return Boolean.TRUE.equals(redisTemplate.expire(key, timeout, unit));
}
}
使用 @Cacheable 注解
@Service
public class UserService {
@Cacheable(value = "users", key = "#id")
public User findById(Long id) {
// 缓存中有则直接返回,没有则执行方法
return userRepository.findById(id).orElseThrow();
}
@CacheEvict(value = "users", key = "#id")
public void delete(Long id) {
userRepository.deleteById(id);
}
@CachePut(value = "users", key = "#user.id")
public User update(User user) {
return userRepository.save(user);
}
@CacheEvict(value = "users", allEntries = true)
public void clearAll() {
// 清空 users 缓存
}
}
启用缓存:
@SpringBootApplication
@EnableCaching
public class MyApplication {}
MongoDB
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置
spring:
data:
mongodb:
uri: mongodb://localhost:27017/demo
# 或分别配置
# host: localhost
# port: 27017
# database: demo
使用
@Document(collection = "users")
@Data
public class User {
@Id
private String id;
private String name;
private String email;
private Integer age;
private List<String> tags;
private Address address;
private LocalDateTime createdAt;
}
@Data
public class Address {
private String province;
private String city;
private String detail;
}
public interface UserRepository extends MongoRepository<User, String> {
List<User> findByName(String name);
List<User> findByAddressCity(String city);
List<User> findByTagsIn(List<String> tags);
}
多数据源配置
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
spring:
datasource:
primary:
url: jdbc:mysql://localhost:3306/db1
username: root
password: 123456
secondary:
url: jdbc:mysql://localhost:3306/db2
username: root
password: 123456
练习
- 集成 Redis 实现对用户信息的缓存
- 使用
@Cacheable缓存查询结果,@CacheEvict在更新/删除时清除缓存 - 使用 Redis 实现一个简单的计数器
