Spring Boot Learning Note one

Source: Internet
Author: User

Web Development with Java always needs to solve various dependencies and configurations it's annoying. Spring boot does well in this regard. Using MAVEN to manage a project requires a simple introduction of several packages to do web development. Here is an example

<?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.zns</groupid>    <artifactid>restful</artifactid >    <version>0.0.1-snapshot</version>    <packaging >jar</packaging>    <name>Restful</name>    < description>demo project for spring boot</description>     <parent>        <groupid>org.springframework.boot</ groupid>        <artifactid>spring-boot-starter-parent</artifactid>         <version>1.5.4.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-jdbc</artifactId>         </dependency>         <dependency>            <groupId> org.mybatis.spring.boot</groupid>             <artifactId>mybatis-spring-boot-starter</artifactId>             <version>1.3.0</version>         </dependency>        <dependency>             <groupid>org.springframework.boot</ groupid>    &Nbsp;       <artifactid>spring-boot-starter-web</artifactid>         </dependency>         <dependency>            <groupid >mysql</groupId>            < artifactid>mysql-connector-java</artifactid>             <scope>runtime</scope>        </ dependency>        <dependency>             <groupId>org.springframework.boot</groupId>             <artifactid>spring-boot-starter-test</ artifactid>            <scope>test</scope>         </dependency>        <dependency>             <groupId>com.googlecode.xmemcached< /groupid>            <artifactid> xmemcached</artifactid>            < version>2.0.0</version>        </dependency>     </dependencies>    <build>         <plugins>            < plugin>                < Groupid>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>        </plugins>     </build></project>

This pom.xml configuration of the spring boot documentation is very detailed. Let's not talk about it here.

Tell me how I started developing it step-by-step. Spring boot, though, is simple to configure, but always needs to be configured. Write our project configuration in Src/main/resources/application.properties. I only have a few simple lines here.

Server.port=8080 #配置端口
server.tomcat.uri-encoding=UTF-8 #配置字符编码

#数据连接的配置
Spring.datasource.driver-class-name=Com.mysql.jdbc.Driver
Spring.datasource.url=Jdbc:mysql://127.0.0.1:3306/shiro?? Useunicode=true&characterencoding=utf-8
Spring.datasource.username=Root
Spring.datasource.password=Root

#mybatis集成
mybatis.config-location=Classpath:mybatis/mybatis-config.xml #mybatis配置文件这里我只配置了一些别名
mybatis.mapper-locations=Classpath:mybatis/mapper/*.xml #mapper文件
Mybatis.type-aliases-package=Com.zns.model #Dao包路径. In the mapper inside the ResultClass can be abbreviated class name.

#日志的配置
Logging.level.root=warn
Logging.level.org.springframework.web=Info
Logging.level.org.mybatis=Debug
Logging.level.com.zns.domain=Debug #这里我是配置的mapper接口的路径可以在日志中打印sql语句.
Logging.file=spring.log #spring Boot Displays the log only in the console by default. If you need to save the file, we need to customize the log file name.

Here log I did not specify the save path according to the official website document is written so that is allowed. By default, the save path of the log is saved in the project's current path, which is the root path of the project. More configuration I'm not looking at it now. If the online project has the opportunity to use Spring boot I certainly hope that the log can be saved on a daily basis so it's easier to troubleshoot problems. A careful friend may find that my pom.xml has not introduced log4j or other log packages. This is the benefit of spring boot and does not need to be self-primed.

The first time I used this spring boot, it was strange that there was no index.jsp entrance where there was a Java with a main method. Is this the portal for the entire Web project?

Import Org.springframework.boot.*;import Org.springframework.boot.autoconfigure.*;import Org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@                Restcontroller@enableautoconfigurationpublic class Example {@RequestMapping ("/") String Home () {    Return "Hello world!";    } public static void Main (string[] args) throws Exception {Springapplication.run (example.class, args); }}

This is an example of an official document, when I looked silly. It is not possible for each controller to write a main method. Go straight to Google.

Package Com.zns;import Org.mybatis.spring.annotation.mapperscan;import Org.springframework.boot.SpringApplication ; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @mapperscan (" Com.zns.domain ") public class Restfulapplication {public static void main (string[] args) {Springapplication.run    (Restfulapplication.class, args); }}

It turns out this is the place to be. The official documentation explains that @SpringBootApplication annotations are equivalent to using @configuration, @EnableAutoConfiguration and @componentscan and their default properties.

So in the controller you write only need to refer to @restcontroller or @controller annotations on the line.

Here @mapperscan is specialized in scanning the Mapper interface. The bean does not need to be defined either. Use it directly where you need it.

@Autowired annotations are available.

Write down your notes for fear of forgetting. Welcome to the discussion, and I hope Daniel will point out my mistake.

This article is from the "coding" blog, make sure to keep this source http://xtceetg.blog.51cto.com/5086648/1939506

Spring Boot Learning Note one

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.