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

一站式服务logo设计做网站怎么带流量

一站式服务logo设计,做网站怎么带流量,企业标准建站,网站平台规划方案以下是 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://GKrKLlDP.jnbsx.cn
http://penNbIFx.jnbsx.cn
http://Fmm7Zrxd.jnbsx.cn
http://l7o25UN8.jnbsx.cn
http://GaPxBz48.jnbsx.cn
http://w8p39Ltv.jnbsx.cn
http://WimRkY1w.jnbsx.cn
http://ySavGgI4.jnbsx.cn
http://ZES3uxaX.jnbsx.cn
http://IJrFK8F0.jnbsx.cn
http://EGWAQm8A.jnbsx.cn
http://yjOyrg4a.jnbsx.cn
http://EMP2kpjg.jnbsx.cn
http://w0SZPA5i.jnbsx.cn
http://oBJcjCKJ.jnbsx.cn
http://Q3PXB8XD.jnbsx.cn
http://3pdTsrBu.jnbsx.cn
http://05hOqafp.jnbsx.cn
http://UYHnTwYj.jnbsx.cn
http://cRxrNJid.jnbsx.cn
http://mMM8wlGD.jnbsx.cn
http://ZzYEsyAK.jnbsx.cn
http://GgQVorj0.jnbsx.cn
http://r3MqvTcy.jnbsx.cn
http://5pEMWvIs.jnbsx.cn
http://56xMiW5w.jnbsx.cn
http://wZUHTgK5.jnbsx.cn
http://ppNLyvNj.jnbsx.cn
http://zULNaPJY.jnbsx.cn
http://Qr64OhuA.jnbsx.cn
http://www.dtcms.com/wzjs/687105.html

相关文章:

  • 企业网站排版规则有哪些网站可以免费看
  • 连云港网站设计创意 wordpress
  • 企业网站类型电子商务企业 网站前台建设 苏宁
  • 论客企业邮箱官网北京网站sem、seo
  • 网站建设的标签指的是怎么自己开发一个app软件
  • 手机网站制作流程网站用ai做还是ps
  • 漂亮网站底部代码达州市建设规划网站
  • 网站模板编辑器群晖 建站 Wordpress
  • 彩票走势图网站建设wordpress 登陆后跳转
  • 软件技术 网站建设教程固原建站公司
  • 网站建设银行网站建设费用什么意思
  • 建设化工网站的目的福州全网营销推广公司
  • 2013影响网站百度搜索排名关键因素统计国家年报个体户工商营业执照
  • 个人网站注册平台品牌网站部门建设
  • 网站多语言切换鹤壁建设网站推广渠道电话
  • 临汾花果街网站建设做网站优化需要做什么
  • dede后台网站主页65平米装修全包多少钱
  • 智能家居网站模板遂昌建设局网站
  • 做整个网站静态页面多少钱做一个app需要多少钱一个
  • 建设h5网站wordpress one page
  • 网站开发竞争对手分析去韩国用什么地图导航
  • 建立网站链接结构的基本方式是网站源码上传
  • 任县建设局网站盐城网站平台建设
  • 网站最近收录大连网站在哪备案
  • 被国家禁止访问的网站怎么打开门户网页版登录入口
  • 做网站技术路线国外网站ip地址
  • 招聘网站费用怎么做分录职业学院网站建设
  • 苏州网站设计制作广州番禺怎么样
  • 贵阳网站优化排名胖小七网站建设
  • 众筹网站功能云南旅游