最近接触了一下SpringBoot,一个“约定大于配置”的微框架。大概的使用了一下,确实比咱们传统的spring在配置上面简化了很多,很多事情不需要通过web.xml和applicationConetxt.xml进行配置。而且启动服务,有内置的tomcat和jboss容器,也很容易。总的来讲,其实就是就是一些库的集合,它能够被任意项目的构建系统所使用。我建立了一个maven项目,写了一个启动web的例子和连接数据库的例子。
1.web
pom配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
<> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.2.5.RELEASE</version> </dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>1.2.5.RELEASE</version> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>1.2.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency>
|
启动springboot的话,只需要spring-boot-starter-web就可以了 。然后编写一个类包含处理HTTP请求的方法以及一个main()函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }
|
启动main函数后,在控制面板中可以发现启动了一个Tomcat容器,一个基于Spring MVC的应用也同时启动起来,这时访问http://localhost:8080就可以看到Hello World!出现在浏览器中了。同时控制面板上面打印出的spring看着很爽。@EnableAutoConfiguration这个配置就是springboot特有的,作用就是按照应用中配置的依赖对spring的自动配置,减少开发量。@Controller和@RequestMapping也可以合体为@ResController。作用都是一样的。SpringApplication是个帮助类,它的作用就是帮助启动上下文。所以说上面整个代码的流程就是SpringApplication执行run方法,然后configuration发现这是一个Web应用(根据类路径上的依赖确定),于是在内嵌的Tomcat容器中启动了一个Spring的应用上下文,并且监听默认的tcp端口8080(默认约定)。同时在Spring Context中根据默认的约定配置了Spring WebMvc:
Servlet容器默认的Context路径是/
DispatherServlet匹配的路径(servlet-mapping中的url-patterns)是/*
@ComponentScan路径被默认设置为SampleController的同名package,也就是该package下的所有@Controller,@Service, @Component, @Repository都会被实例化后并加入Spring Context中。
需要注意的是,@ComponentScan默认扫描的是SampleController下面的所有bean,所以下面连接数据库测试的例子,SampleController就放在根目录下面。
2.数据库
连接数据库的例子我用mysql,有机会再写一个mongoDB的例子。springboot对关系型数据库和非关系型数据库都有很好的支持。spring-boot-starter-data-mongodb就是mongodb在maven中的配置。整个项目的结构是:
DateSource的信息配置在resource中配置一个application.properties的文档:
1 2 3 4
|
spring.datasource.url=jdbc:mysql: spring.datasource.username=root spring.datasource.password=wyd spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
bean是Visitor,这个没什么好说的,VisitorDao中写了一个insert的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@Component public class VisitorDao { static Log log = LogFactory.getLog(VisitorDao.class); @Autowired private JdbcTemplate jdbcTemplate; public boolean insertVistor(final Visitor vistor) { boolean flag = false; int i = jdbcTemplate.update("insert into visitor(name,email,status,createtime) values(?,?,?,?)", new Object[] { vistor.getName(), vistor.getEmail(), vistor.getStatus(), vistor.getCreateTime() }); if (i > 0) { flag = true; } return flag; } }
|
接着就是一个service层,我在执行的run中调用inset方法,去插入数据,就ok了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
@Component public class SampleDataService implements CommandLineRunner { static Log log = LogFactory.getLog(SampleDataService.class); @Autowired private VisitorDao visitorDao; public void run(String... args) { Visitor visitor = new Visitor(); visitor.setEmail("[email protected]"); visitor.setName("china"); visitor.setStatus(1); visitorDao.insertVistor(visitor); log.info("insert in test"); } }
|
然后启动SampleController,就会在控制面板看到“inset in test”的输出。插入数据库成功。
原文链接 大专栏 https://www.dazhuanlan.com/2019/08/16/5d55fc4baed17/