SpringBoot Spring SpringMVC MyBatis Redis Kakfa Elasticsearch Spring Security Spring Actator
在Spring Boot Initializr中或者Idea中初始化一个SpringBoot项目并导出
使用Idea打开导出的项目
各个层之间的关系如下
在Maven Repository搜索MySql Maven配置文件,在resources文件包内的pom.xml文件中导入相关的配置文件依赖,并在application.properties文件中配置相关的参数。
# ServerProperties
server.port=8080
server.servlet.context-path=/community
# ThymeleafProperties
spring.thymeleaf.cache=false
# DataSourceProperties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
#数据库的名称、密码等
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#最大连接数、超时时间等
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=30000
# MybatisProperties
#mapper扫描路径
mybatis.mapper-locations=classpath:mapper/*.xml
#在communtiy下创建实体类
mybatis.type-aliases-package=com.nowcoder.community.entity
mybatis.configuration.useGeneratedKeys=true
mybatis.configuration.mapUnderscoreToCamelCase=true
在community文件下创建config entity文件包,在resources文件下创建mapper文件包
在entity文件下创建User类
public class User {
private int id;
private String username;
private String password;
private String salt;
private String email;
private int type;
private int status;
private String activationCode;
private String headerUrl;
private Date createTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getActivationCode() {
return activationCode;
}
public void setActivationCode(String activationCode) {
this.activationCode = activationCode;
}
public String getHeaderUrl() {
return headerUrl;
}
public void setHeaderUrl(String headerUrl) {
this.headerUrl = headerUrl;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
", salt='" + salt + ''' +
", email='" + email + ''' +
", type=" + type +
", status=" + status +
", activationCode='" + activationCode + ''' +
", headerUrl='" + headerUrl + ''' +
", createTime=" + createTime +
'}';
}
}
在dao文件下创建UserMapper接口访问数据库
@Mapper
public interface UserMapper {
User selectById(int id);
User selectByName(String username);
User selectByEmail(String email);
int insertUser(User user);
int updateStatus(int id, int status);
int updateHeader(int id, String headerUrl);
int updatePassword(int id, String password);
}
使用Mapper注解,并在mapper文件下创建user-mapp.xml,使得方法与Sql语句相关联,Mybatis 的xml配置可以在官网找到相关的配置。
username, password, salt, email, type, status, activation_code, header_url, create_time
id, username, password, salt, email, type, status, activation_code, header_url, create_time
insert into user ( )
values(#{username}, #{password}, #{salt}, #{email}, #{type}, #{status}, #{activationCode}, #{headerUrl}, #{createTime})
update user set status = #{status} where id = #{id}
update user set header_url = #{headerUrl} where id = #{id}
update user set password = #{password} where id = #{id}
可以使用@Test注解测试相关方法是否正常使用
在Entity创建DiscussPost类,用于表示发送的相关数据,并在dao文件中创建DiscussPostMapper接口
public class DiscussPost {
private int id;
private int userId;
private String title;
private String content;
private int type;
private int status;
private Date createTime;
private int commentCount;
private double score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public int getCommentCount() {
return commentCount;
}
public void setCommentCount(int commentCount) {
this.commentCount = commentCount;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "DiscussPost{" +
"id=" + id +
", userId=" + userId +
", title='" + title + ''' +
", content='" + content + ''' +
", type=" + type +
", status=" + status +
", createTime=" + createTime +
", commentCount=" + commentCount +
", score=" + score +
'}';
}
}
@Mapper
public interface DiscussPostMapper {
// 考虑到后期分页功能加入offset 和 limit变量
// @Param注解用于给参数取别名,
// 如果只有一个参数,并且在里使用,则必须加别名.
List selectDiscussPosts(int userId, int offset, int limit);
int selectDiscussPostRows(@Param("userId") int userId);
}
在mapper文件下创建相关的xml文件用于数据库操作
id, user_id, title, content, type, status, create_time, comment_count, score
在service文件下创建DiscussPostService类,用于服务层使用(使用@Service注解)
@Service
public class DiscussPostService {
@Autowired
private DiscussPostMapper discussPostMapper;
public List findDiscussPosts(int userId, int offset, int limit) {
return discussPostMapper.selectDiscussPosts(userId, offset, limit);
}
public int findDiscussPostRows(int userId) {
return discussPostMapper.selectDiscussPostRows(userId);
}
}
将静态资源(css image js等文件)放到static文件下,将模板文件(site index.html)放到templates文件下。
接下来开发视图层,新建一个HomeController在controller文件下使用@Controller注解
//Controller访问路径可以省略
@Controller
public class HomeController {
//注入对象
@Autowired
private DiscussPostService discussPostService;
@Autowired
private UserService userService;
//使用GET方法
@RequestMapping(path = "/index", method = RequestMethod.GET)
List list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());
List
更改Index.html文件,使用Thymeleaf对其中相对路径进行更改,并显示相关的帖子。
-
备战春招,面试刷题跟他复习,一个月全搞定!
置顶
精华
寒江雪 发布于 2019-04-15 15:32:18
- 赞 11
- |
- 回帖 7
完整的HTML文件如下:
牛客网-首页