Java电商秒杀项目(一)
项目环境搭建
Spring Boot环境的搭建
集成Thymeleaf Result结果搭建
集成Mybatis+Druid
集成Jedis+Redis安装+通用缓存Key封装
进行环境搭建,然后添加一定的依赖:
一、加入依赖pom.xml
依次Spring Boot依赖、thymeleaf依赖、 Mybatis、 mysql、 druid、 Jedis的依赖,如下还要添加thymelefa的依赖:
二、添加配置文件application.properties
spring.thymeleaf.prefix=Classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.enable=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
mybatis.type-aliases-package=com.example.domain.model
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
mybatis.mapper-locations=classpath:edu/cn/dao/*.xml
spring.datasource.url=jdbc:mysql://localhost:3306/mymiaosha?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters=stat
spring.datasource.maxActive=2
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
redis.host=localhost:3306
redis.port=6379
redis.timeout=3
redis.password=123456
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3
三、集成Thymeleaf
Thymeleaf是一个现代服务器端Java模板引擎,适用于Web和独立环境,主要目标是提供一种优雅且高度可维护的模板创建方式,它可以完全替代 JSP ,可以静态显示页面,也可以使页面动态显示。
四、集成mybatis+druid
1、controller代码
@Controller
@RequestMapping("/cute")
public class SampleController {
@Autowired
UserService userService;
@RequestMapping("/thy")
public String thymeleaf(Model model) {
model.addAttribute("name", "xiaoming");
return "Hello";
}
@RequestMapping("/")
@ResponseBody
public String home(){
return "Hello World!";
}
@RequestMapping("/hello")
@ResponseBody
public Result<String> hello(){
return Result.success("hello,小爱同学");
}
@RequestMapping("/heError")
@ResponseBody
public Result helloError(){
return Result.error(CodeMsg.SERVER_ERROR);
}
}
2、在UserService 里面注入userDao
3、dao层及pojo各代码的实现实现
四、集成Jedis+Redis
那jedis就是集成了redis的一些命令操作,封装了redis的java客户端。提供了连接池管理。一般不直接使用jedis,而是在其上在封装一层,作为业务的使用。