传统的Spring web项目配置须要通过如下步骤:html
可是针对一些单一业务需求的项目,例如该项目只是实现一个邮件收发,表格导出等单一功能,配置步骤便显得麻烦。java
Spring-Boot的自动配置则可以简化不少配置,减小传统开发步骤,直接开展项目。mysql
应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring-Boot应用密切相关的配置类信息。 度量指标类:获取应用程序运行过程当中用于监控的度量指标,好比:内存信息、线程池信息、HTTP请求统计等。 操做控制类:提供了对应用的关闭等操做类功能。
例如:web
Spring-Boot CLI是一个命令行工具,可用于快速搭建基于Spring的原型。它支持运行Groovy脚本,这也就意味着你可使用相似Java的语法,但不用写不少的模板代码。算法
https://docs.spring.io/spring...spring
在PATH 中配置Spring-Boot CLI文件夹下bin的路径:
D:spring-boot-cli-1.5.9.RELEASE-binspring-1.5.9.RELEASEbinsql
输入 spring --version (注意是--)数据库
spring init --build=maven --java-version=1.8 --dependencies=web --packaging=jar --boot-version=1.5.9.RELEASE --groupId=com.example.demo --artifactId=javenjson
--build:表示项目构建工具maven,也能够选择gradle --java-version:表示JDK版本 --dependencies=web:表示依赖web插件 --packaging:表示打包程序方式 --boot-version:选择 spring boot的版本 --groupId:maven的groupId --artifactId:maven的artifactId
将生成的javan.zip导入eclipse(STS)中便可spring-mvc
Spring-Boot相比于之前的控制台报错信息,更加人性化和简洁。
Spring-Boot内嵌容器支持开箱即用(out of the box)
也可使用Spring-Boot应用部署到任何兼容Servlet 3.0+的容器。
进行项目名称等设置并选择版本、依赖。而后会下载这个项目的压缩文件,解压后,使用 eclipse,Import -> Existing Maven Projects -> Next ->选择解压后的文件夹-> Finsh 成功导入该项目
上面 1)、2)的构建方式中生成的pom.xml文件中默认有两个模块:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>
yml: Spring-Boot会加载的配置文件(和properties功能一致,只是文件编辑的格式不一样)
除此以外: application.properties 配置中文值的时候,读取出来的属性值会出现乱码问题。可是 application.yml 不会出现乱码问题。缘由是,Spring-Boot 是以 iso-8859 的编码方式读取 application.properties 配置文件。
如下是 .properties和 .yml格式对比
spring.datasource.url=jdbc:mysql://localhost:3306/yourDB spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true spring.thymeleaf.cache=false
spring: datasource: url: jdbc:mysql://localhost:3306/yourDB username: root password: root driver-class-name: com.mysql.jdbc.Driver jpa: properties.hibernate.hbm2ddl.auto: update properties.hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect show-sql: true thymeleaf: cache: false
yml的格式特色:缩进式,"="号使用":"替代,":"以后要使用空格。
yml比properties更能清晰体现结构以及内容,相似eclipse里面的包路径展现方式
@PropertySource 扫描指定路径下的properties
@SpringBootApplication @PropertySource("classpath:prop/application.properties") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
@EnableEncryptableProperties @RunWith(SpringRunner.class) @SpringBootTest(classes=Deserializers.Base.class) //@SpringBootTest public class JaysptTest { @Autowired private StringEncryptor stringEncryptor;//密码解码器注入 @Test public void test() { System.out.println("生成加密后的数据库用户名:"+stringEncryptor.encrypt("root")); System.out.println("生成加密后的数据库密码:"+stringEncryptor.encrypt("root")); } }
生成加密后的数据库用户名:5KtXuBsNjeQtOuUOR8PPMg== 生成加密后的数据库密码:Y/SEuMVPbIqgimIKqnxFrg==
在配置文件中修改:
默认状况下jasypt采用的算法是PBEWithMD5AndDES,该算法对同一串明文每次加密的密文都不同,比较适合作数据加解密。可是该算法必须配置密码,咱们在yml文件配置以下参数
jasypt: encryptor: password: 123456
spring: datasource: url: jdbc:mysql://localhost:3306/sbjpa?useUnicode=true&characterEncoding=UTF-8 username: ENC(5KtXuBsNjeQtOuUOR8PPMg==) password: ENC(Y/SEuMVPbIqgimIKqnxFrg==) driver-class-name: com.mysql.jdbc.Driver
@RestController public class UsersController { @RequestMapping("/users") public Map<String, Object> getUsers() { Map<String, Object> map = new HashMap<>(); Users users = new Users(); users.setUid(001); users.setUserName("administrator"); users.setUserSex("simpleBoy"); users.setUserAge(16); map.put("users", users); return map; } }
输出结果为json对象:
相比以往复杂的配置,Spring-Boot的配置至关简单:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency>
只用添加相关依赖和application.yml并配合注解便可
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--Spring Boot--> <!--支持 Web 应用开发,包含 Tomcat 和 spring-mvc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--添加适用于生产环境的功能,如性能指标和监测等功能。 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <-- 这里使用的是MySQL数据库--!> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies>
mybatis: type-aliases-package: com.example.demo.po spring: datasource: url: jdbc:mysql://localhost:3306/sbjpa username: root password: root driverClassName: com.mysql.jdbc.Driver
@MapperScan
1. @MapperScan("com.example.*.dao") 2. @MapperScan("com.example.test1.dao,com.example.test2.dao")
@SpringBootApplication @MapperScan("com.example.*.dao") public class SpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMybatisApplication.class, args); } }
public interface UsersMapper { @Select("select * from users") @Results({ @Result(property = "userName", column = "user_name"), @Result(property = "userSex", column = "user_sex"), @Result(property = "userAge", column = "user_age") }) public List<Users> getAll(); @Insert("insert into users(user_name,user_sex,user_age) values(#{usersName},#{userSex},#{userAge})") public void insert(Users users); @Update("update users set user_name=#{usersName},user_sex=#{userSex},user_age=#{userAge}") public void update(Users users); @Delete("delete from users where uid=#{uid}") public void delete(Integer uid); }
1. @MapperScan("com.example.*.dao") 2. @MapperScan("com.example.test1.dao,com.example.test2.dao")
@SpringBootApplication @MapperScan("com.example.*.dao") public class SpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMybatisApplication.class, args); } }
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml mybatis.type-aliases-package=com.example.*.po spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/sbmybatis?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password =
<?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.neo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.neo.entity.UserEntity"> <id column="id" property="id" jdbcType="BIGINT" /> <result column="userName" property="userName" jdbcType="VARCHAR" /> <result column="passWord" property="passWord" jdbcType="VARCHAR" /> <result column="user_sex" property="userSex" javaType="com.neo.enums.UserSexEnum" /> <result column="nick_name" property="nickName" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List"> id, userName, passWord, user_sex, nick_name </sql> <sql id="Base_Where_List"> <if test="userName != null and userName != ''"> and userName = #{userName} </if> <if test="userSex != null and userSex != ''"> and user_sex = #{userSex} </if> </sql> <select id="getAll" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> FROM users </select> <select id="getList" resultMap="BaseResultMap" parameterType="com.neo.param.UserParam"> select <include refid="Base_Column_List" /> from users where 1=1 <include refid="Base_Where_List" /> order by id desc limit #{beginLine} , #{pageSize} </select> <select id="getCount" resultType="Integer" parameterType="com.neo.param.UserParam"> select count(1) from users where 1=1 <include refid="Base_Where_List" /> </select> <select id="getOne" parameterType="Long" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> FROM users WHERE id = #{id} </select> <insert id="insert" parameterType="com.neo.entity.UserEntity"> INSERT INTO users (userName,passWord,user_sex) VALUES (#{userName}, #{passWord}, #{userSex}) </insert> <update id="update" parameterType="com.neo.entity.UserEntity"> UPDATE users SET <if test="userName != null">userName = #{userName},</if> <if test="passWord != null">passWord = #{passWord},</if> nick_name = #{nickName} WHERE id = #{id} </update> <delete id="delete" parameterType="Long"> DELETE FROM users WHERE id =#{id} </delete> </mapper>