傳統的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>