原文连接java
上篇文章介绍了Spring boot初级教程: spring boot(一):入门篇 ,方便你们快速入门、了解实践Spring boot特性;本篇文章接着上篇内容继续为你们介绍spring boot的其它特性(有些未必是spring boot体系桟的功能,可是是spring特别推荐的一些开源技术本文也会介绍),对了这里只是一个大概的介绍,特别详细的使用咱们会在其它的文章中来展开说明。mysql
web开发
spring boot web开发很是的简单,其中包括经常使用的json输出、filters、property、log等web
json 接口开发
在之前的spring 开发的时候须要咱们提供json接口的时候须要作那些配置呢
1. 添加 jackjson 等相关jar包
2. 配置spring controller扫描
3. 对接的方法添加@ResponseBodyspring
就这样咱们会常常因为配置错误,致使406错误等等,spring boot如何作呢,只须要类添加 @RestController 便可,默认类中的方法都会以json的格式返回sql
@RestController public class HelloWorldController { @RequestMapping("/getUser") public User getUser() { User user=new User(); user.setUserName("小明"); user.setPassWord("xxxx"); return user; } }
若是咱们须要使用页面开发只要使用 @Controller ,下面会结合模板来讲明数据库
自定义Filter
咱们经常在项目中会使用filters用于录调用日志、排除有XSS威胁的字符、执行权限验证等等。Spring Boot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,而且咱们能够自定义Filter。
两个步骤:
1. 实现Filter接口,实现Filter方法
2. 添加 @Configurationz 注解,将自定义Filter加入过滤链json
好吧,直接上代码服务器
@Configuration public class WebConfiguration { @Bean public RemoteIpFilter remoteIpFilter() { return new RemoteIpFilter(); } @Bean public FilterRegistrationBean testFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new MyFilter()); registration.addUrlPatterns("/*"); registration.addInitParameter("paramName", "paramValue"); registration.setName("MyFilter"); registration.setOrder(1); return registration; } public class MyFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) srequest; System.out.println("this is MyFilter,url :"+request.getRequestURI()); filterChain.doFilter(srequest, sresponse); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } } }
自定义Property
在web开发的过程当中,我常常须要自定义一些配置文件,如何使用呢session
配置在application.properties中
com.neo.title=纯洁的微笑 com.neo.description=分享生活和技术
自定义配置类app
@Component public class NeoProperties { @Value("${com.neo.title}") private String title; @Value("${com.neo.description}") private String description; //省略getter settet方法 }
log配置
配置输出的地址和输出级别
logging.path=/user/local/log logging.level.com.favorites=DEBUG logging.level.org.springframework.web=INFO logging.level.org.hibernate=ERROR
path为本机的log地址, logging.level 后面能够根据包路径配置不一样资源的log级别
数据库操做
在这里我重点讲述mysql、spring data jpa的使用,其中mysql 就不用说了你们很熟悉,jpa是利用Hibernate生成各类自动化的sql,若是只是简单的增删改查,基本上不用手写了,spring内部已经帮你们封装实现了。
下面简单介绍一下如何在spring boot中使用
一、添加相jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
二、添加配置文件
spring.datasource.url=jdbc:mysql://localhost:3306/test 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
其实这个hibernate.hbm2ddl.auto参数的做用主要用于:自动建立|更新|验证数据库表结构,有四个值:
1. create: 每次加载hibernate时都会删除上一次的生成的表,而后根据你的model类再从新来生成新表,哪怕两次没有任何改变也要这样执行,这就是致使数据库表数据丢失的一个重要缘由。
2. create-drop :每次加载hibernate时根据model类生成表,可是sessionFactory一关闭,表就自动删除。
3. update:最经常使用的属性,第一次加载hibernate时根据model类会自动创建起表的结构(前提是先创建好数据库),之后加载hibernate时根据 model类自动更新表结构,即便表结构改变了但表中的行仍然存在不会删除之前的行。要注意的是当部署到服务器后,表结构是不会被立刻创建起来的,是要等 应用第一次运行起来后才会。
4. validate :每次加载hibernate时,验证建立数据库表结构,只会和数据库中的表进行比较,不会建立新表,可是会插入新值。
dialect 主要是指定生成表名的存储引擎为InneoDB
show-sql 是否打印出自动生产的SQL,方便调试的时候查看
三、添加实体类和Dao
@Entity public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id; @Column(nullable = false, unique = true) private String userName; @Column(nullable = false) private String passWord; @Column(nullable = false, unique = true) private String email; @Column(nullable = true, unique = true) private String nickName; @Column(nullable = false) private String regTime; //省略getter settet方法、构造方法 }
dao只要继承JpaRepository类就能够,几乎能够不用写方法,还有一个特别有尿性的功能很是赞,就是能够根据方法名来自动的生产SQL,好比 findByUserName 会自动生产一个以 userName 为参数的查询方法,好比 findAlll 自动会查询表里面的全部数据,好比自动分页等等。。