传统的Spring web项目配置需要经过以下步骤:
但是针对一些单一业务需求的项目,例如该项目只是实现一个邮件收发,表格导出等单一功能,配置步骤便显得麻烦。
Spring-Boot的自动配置则能够简化很多配置,减少传统开发步骤,直接开展项目。
度量指标类:获取应用程序运行过程中用于监控的度量指标,比如:内存信息、线程池信息、HTTP请求统计等。
操作控制类:提供了对应用的关闭等操作类功能。" title="" data-original-title=“复制”>
应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring-Boot应用密切相关的配置类信息。
度量指标类:获取应用程序运行过程中用于监控的度量指标,比如:内存信息、线程池信息、HTTP请求统计等。
操作控制类:提供了对应用的关闭等操作类功能。
[外链图片转存失败(img-W0isdES0-1562239821039)(https://mp.csdn.net/img/bVbhznG)]
[外链图片转存失败(img-r3FTbTWq-1562239821040)(https://mp.csdn.net/img/bVbhznH)]
例如:
[外链图片转存失败(img-lsLJhuzx-1562239821040)(https://mp.csdn.net/img/bVbhznI)]
[外链图片转存失败(img-eAZcpP8Y-1562239821041)(https://mp.csdn.net/img/bVbhznJ)]
Spring-Boot CLI是一个命令行工具,可用于快速搭建基于Spring的原型。它支持运行Groovy脚本,这也就意味着你可以使用类似Java的语法,但不用写很多的模板代码。
https://docs.spring.io/spring...
[外链图片转存失败(img-8FGC9PcL-1562239821042)(https://mp.csdn.net/img/bVbhznM)]
在PATH 中配置Spring-Boot CLI文件夹下bin的路径:
D:spring-boot-cli-1.5.9.RELEASE-binspring-1.5.9.RELEASEbin
输入 spring --version (注意是--)
[外链图片转存失败(img-PD3Qsm3G-1562239821043)(https://mp.csdn.net/img/bVbhznQ)]
spring init --build=maven --java-version=1.8 --dependencies=web --packaging=jar --boot-version=1.5.9.RELEASE --groupId=com.example.demo --artifactId=javen
[外链图片转存失败(img-C5dGkESV-1562239821043)(https://mp.csdn.net/img/bVbhznW)]
--build:表示项目构建工具maven,也可以选择gradle
--java-version:表示JDK版本
--dependencies=web:表示依赖web插件
--packaging:表示打包程序方式
--boot-version:选择 spring boot的版本
--groupId:maven的groupId
--artifactId:maven的artifactId
[外链图片转存失败(img-DyNW2gzw-1562239821044)(https://mp.csdn.net/img/bVbhznX)]
将生成的javan.zip导入eclipse(STS)中即可
[外链图片转存失败(img-JAEo11gn-1562239821044)(https://mp.csdn.net/img/bVbhzn5)]
Spring-Boot相比于以前的控制台报错信息,更加人性化和简洁。
Spring-Boot内嵌容器支持开箱即用(out of the box)
[外链图片转存失败(img-jqcJPEb3-1562239821045)(https://mp.csdn.net/img/bVbhzn6)]
也可以使用Spring-Boot应用部署到任何兼容Servlet 3.0+的容器。
进行项目名称等设置并选择版本、依赖。然后会下载这个项目的压缩文件,解压后,使用 eclipse,Import -> Existing Maven Projects -> Next ->选择解压后的文件夹-> Finsh 成功导入该项目
上面 1)、2)的构建方式中生成的pom.xml文件中默认有两个模块:
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath/>
parent>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
configuration>
plugin>
plugins>
build>
yml: Spring-Boot会加载的配置文件(和properties功能一致,只是文件编辑的格式不同)
除此之外: application.properties 配置中文值的时候,读取出来的属性值会出现乱码问题。但是 application.yml 不会出现乱码问题。原因是,Spring-Boot 是以 iso-8859 的编码方式读取 application.properties 配置文件。
以下是 .properties和 .yml格式对比
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" title="" data-original-title=“复制”>
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
yml的格式特点:缩进式,"="号使用":"替代,":"之后要使用空格。
yml比properties更能清晰体现结构以及内容,类似eclipse里面的包路径展示方式
@PropertySource 扫描指定路径下的properties
}" title="" data-original-title=“复制”>
@SpringBootApplication
@PropertySource(“classpath:prop/application.properties”)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
}" title="" data-original-title=“复制”>
@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
}" title="" data-original-title=“复制”>
@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对象:[外链图片转存失败(img-0fhYptDC-1562239821046)(https://mp.csdn.net/img/bVbhzom)]
相比以往复杂的配置,Spring-Boot的配置相当简单:
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
只用添加相关依赖和application.yml并配合注解即可
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<-- 这里使用的是MySQL数据库--!>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
dependencies>
spring:
datasource:
url: jdbc:mysql://localhost:3306/sbjpa
username: root
password: root
driverClassName: com.mysql.jdbc.Driver" title="" data-original-title=“复制”>
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")
}" title="" data-original-title=“复制”>
@SpringBootApplication
@MapperScan(“com.example.*.dao”)
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
}" title="" data-original-title=“复制”>
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")
}" title="" data-original-title=“复制”>
@SpringBootApplication
@MapperScan(“com.example.*.dao”)
public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args);
}
}
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 = " title="" data-original-title=“复制”>
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 =
id, userName, passWord, user_sex, nick_name
and userName = #{userName}
and user_sex = #{userSex}
INSERT INTO
users
(userName,passWord,user_sex)
VALUES
(#{userName}, #{passWord}, #{userSex})
UPDATE
users
SET
userName = #{userName},
passWord = #{passWord},
nick_name = #{nickName}
WHERE
id = #{id}
DELETE FROM
users
WHERE
id =#{id}
" title="" data-original-title=“复制”>
<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>