当前位置: 首页 > wzjs >正文

萝卜建站分类信息seo整站优化

萝卜建站分类信息,seo整站优化,php网站代做是什么意思,专业的网站制作中心以下是 Spring Boot MySQL MyBatis&#xff08;注解和XML配置两种方式&#xff09;集成Redis的完整启用及配置详解&#xff0c;包含代码示例、注释说明和表格总结&#xff1a; 1. 添加依赖 在pom.xml中添加Spring Boot对MySQL、MyBatis和Redis的支持依赖&#xff1a; <d…

以下是 Spring Boot + MySQL + MyBatis(注解和XML配置两种方式)集成Redis的完整启用及配置详解,包含代码示例、注释说明和表格总结:


1. 添加依赖

pom.xml中添加Spring Boot对MySQL、MyBatis和Redis的支持依赖:

<dependencies><!-- Spring Boot核心 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- MySQL驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><!-- MyBatis集成 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.1</version></dependency><!-- Redis和缓存 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- JSON序列化依赖 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
</dependencies>

2. 配置MySQL和Redis

application.properties中配置数据库和Redis信息:

# MySQL配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# Redis配置
spring.redis.host=localhost
spring.redis.port=6379# 缓存配置
spring.cache.type=redis
spring.cache.redis.key-prefix=myapp_
spring.cache.redis.time-to-live=3600000 # 全局缓存过期时间(1小时)

3. 数据库表结构(MySQL)

CREATE TABLE users (id BIGINT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(255) NOT NULL,age INT
);

4. 实体类(User)

public class User {private Long id;private String name;private Integer age;// 构造函数、Getter/Setter省略
}

5. MyBatis Mapper配置(两种方式)

方式1:注解方式
import org.apache.ibatis.annotations.*;
import java.util.List;@Mapper
public interface UserMapper {@Select("SELECT * FROM users WHERE id = #{id}")User selectUserById(Long id);@Update("UPDATE users SET name=#{name}, age=#{age} WHERE id=#{id}")void updateUser(User user);@Delete("DELETE FROM users WHERE id=#{id}")void deleteUserById(Long id);
}
方式2:XML配置
  • 创建XML文件src/main/resources/mapper/UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper"><select id="selectUserById" resultType="User">SELECT * FROM users WHERE id = #{id}</select><update id="updateUser">UPDATE users SET name=#{name}, age=#{age} WHERE id=#{id}</update><delete id="deleteUserById">DELETE FROM users WHERE id=#{id}</delete>
