赞
踩
配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili
【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili
spring
springmvc
mybatis
spring springmvc mybatis 简单功能 员工添加 查询 所有
# 项目 - 需求分析 概要设计(库表设计) 详细设计(验证库表正确性) 编码(环境搭建+业务代码) 测试 部署上线 # 员工添加 查询所有功能 SSM - 库表 库: ssm 数据库:mysql 表: id name birthday salary drop database if exists ssm; create database if not exists ssm; use ssm; drop table if exists emp; create table if not exists emp( id int(11) not null primary key auto_increment, name varchar(40), birthday timestamp, salary double(10,2) )engine=innodb default charset=utf8mb4; # 编码 环境搭建新建Maven-webapp项目 ssm:spring springmvc 一个团队开发 无缝整合 - springmvc spring mybatis
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.3.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.6</version> </dependency> <!-- springmvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.6</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.4</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!-- mybatis-spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- jackson 转换json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.3</version> </dependency> <!-- aspectj aop切面 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency> <!--aspectj--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.5</version> </dependency>
注意:spring相关依赖要保证版本号一致
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--开启注解扫描--> <context:component-scan base-package="com.baizhi.service"/> <!--创建数据源--> <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!--创建sqlSessionFactory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> <!--mapper配置文件位置--> <property name="mapperLocations" value="classpath:com/baizhi/mapper/*.xml"/> <!--实体别名 类名或类名小写均可 --> <property name="typeAliasesPackage" value="com.baizhi.entity"/> </bean> <!--创建DAO--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.baizhi.dao"/> </bean> <!--创建事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--开启注解事务生效 @Transactional--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
package com.baizhi.entity; import com.fasterxml.jackson.annotation.JsonFormat; import java.util.Date; public class Emp { private Integer id; private String name; @JsonFormat(pattern = "yyyy-MM-dd")//json格式日期转换,只对日期生效 private Date birthday; private Double salary; public Emp() { } public Emp(Integer id, String name, Date birthday, Double salary) { this.id = id; this.name = name; this.birthday = birthday; this.salary = salary; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + '\'' + ", birthday=" + birthday + ", salary=" + salary + '}'; } }
package com.baizhi.dao; import com.baizhi.entity.Emp; import java.util.List; public interface EmpDAO { //保存 void save(Emp emp); //查询所有 List<Emp> findAll(); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.baizhi.dao.EmpDAO"> <!--保存 useGeneratedKeys="true" keyProperty="id"仅对mysql数据库有效, 可进行主键自增,插入数据时直接插入null值即可 --> <insert id="save" parameterType="Emp" useGeneratedKeys="true" keyProperty="id"> insert into emp values (#{id}, #{name}, #{birthday}, #{salary}) </insert> <!--查询所有--> <select id="findAll" resultType="Emp"> select id,name,birthday,salary from emp </select> </mapper>
package com.baizhi.service; import com.baizhi.entity.Emp; import java.util.List; public interface EmpService { void save(Emp emp); List<Emp> findAll(); }
package com.baizhi.service; import com.baizhi.dao.EmpDAO; import com.baizhi.entity.Emp; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service("empService") @Transactional public class EmpServiceImpl implements EmpService{ private EmpDAO empDAO; //推荐构造注入 @Autowired public EmpServiceImpl(EmpDAO empDAO) { this.empDAO = empDAO; } @Override public void save(Emp emp) { empDAO.save(emp); } @Override public List<Emp> findAll() { return empDAO.findAll(); } }
package com.baizhi.test; import com.baizhi.entity.Emp; import com.baizhi.service.EmpService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Date; public class TestEmpServiceImpl { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); EmpService empService = (EmpService) context.getBean("empService"); empService.save(new Emp(null,"皮卡丘",new Date(),123.456)); empService.findAll().forEach(emp -> System.out.println("emp = " + emp)); //输出结果:emp = Emp{id=1, name='皮卡丘', birthday=Wed Apr 27 16:53:24 CST 2022, salary=123.46} } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启注解扫描--> <context:component-scan base-package="com.baizhi.controller"/> <!--开启mvc注解驱动--> <mvc:annotation-driven/> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 加载spring.xml--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- 配置spring工厂启动--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置springmvc--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
package com.baizhi.controller; import com.baizhi.entity.Emp; import com.baizhi.service.EmpService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController //使用此注解可将类中返回值转为json响应到浏览器 @RequestMapping("emp") public class EmpController { private EmpService empService; //推荐构造注入 @Autowired public EmpController(EmpService empService) { this.empService = empService; } //保存 @RequestMapping("save") public void save(Emp emp){ empService.save(emp); } //查询所有 @RequestMapping("findAll") public List<Emp> findAll(){ return empService.findAll(); } }
测试插入数据:
访问地址:http://localhost:8888/ssm/emp/save?name=猪猪侠&birthday=2020/12/12&salary=234.567
测试查询数据:
大量maven冗余配置
每次构建项目都要书写大量相同配置极大浪费了项目开发时间
每次整合第三方技术都需要编写相关配置文件
项目测试每次都需要部署到tomcat
注意:这就是早期的SSM或者SSH开发存在问题,是不是很麻烦☹️☹️☹️
配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili
Spring Boot是由Pivotal团队提供的全新框架
,其设计目的是用来简化Spring应用的初始搭建以及开发过程
。该框架使用了特定的方式来进行配置
,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
Spring Boot 全新框架作用:简化spring应用初始搭建和开发过程
如何简化:开发人员使用springboot只要基于特定方式进行配置,简化spring使用
SpringBoot 微框架: 5分钟完成之前ssm中环境
springboot(微框架) = springmvc(控制器) + spring core(项目管理)
(1)创建完整的独立的Spring应用程序
:使spring、springmvc只有一个容器
(2)内部嵌入了Tomcat,无需部署WAR文件
:springboot内嵌tomcat,应用跑在内嵌服务器
(3)简化Maven配置,自动配置Spring、Springmvc,没有XML配置
:只需要额外引入少数几个依赖
“用了springboot,spring应用再无xml”
总结:
springboot项目中必须在src/main/resources中放入application.yml(.properties)核心配置文件,且名字必须为:application
springboot项目中必须在src/main/java中所有子包之外构建全局入口类型:xxApplication,入口类一个springboot项目只能有一个
配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili
SpringBoot官网:Spring Boot
新建项目时只需要建立普通的maven项目即可,不需要勾选任何选项:
新建后的项目结构如下,需要额外引入src/test/resources目录:
# 1.System Requirements JDK 1.8+ MAVEN 3.2+ Spring Framework 5.x+ 即SpringBoot 2.5.0以上 # 2.ServletContainers: Tomcat 9.0+ # 3.开发工具 IDEA 2021版本
<!-- 继承springboot的父项目,便于维护版本 2.3之前版本:2.3.x.RELEASE 2.4之后版本:2.4.x 去掉了RELEASE,改为纯数字表示 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> </parent> <!--引入依赖--> <dependencies> <!--引入springboot的web支持spring-boot-starter-web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
# 公共配置 server: port: 8888 # 修改内嵌服务器端口号 servlet: context-path: /springboot01 # 修改项目名 注意项目名必须以“/”开头
//在项目中如下的包结构中创建入口类 Application /* com +| study */ package com.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @ClassName SpringBoot01Application * @Description TODO * @Author Jiangnan Cui * @Date 2022/4/28 11:27 * @Version 1.0 */ /** * @SpringBootApplication注解 * 修饰范围:只能用在入口类上,且只能出现一次 * 作用:标识这个类是一个springboot的入口类,是启动整个springboot项目的总入口 * springboot项目创建步骤总结: * 1.pom.xml文件引入依赖 * 2.resources目录下生成application.yml * 3.创建入口类加入@SpringBootApplication注解,在main中启动应用 */ @SpringBootApplication public class SpringBoot01Application { public static void main(String[] args) { /** * 启动springboot应用 * 参数1:指定入口类的类对象(.class),注意不是类的对象 * 参数2:main函数参数 * 注意:默认端口是8080,如果端口被占用,需要在application.yml中重新指定端口,否则不能正常启动项目 */ SpringApplication.run(SpringBoot01Application.class,args); } } // springboot = springmvc(控制器controller) + spring(工厂) /** * @SpringBootApplication: 注解 * 组合注解: 就是由多个注解组合而成一个注解 * 元注解 : 用来修饰注解的注解,如:@Target、@Retention、@Documented、@Inherited * @Target: 指定注解作用范围 * @Retention: 指定注解什么时候有效 * 包含下面三个注解: * @SpringBootConfiguration: * 这个注解就是用来自动配置spring、springmvc(初始化servlet ...)相关环境 * * @EnableAutoConfiguration: 开启自动配置 * 自动配置核心注解 自动配置spring相关环境 自动与项目中引入的第三方技术自动配置其环境 * mybatis-springboot、redis-springboot 、es-springboot 、rabbitmq 第三方技术 * * @ComponentScan: 组件扫描 * 根据注解发挥注解作用,默认扫描当前包及其子包 * * 启动springboot应用时候需要传递main函数参数作为启动的第二个参数: * 主要用途是测试用,项目启动后动态传参,传递JVM相关的一些参数 * * 外部部署时打包成jar包:java -jar --spring.config.location=绝对路径 xxx.jar * */
运行main启动项目:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.0) 2022-04-28 11:29:21.768 INFO 10908 --- [ main] com.study.SpringBoot01Application : Starting SpringBoot01Application using Java 1.8.0_131 on cjn-PC with PID 10908 (D:\Software_Development\IDEA_code\SpringBoot\springboot01\target\classes started by cjn in D:\Software_Development\IDEA_code\SpringBoot\springboot01) 2022-04-28 11:29:21.775 INFO 10908 --- [ main] com.study.SpringBoot01Application : No active profile set, falling back to default profiles: default 2022-04-28 11:29:22.628 INFO 10908 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8888 (http) 2022-04-28 11:29:22.642 INFO 10908 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-04-28 11:29:22.642 INFO 10908 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46] 2022-04-28 11:29:22.716 INFO 10908 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-04-28 11:29:22.716 INFO 10908 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 895 ms 2022-04-28 11:29:23.044 INFO 10908 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8888 (http) with context path '' 2022-04-28 11:29:23.053 INFO 10908 --- [ main] com.study.SpringBoot01Application : Started SpringBoot01Application in 1.805 seconds (JVM running for 3.3) 2022-04-28 11:29:23.054 INFO 10908 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT 2022-04-28 11:29:23.056 INFO 10908 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC //说明: 出现以上日志说明启动成功
注意:到这里项目环境已经搭建成功了,看看仅仅需要5分钟
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/199769
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。