在这个项目里我用的是 springboot
的2版本,ORM选用 JPA
快速开发,JSON工具使用阿里的 fastjson
,当然,mq用的是 rabbitMQ
。导入的是 springboot
集成的依赖。
1. 配置部分
1.1 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
1.2 application.properties
server.port=10000
spring.datasource.url=jdbc:mysql://xxxxx/xxxxx?characterEncoding=utf-8
spring.datasource.username=xxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
spring.rabbitmq.host=localhost
spring.rabbitmq.username=root
spring.rabbitmq.password=root
spring.rabbitmq.port=5672
我只是很有针对性的对 mq
和 datasource
进行了配置。
1.3 数据表
create table if not result
(
id int auto_increment primary key,
ticket_id int null,
user_id int null
);
create table if not exists ticket
(
id int auto_increment primary key,
name varchar(255) null,
content varchar(255) null,
user_name varchar(20) null,
count int default '6666' not null
);
根据数据表可以Generate出JavaBean,不贴JavaBean了。 ##### 1.4 项目架构
├── src│ ├── main│ │ ├── java│ │ │ └── com│ │ │ └── fantj│ │ │ └── springbootjpa│ │ │ ├── AMQP.java│ │ │ ├── controller│ │ │ │ └── TicketController.java│ │ │ ├── mq│ │ │ │ ├── Message.java│ │ │ │ ├── MQConstants.java│ │ │ │ ├── MQReceiver.java│ │ │ │ └── MQSender.java│ │ │ ├── pojo│ │ │ │ ├── Result.java│ │ │ │ └── Ticket.java│ │ │ ├── repostory│ │ │ │ ├── ResultRepository.java│ │ │ │ └── TicketRepository.java│ │ │ └── service│ │ │ ├── ResultServiceImpl.java│ │ │ ├── ResultService.java│ │ │ ├── TicketServiceImpl.java│ │ │ └── TicketService.java│ │ └── resources│ │ ├── application.properties│ │ └── rebel.xml
2. 启动类
@SpringBootApplication@EntityScan("com.fantj.springbootjpa.pojo")@EnableRabbitpublic class AMQP { public static void main(String[] args) { SpringApplication.run(AMQP.class, args); }}
注意这个 @EnableRabbit
注解,它会开启对rabbit注解的支持。
3. controller
很简单的一个controller类,实现查询和抢票功能。
@RestController
@RequestMapping("/ticket")
public class TicketController {
@Autowired
private TicketService ticketService;
@Autowired
private MQSender mqSender;
@RequestMapping("/get/{id}")
public Ticket getByid(@PathVariable Integer id){
return ticketService.findById(id);
}
@RequestMapping("/reduce/{id}/{userId}")
public String reduceCount(@PathVariable Integer id,
@PathVariable Integer userId){
Message message = new Message(id,userId);
ticketService.reduceCount(id);
mqSender.sendMessage(new Message(message.getTicketId(),message.getUserId()));
return "抢票成功!";
}
}
注意 privateMQSendermqSender;
这是我的 rabbit
发送消息的类。
4. Service
接口我就不再这里贴出,直接贴实现类。
4.1 ResultServiceImpl.java
@Service
public class ResultServiceImpl implements ResultService{
@Autowired
private ResultRepository resultRepository;
@Override
public void add(Result result) {
resultRepository.add(result.getTicketId(), result.getUserId());
}
@Override
public Result findOneByUserId(Integer userId) {
return resultRepository.findByUserId(userId);
}
}
4.2 TicketServiceImpl.java
@Service
public class TicketServiceImpl implements TicketService{
@Autowired
private TicketRepository repository;
@Override
public Ticket findById(Integer id) {
return repository.findTicketById(id);
}
@Override
public Ticket reduceCount(Integer id) {
repository.reduceCount(id);
return findById(id);
}
}
这两个都是很普通的service实现类,没有新加入的东西。
5. Dao
5.1 ResultRepository.java
@Repository
public interface ResultRepository extends JpaRepository<Result,Integer> {
@Transactional
@Modifying
@Query(value = "insert into result(ticket_id,user_id) values(?1,?2) ",nativeQuery = true)
void add(@Param("ticketId") Integer ticketId,@Param("userId") Integer userId);
Result findByUserId(Integer userId);
}
5.2 TicketRepository.java
@Repositorypublic interface TicketRepository extends JpaRepository<Ticket,Integer>{ /** * 减少库存 */ @Transactional @Modifying @Query(value = "update ticket t set t.count=t.count+(-1) where id=?1",nativeQuery = true) int reduceCount(Integer id); /** * 查询信息 */ Ticket findTicketById(Integer id);}
到了这里,你会发现,md哪里有用mq的痕迹...
共同学习,写下你的评论
评论加载中...
作者其他优质文章