导航:从零开始手写mmo游戏从框架到爆炸(零)—— 导航-CSDN博客
集成springboot-jpa,不用mybatis框架一个是方便对接不同的数据源。第二个目前规划的游戏内容可能对数据库的依赖不是很大,jpa应该肯定能满足要求了。我们先创建一个用户表方便后续注册和登录的功能开发。
在eternity-server中引入依赖:
<!--jpa start--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
创建实体:
package com.loveprogrammer.domain;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import javax.persistence.*;/*** @ClassName User* @Description TODO* @Author admin* @Date 2024/2/6 17:00* @Version 1.0*/
@Data
@AllArgsConstructor
@NoArgsConstructor
//使用JPA注解配置映射关系
@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "tbl_user") //@Table来指定和哪个数据表对应;如果省略默认表名就是user;
public class User {@Id //这是一个主键@GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键private Integer id;@Column(name = "nick_name", length = 50)private String nickName;@Column(name = "account", length = 50)private String account;@Column(name = "password", length = 50)private String password;@Column() //省略默认列名就是属性名private String email;}
启动后自动建表:
Hibernate: create table tbl_user (id integer not null auto_increment, account varchar(50), email varchar(255), nick_name varchar(50), password varchar(50), primary key (id)) engine=InnoDB
Dao,Service如下:
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
public interface IUserService {List<User> getUserList();User findUserById(Integer id);void save(User user);void edit(User user);void delete(Integer id);
}
@Service
public class UserServiceImpl implements IUserService {@Autowiredprivate UserRepository userRepository;@Overridepublic List<User> getUserList() {return userRepository.findAll();}@Overridepublic User findUserById(Integer id) {Optional<User> user = userRepository.findById(id);return user.orElse(null);}@Overridepublic void save(User user) {userRepository.save(user);}@Overridepublic void edit(User user) {userRepository.save(user);}@Overridepublic void delete(Integer id) {userRepository.deleteById(id);}
}
整体结构如下:
全部源码详见:
gitee : eternity-online: 多人在线mmo游戏 - Gitee.com
分支:step-07