基于IDEA采用springboot+Mybatis搭建ssm框架简单demo项目的搭建配置流程
一、通过对比可以原始SSM搭建流程,spring boot省去了大量的配置,极大提高了开发者的效率。原始SSM框架搭建流程见博客:
https://www.cnblogs.com/No2-explorer/p/10993868.html
二、下面开展springboot的快速入门之旅吧!
1、创建数据库和em_info(示例员工信息表格)
DROP TABLE IF EXISTS `em_info`; CREATE TABLE `em_info` ( `em_id` INT(50) NOT NULL AUTO_INCREMENT COMMENT '员工ID', `em_name` VARCHAR(100) NOT NULL COMMENT '员工姓名', `em_sex` VARCHAR(30) NOT NULL COMMENT '性别', `em_birthday` DATE NOT NULL COMMENT '出生日期', `em_hiredate` DATE NOT NULL COMMENT '入职日期', `em_salary` DOUBLE NOT NULL COMMENT '工资', PRIMARY KEY (`em_id`) )ENGINE=INNODB DEFAULT CHARSET=utf8mb4; INSERT INTO `em_info` VALUES (NULL,'小李飞刀','男','1999-2-8','2019-6-6','9999');
2、通过spring Initializr创建springboot
选择web、Template Engines、SQL的选项见上图右侧
配置完成后,生成下图相关文件
3、sources包下默认配置文件application.properties,可以根据个人喜好删除application.properties后,更改application.yml配置文件
编写yml配置:(主要是数据库相关配置)
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.232.128:3306/ssm username: root password: root
4、
分别创建controller、service、mapper三层架构,编写相关接口和实现类,pojo类。如下图:
4.1controller层
/** * Em_infoController class * * @author fzj * @date 2019/6/9 */ @RestController //@RestContller返回json格式不能用于页面提取数据,如果需要返回数据给页面则使用@Controller注释 @RequestMapping("/em_info") public class Em_infoController { @Autowired private IEm_infoService em_infoService; @RequestMapping("/findAll.do") public List<Em_info> findAll() throws Exception{ List<Em_info> em_infoList = em_infoService.findAll(); return em_infoList; } }
4.2、Service层没有业务逻辑,直接接口和实现类调用方法即可。
public interface IEm_infoService { List<Em_info> findAll() throws Exception; }
service实现类
@Service public class Em_infoServiceImpl implements IEm_infoService{ @Autowired private IEm_infoMapper em_infoMapper; @Override public List<Em_info> findAll() throws Exception { return em_infoMapper.findAll(); } }
4.3、mapper层接口
public interface IEm_infoMapper { @Select("select * from em_info") List<Em_info> findAll() throws Exception; }
4.4、Pojo类:
package com.fzj.springboot_ssm.pojo; import org.springframework.context.annotation.Bean; import java.util.Date; /** * Em_info class * * @author fzj * @date 2019/6/9 */ public class Em_info { private Integer em_id; private String em_name; private String em_sex; private Date em_birthday; private Date em_hiredate; private Double em_salary; public Integer getEm_id() { return em_id; } public void setEm_id(Integer em_id) { this.em_id = em_id; } public String getEm_name() { return em_name; } public void setEm_name(String em_name) { this.em_name = em_name; } public String getEm_sex() { return em_sex; } public void setEm_sex(String em_sex) { this.em_sex = em_sex; } public Date getEm_birthday() { return em_birthday; } public void setEm_birthday(Date em_birthday) { this.em_birthday = em_birthday; } public Date getEm_hiredate() { return em_hiredate; } public void setEm_hiredate(Date em_hiredate) { this.em_hiredate = em_hiredate; } public Double getEm_salary() { return em_salary; } public void setEm_salary(Double em_salary) { this.em_salary = em_salary; } @Override public String toString() { return "Em_info{" + "em_id=" + em_id + ", em_name='" + em_name + '\'' + ", em_sex='" + em_sex + '\'' + ", em_birthday=" + em_birthday + ", em_hiredate=" + em_hiredate + ", em_salary=" + em_salary + '}'; } }
5、最后一把尤其关键,需要在springbooot启动类添加注解,否则springboot扫描不到mapper包
6、到此,项目已经配置完成!
点击IDEA绿色小三角形,运行项目,浏览器输入访问路径http://localhost:8080/em_info/findAll.do
7、
页面就显示json格式返回结果:
Sqlyog查询结果对比