前言

笔者在校用SSM较多,一直是知道SpringBoot的,同时也知道SSM学好boot报错才容易发现问题,为了效率还是稍微花了点时间用到了boot,虽然说复制SSM配置模板(直接导以前搭配好的项目然后修改)也是快速开发,boot其实也是基于这个理念的原理。会SSM用boot是分分钟的事情,我也只是稍微入个boot工具的使用门——搭建了SSM同等效力的基本整合框架。blog写的可能随意一点,以往字斟句酌的文章着实费时费力,新的尝试吧。学习时间不多,可能在扩展方面讲的不对,欢迎指出。

SpringBoot的安装

笔者用的惯的编译软件是开源的eclipse(工具的种类不用在意),进入在此下载(往上翻,网页定位默认在下方),当然你不想多下载一个编译器,也可以采用安装插件的形式安装。

下好之后是一个jar包,直接解压到相对应的文件夹即可。

将ssm框架系统升级为springboot系统 ssm框架可以用springboot吗_快速入门

将ssm框架系统升级为springboot系统 ssm框架可以用springboot吗_快速入门_02

.exe就是springboot版本的eclipse了,值得注意的是笔者发现使用存在两点问题(虽然好用):1、包导入的自动补全功能要自己设置:windows->perferences->java->editor->save action勾选Organize imports。2、代码悬停错误提示功能无效,只能点击到错误代码按ctrl+1来使用了,windows->perferences->java->editor->hover该勾选的都选了。

将ssm框架系统升级为springboot系统 ssm框架可以用springboot吗_spring boot_03

将ssm框架系统升级为springboot系统 ssm框架可以用springboot吗_spring boot_04

创建第一个hello项目

直接新建一个spring quick start项目,觉得慢可以替换为阿里的:https://start.aliyun.com

将ssm框架系统升级为springboot系统 ssm框架可以用springboot吗_mybatis_05

选择你预装pom文件里面的jar包,勾选即可。

将ssm框架系统升级为springboot系统 ssm框架可以用springboot吗_html_06

SpringBoot是Spring全家桶的集大成者,Boot在这里是快速开发的意思。免去了很多的xml文件的配置,也就是环境的搭建,距离来说:视图解析器等等。那么我们来说说,他是这么实现的呢——通过默认的方式,还是以视图解析器为例子,系统默认搭配好的视图解析器的路径为:

# 默认路径
spring.thymeleaf.prefix=classpath:/templates/

也就是说不需要你自己调配,他有个默认好的模板(quick start项目)。与此同理的还有pom文件,创建项目的时候会有:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.0.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
         and so on~
	</dependencies>

注意到,预装的依赖并没有版本号。那他的版本呢?在parent里面有设置(使用parent的版本,当然你可以自己手动添加依赖)。

建立好后会发现java文件夹和resource文件夹,resource下面有sattic、teplates分别是模板默认放置资源和html文件的地方。

注意到有一个默认的主程序类,笔者这里的包名修改了本来应该是com.example.demo。这里是新入门困惑最多的地方!为什么呢?springboot虽然是模板,他的扫描机制便是根据这个主程序类启动来定位的,与DemoApplication.java同级或者子级的包内/文件才能被扫描到(注解比如:@service@controller)。理解这点,这个类完全不用动。不像我,整合dao和service扫描不到——配置dao或者service失败,报错:Servlet.service() for servlet [dispatcherServlet] in context with path [] th。然后让你自己定义bean,他其实就是没扫描到嘛。

添加了@MapperScan和@ComponentScan(basePackages = "com.example.*")那玩意儿也是没啥子用,dao层可以直接用了,service死活不行。

将ssm框架系统升级为springboot系统 ssm框架可以用springboot吗_spring boot_07

完整的包结构,讲主程序的包拔高一个层次是这个原因,当然我感觉是不规范的(不过我觉得挺好哈哈哈哈哈):

将ssm框架系统升级为springboot系统 ssm框架可以用springboot吗_mybatis_08

接下来就跟SSM没什么区别了,直接贴代码了。

应用配置:application.properties:

相当于application.xml,记得在数据源的url配置加上时区设置噢~

##############################################
#
# thymeleaf静态资源配置
#
##############################################
# 默认路径
spring.thymeleaf.prefix=classpath:/templates/
# 后缀
spring.thymeleaf.suffix=.html
# 模板格式
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

##############################################
#
# 数据库dataSource配置
#
##############################################
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ireader_admin?useUnicode=ture&characterEncoding=UTF-8&serverTimezone=GMT%2B8&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

Controller

@Controller
public class DemoController {
	@Autowired 
	UserService userService;
	
	@RequestMapping("/hello")
	public String hello(Model model)  {
		System.out.println("进入到helloController");
	
		model.addAttribute("test",userService.getUserById(1).getUname());
		System.out.println(userService.getUserById(1).toString());
		 
		return "hello";
	}
}

Service以及impl

public interface UserService {
	UserInfo getUserById(int uid);
}
@Service
public class UserServiceImpl implements UserService{
	@Autowired 
	UserDao userDao;

	@Override
	public UserInfo getUserById(int uid) {
		// TODO Auto-generated method stub
		return (UserInfo)userDao.getUserById(uid);
	}
}

 Domain层(pojo)

注意序列化,不写全了。

public class UserInfo implements Serializable{
	private int uid;
	private String uname;
	private String usex;
	private String uphone;
	private String uemail;
	private String uaddress;
	private Date ucreate_time;
	private String ustate;
	SimpleDateFormat format  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public int getUid() {
		return uid;
	}

Dao层

用注解的方式,他们也叫Mapper层。

@Select("               这里写动态sql语句(对应原本 mapper.cml里面的sql)      ")

下面是实现的对应动态sql注入的Dao方法。

@Mapper
public interface UserDao{
	@Select("select * from user_info where uid = #{uid}")
	UserInfo getUserById(int uid);
}

写在最后

SpringBoot还有很多功能和改变我这里并没有用到,所用的注解也均为SMM的老注解了,他有很多新的注解,比如说@RestController(好像是吧,裸打的)集合了@ResponseBodyh和@RequestMapping等等,同时也可以使用yml配置和配置类等等,目前我是没去系统了解,后续如果有继续使用可以慢慢在使用中了解。

ctrl + 1实现接口类的方法填充。

2021.11.10

最近工作要根据原始需求写出一个功能模块,新建了一个boot项目,再次使用thyleaf的时候发现不能够前端直接跳转页面(前后端分离和大数据做多了...),每次写一个contoller单独转发页面也太累了。我们可以同一进行视图转发:

/**
*@author cbry
*date: 2021/11/9
 *  thymeleaf跳转页面
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 视图映射
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //registry相当于viewcontroller的注册中心,想让哪些请求跳到哪些页面,在这里注册就行了
        registry.addViewController("/templateUp.html").setViewName("templateUp");//添加视图控制器,第一个参数urlPath是请求地址等同于requestMapping的地址。第二个参数viewName是视图名,也就是原来controller中return的页面的名。
        registry.addViewController("/templateList.html").setViewName("templateList");
        registry.addViewController("/templateUp").setViewName("templateUp");//添加视图控制器,第一个参数urlPath是请求地址等同于requestMapping的地址。第二个参数viewName是视图名,也就是原来controller中return的页面的名。
        registry.addViewController("/templateList").setViewName("templateList");
    }
}