</mapper>
  • 配置MyBatis扫描路径:在application.properties中添加:
    mybatis.mapper-locations=classpath:mapper/*.xml
    

6. 自定义Redis缓存配置

创建配置类以自定义Redis的序列化方式和缓存行为:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@Configuration
public class RedisConfig {@Beanpublic RedisCacheConfiguration redisCacheConfiguration() {return RedisCacheConfiguration.defaultCacheConfig()// 键序列化器为String类型.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))// 值序列化器为JSON类型.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))// 默认缓存过期时间(覆盖全局配置).entryTtl(Duration.ofMinutes(30));}
}

7. Service层集成缓存

在Service层使用@Cacheable@CachePut等注解,结合MyBatis查询:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;// 1. 缓存查询用户的结果@Cacheable(value = "userCache", key = "#id")public User getUserById(Long id) {System.out.println("从数据库查询用户ID:" + id);return userMapper.selectUserById(id);}// 2. 更新用户信息并更新缓存@CachePut(value = "userCache", key = "#user.id")public User updateUser(User user) {System.out.println("更新用户缓存:" + user.getId());userMapper.updateUser(user);return user;}// 3. 删除指定用户的缓存@CacheEvict(value = "userCache", key = "#id")public void deleteUserById(Long id) {System.out.println("删除用户缓存:" + id);userMapper.deleteUserById(id);}
}

8. Controller层示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/api/users")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/{id}")public User getUser(@PathVariable Long id) {return userService.getUserById(id);}@PutMapping("/update")public User updateUser(@RequestBody User user) {return userService.updateUser(user);}@DeleteMapping("/{id}")public void deleteUser(@PathVariable Long id) {userService.deleteUserById(id);}
}

9. 关键配置与注解总结

模块配置/注解作用示例
依赖mybatis-spring-boot-starter集成MyBatis与Spring Bootpom.xml添加依赖
数据库配置spring.datasource.*配置MySQL连接信息spring.datasource.url=jdbc:mysql://...
Redis配置spring.redis.*配置Redis服务器地址和端口spring.redis.host=localhost
MyBatis注解方式@Mapper标识MyBatis接口映射@Mapper
MyBatis XML方式mybatis.mapper-locations指定XML映射文件路径classpath:mapper/*.xml
缓存管理@EnableCaching启用Spring缓存注解支持主类添加注解
缓存注解@Cacheable缓存方法返回结果,避免重复数据库查询@Cacheable(value = "userCache", key = "#id")
更新缓存@CachePut更新缓存而不影响方法执行(如更新用户信息)@CachePut(value = "userCache", key = "#user.id")
清除缓存@CacheEvict删除指定缓存或全部缓存(如删除用户后清除对应缓存)@CacheEvict(value = "userCache", key = "#id")

10. 注意事项

  1. Mapper配置

    • 注解方式:需在启动类或配置类上添加@MapperScan("com.example.mapper")指定包路径。
    • XML方式:需在application.properties中配置mybatis.mapper-locations
  2. 序列化

    • 默认使用JdkSerializationRedisSerializer,若需JSON序列化需自定义配置(如GenericJackson2JsonRedisSerializer)。
  3. 缓存键设计

    • 确保缓存键唯一且可读,如使用#id动态生成键。
    • 可通过keyGenerator自定义键生成逻辑。
  4. 事务管理

    • 对于数据库操作,需结合@Transactional注解确保数据一致性。

通过以上步骤,可实现Spring Boot + MySQL + MyBatis(注解或XML配置)与Redis的高效集成,利用缓存减少数据库压力,提升系统性能。

http://www.dtcms.com/wzjs/578834.html

相关文章:

  • 建设手机网站费用在线室内设计工具
  • 饰品做商城网站模式电脑网站 源码
  • 淄博网站制作定制品牌住宅小区物业管理系统网站建设
  • 响应式网站建设福州建设主管部门官方网站
  • 九江网站设计服务机构哪家好设计学校网站模板
  • 上海建设银行黄浦区营业网站网站开发参考书籍
  • seo 网站标题长度泰州做企业网站
  • c2c的电子商务网站有哪些浙江网站推广公司
  • 智能网站建设背景上海商标注册
  • 企业网站建设 骆诗设计网站建设类
  • 低价网站制作网站建设需要哪些信息
  • 企业品牌形象设计杭州seo首页优化软件
  • jsp网站开发 心得网站栏目模块
  • 域名怎么和网站绑定湖南建设网塔吊证查询
  • 外贸在线网站建站租车行网站模版
  • 鞍山做网站排名网店网站技术方案
  • 推广网站设计推广方案网络公司什么意思
  • 个人购物网站备案制作wordpress模板教程
  • 婚庆网站怎么设计模板广州抖音seo价格
  • 著名设计师网站有经验的郑州网站建设
  • wordpress全站伪静态南沙网站开发
  • 沧州兼职网站建设WordPress如何去掉文章时间
  • 公主岭网站开发北京终端区优化
  • 长沙网站排名优化搜索引擎优化的五个方面
  • 网站源码绑定域名网站建设的五类成员
  • 太原网站建设 网站制作深圳网站制作建设服务公司
  • 收费下载网站cms网页制作软件是应用软件吗
  • 网站一键提交做国外网站选择vps
  • 一学一做腾讯视频网站百度收录批量查询工具
  • 做营销型网站要多少钱展厅设计ppt优秀案例分析