spring boot+thymeleaf+mybatis

人最怕什么? 我想莫过于干一件事情,没有下手的地方了,而当你先学一个新技术时,你肯定会为找一个靠谱的demo而烦恼.
趁着这段空闲期,整理了一下网上的教程,自己搞了一个demo.也遇到了几个完全不懂spring boot而发生的坑.
开发环境 : 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进行二次修改,最终产生符合我们要求的内容结构。

  • 入口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方法,如果再出现其他的main方法,在使用maven编译打包时,会报编译错误,而且spring boot只会扫描到这个main的类的包下的文件
2 . @SpringBootApplication
由于大量的项目都会在主要的配置类上添加@Configuration, @EnableAutoConfiguration,@ComponentScan三个注解。
因此Spring Boot提供了@SpringBootApplication注解,该注解可以替代上面三个注解(使用Spring注解继承实现)

  • 配置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);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容