一、Springboot的创建
1、Springboot的优点:
Springboot是来简化Spring框架,从而能够更加简单快捷的构建Spring应用的框架,其具有如下优点:
①为所有Spring开发者更快的入门
②开箱即用,提供各种默认配置来简化项目配置
③内嵌式容器简化Web项目
④没有冗余代码生成和XML配置的要求
2、Springboot的系统要求:
JDK1.8(JDK8)以上
3、使用IDEA创建Springboot项目:
①首先选择file→New→Project,而后选择Spring Initializr,最后再选择next,如图所示:(此次我的JDK版本是10)
②在此次设置项目的名称等参数,Artifact是项目名称,type选择Moven Project,Language选择Java,Java版本根据自己的来,而后选择next,如图所示:
③接下来点击Web,勾选web即可,如图所示:
④此处还可以修改项目名称,而后Finish,这样变创建好了Springboot项目
4、项目的结构
src文件:
Java文件夹主要用来存放编写的Java核心代码,其中项目名+Application命名的class文件(此处的DemoApplication文件)是整个项目的启动文件
Resources文件夹用来存放项目的资源文件,如spring,hibernate的配置文件
test文件用来存放各种测试文件
target文件:项目的输出位置
二、编写Hello World代码
首先打开DemoApplication启动文件,而后在其中编写如下代码(代码中解释了启动形式):
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController //表示该类中的所有方法返回json格式,等同于@Controller+ @ResponseBody //让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration 和@ComponentScan @SpringBootApplication public class DemoApplication { public static void main(String[] args) { //整个程序的入口,启动Springboot项目 SpringApplication.run(DemoApplication.class, args); } @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8") String index(){ return "Hello Spring Boot!"; } }
右键点击Run 'DemoApplition'而后运行,结果如图所示:
而后打开浏览器输入 http://127.0.0.1:8080/,便实现了springboot的启动运行,运行结果如下:
到此,一个简单的SpringBoot搭建完成了
2.然后开始我们的第二步,搭建SSM
在上一步的基础上我们需要添加些相关的配置来搭建SSM,如下为结构图:
UserController:
@RestController /**自动返回的是json格式数据***/ public class UserController { @Autowired private UserService userService; @RequestMapping("list") public List<User> list(){ //只对紧邻的下一条select语句进行分页查询,对之后的select不起作用 List<User> list = userService.findAllUser(); return list; } }
User:
public class User implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private Integer id; private String username; private Date birthday; private String sex; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
UserMapper:
@Mapper public interface UserMapper { public List<User> findAll(); }
UserService:
public interface UserService { /** * 根据接口查询所用的用户 */ public List<User> findAllUser(); }
UserServiceImpl:
@Service public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; public List<User> findAllUser() { List<User> list = userMapper.findAll(); return list; } }
UserMapper.XML:
<?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.demo.mapper.UserMapper"> <select id="findAll" resultType="com.demo.bean.User"> select * from user </select> </mapper>
SqlMapperConfig:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 对在此配置文件下的所有cache进行全局性开/关设置 true|false true --> <setting name="cacheEnabled" value="true" /> <!-- 全局性设置懒加载。如果设为‘关',则所有相关联的都会被初始化加载。 --> <setting name="lazyLoadingEnabled" value="true" /> <!-- 当设置为‘开’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。 --> <setting name="aggressiveLazyLoading" value="true" /> <!-- 允许和不允许单条语句返回多个数据集(取决于驱动需求) --> <setting name="multipleResultSetsEnabled" value="true" /> <!-- 使用列标签代替列名称。不用的驱动器有不同的作法。 --> <setting name="localCacheScope" value="STATEMENT" /> <!-- 允许JDBC生成主键。需要驱动器支持.如果设为了true,这个设置将强制使用被生成的主键, 有一些驱动器不兼容不过仍然可以执行。 --> <setting name="useGeneratedKeys" value="true" /> <!-- 指定MyBatis是否并且如何来自动映射数据表字段与对象的属性。PARTIAL将只自动映射简单的,NONE没有嵌套的结果。 FULL将自动映射所有复杂的结果。 --> <setting name="autoMappingBehavior" value="PARTIAL" /> <!-- 配置和设定执行器,SIMPLE执行器执行其它语句。REUSE执行器可能重复使用preparedstatements语句,BATCH执行器可以重复执行语句和批量更新。 --> <setting name="defaultExecutorType" value="SIMPLE" /> <!-- 设置一个时限,以决定让驱动器等待数据库回应的多长时间为超时. 正整数 --> <setting name="defaultStatementTimeout" value="5000" /> </settings> </configuration>
application.properties:
#server.port=80 #logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-location = classpath:mybatis/sqlMapConfig.xml #mybatis.type-aliases-package = com.demo.bean #shujuyuan spring.datasource.driver-class-name= com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = root
Pom.XML:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springbootdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springbootdemo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--mybatis与mysql--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--druid依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.25</version> </dependency> <!--redis依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--jasypt加解密--> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>1.14</version> </dependency> <!--zipkin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> <!--eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
项目运行后访问http://localhost:8080/list,可以见到如下结果(这里省略sql表的设计,可以自己根据实体类来建表)