SpringBoot实现SSM(附上例子)
程序员文章站
2022-03-23 09:10:41
注重版权,转载请注明原作者和原文链接作者:码农BookSea原文链接:先看后赞,养成习惯。点赞收藏,人生辉煌。最近在学SpringBoot,刚好今天听老师讲了SpringBoot来实现SSM,就打算写篇博客跟大家做个分享。我的项目架构如图:新建项目因为我们要建的是SSM ,所以大家这3个选项选上Mybatis不好找的话就直接搜索框打字搜索,如图:新建项目好之后,记得配置下JDK版本 以及Maven的路径(重点!)如下2张图:本次项目一共写了7个文件,文件名见图:....
- 注重版权,转载请注明原作者和原文链接
作者:码农BookSea
原文链接:https://blog.csdn.net/bookssea/article/details/107387152
先看后赞,养成习惯。
点赞收藏,人生辉煌。
最近在学SpringBoot,刚好今天听老师讲了SpringBoot来实现SSM,就打算写篇博客跟大家做个分享。
我的项目架构如图:
新建项目
因为我们要建的是SSM ,所以大家这3个选项选上
Mybatis不好找的话就直接搜索框打字搜索,如图:
新建项目好之后,记得配置下JDK版本 以及Maven的路径(重点!)
如下2张图:
本次项目一共写了7个文件,文件名见图:
其他文件无变动。
首先先在pom.xml添加所需依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Maven会去帮我们下载。
写实体类User:
package com.etc.entity;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
private Integer age;//年龄
private String userRoleName; //用户角色名称
private String picUrl;
private String idPic;
public String getIdPic() {
return idPic;
}
public void setIdPic(String idPic) {
this.idPic = idPic;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public void setAge(Integer age) {
this.age = age;
}
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getAge() {
/*long time = System.currentTimeMillis()-birthday.getTime();
Integer age = Long.valueOf(time/365/24/60/60/1000).IntegerValue();*/
Date date = new Date();
Integer age = date.getYear()-birthday.getYear();
return age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
写application.properties:
server.port=7878
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/smbms?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.etc.entity
要在数据库里建一张表名为 smbms的表(Mysql),字段的话跟我上面写的User类字段一致就行,账号密码不一致的,自行修改。
(端口号大家可以修改,我的是7878)
写dao层,UserMapper:
package com.etc.dao;
import com.etc.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
@Repository
@Mapper
public interface UserMapper {
public User getUserById(@Param("id")String id);
}
UserService:
package com.etc.controller;
import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserService userService;
@RequestMapping("getUser")
public User getUserById(String id) throws Exception {
return userService.getUserById(id);
}
}
UserServiceImpl:
package com.etc.service;
import com.etc.dao.UserMapper;
import com.etc.entity.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements UserService{
@Resource
private UserMapper userMapper;
@Override
public User getUserById(String id){
User user = null;
try{
user=userMapper.getUserById(id);
}catch(Exception e){
user=null;
e.getStackTrace();
}
return user;
}
}
最后一个类是前端控制器UserController:
package com.etc.controller;
import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserService userService;
@RequestMapping("getUser")
public User getUserById(String id) throws Exception {
return userService.getUserById(id);
}
}
白嫖不好,创作不易。各位的点赞就是我创作的最大动力,如果我有哪里写的不对,欢迎评论区留言进行指正,我们下篇文章见!
老铁,如果有收获,请点个免费的赞鼓励一下博主
本文地址:https://blog.csdn.net/bookssea/article/details/107387152