spring boot+thymeleaf+mybatis

人最怕什么? 我想莫過于干一件事情,沒有下手的地方了,而當你先學一個新技術時,你肯定會為找一個靠譜的demo而煩惱.
趁著這段空閑期,整理了一下網(wǎng)上的教程,自己搞了一個demo.也遇到了幾個完全不懂spring boot而發(fā)生的坑.
開發(fā)環(huán)境 : intellij idea 與 jdk7

  • 新建一個最簡單的maven工程,最后的結構如圖


    Paste_Image.png
  • pom文件如下
<?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.sam</groupId>
        <artifactId>spring-boot</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.5.RELEASE</version>
            <relativePath/>
        </parent>

        <dependencies>
            <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.1.1</version>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.21</version>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

  </project>

首先是增加了<parent>,增加父pom比較簡單,而且spring-boot-starter-parent包含了大量配置好的依賴管理,在自己項目添加這些依賴的時候不需要寫<version>了。并且在構建中要聲明使用spring-boot-maven-plugin這個插件,它會對Maven打包形成的JAR進行二次修改,最終產(chǎn)生符合我們要求的內(nèi)容結構。

  • 入口main方法-SamApplication.java
/**
 * Created by sam on 16/6/27.
 */
@SpringBootApplication
public class SamApplication {

    public static void main(String[] args) {
        SpringApplication.run(SamApplication.class, args);
    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {

        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {

                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

                container.addErrorPages(error401Page, error404Page, error500Page);
            }
        };
    }
}

1 . 在src/main/java包下,必須放一個含有main方法的主啟動的類,而且只能有一個main方法,如果再出現(xiàn)其他的main方法,在使用maven編譯打包時,會報編譯錯誤,而且spring boot只會掃描到這個main的類的包下的文件
2 . @SpringBootApplication
由于大量的項目都會在主要的配置類上添加@Configuration, @EnableAutoConfiguration,@ComponentScan三個注解。
因此Spring Boot提供了@SpringBootApplication注解,該注解可以替代上面三個注解(使用Spring注解繼承實現(xiàn))

  • 配置thymeleaf
    1 . 配置pom.xml,參照上圖
    2 . spring boot默認會去查找src/main/resources/templates/下的視圖以及src/main/resource/static/js/,src/main/resource/static/css/下的資源文件
  • 配置application.properties
    默認配置文件,可以在里面配置端口號等
    server.port=8888
  • 配置mybatis
    1 . 配置pom.xml,參照上圖
    2 . 在application.properties里配置datasource
spring.datasource.url=jdbc:mysql://localhost:3306/sam
spring.datasource.username=root
spring.datasource.password=1101
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  • controller
/** 
  * Created by sam on 16/6/27. 
  */
@Controller
public class SpringController {    
    @Autowired    
    UserDao dao;    
    @RequestMapping("/sam")    
    @ResponseBody    
    public String hello() {        
          String email = dao.queryUserName("sam@balabala.com");        
          return "hello sam! " + email;  
    }
}
  • dao
/** 
  * Created by sam on 16/6/27. 
  */
@Mapper
public interface UserDao {    
     @Select("select customer_name from customer where email= #{email}")    
     String queryUserName(@Param("email") String email);
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容