wordpress网站合并,怎么增加网站的外链,手机上打开html的软件,怎么免费做一个网站做淘宝客1 web开发 Spring boot web 开发非常简单#xff0c;其中包括常用的 json输出、filters、property、log等
1.1 json接口开发
在以前的Spring 开发我么提供json 的做法#xff1a;
添加jackjson 等相关jar包配置Spring controller扫描对接的方法添加ResponseBody
而在Spri…1 web开发 Spring boot web 开发非常简单其中包括常用的 json输出、filters、property、log等
1.1 json接口开发
在以前的Spring 开发我么提供json 的做法
添加jackjson 等相关jar包配置Spring controller扫描对接的方法添加ResponseBody
而在Spring boot中只需要添加 RestController 即可。默认类中的方法都会以json格式返回
RestController public class HelloWorldController {RequestMapping(/getUser)public User getUser() {User usernew User();user.setUserName(小明);user.setPassWord(xxxx);return user;}
}在使用页面开发使用 Controller 即可
1.2 自定义Filter
我们常常在项目中会使用filters用于调用日志、排除有xss威胁的字符、执行权限验证等等。Spring Boot自动添加了 OrderedCharacterEncodingFilter 和 HiddenHttpMethodFilter 并且我们可以自定义Filter。
两个步骤
实现Filter 接口实现Filter方法添加 Configurationz 注解讲自定义Filter加入过滤链
Configuration
public class WebConfiguration {Beanpublic RemoteIpFilter remoteIpFilter() {return new RemoteIpFilter();}public FilterRegistrationBean testFilterRegistrationBean() {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{Overridepublic void destroy() {// TODO Auto-generated method stub}Overridepublic void doFilter(ServletRequest sRequest, ServletResponse sResponse, FilterChain fc)throws IOException, ServletException {// TODO Auto-generated method stubHttpServletRequest request (HttpServletRequest)sRequest;System.out.println(this is request.getRequestURI());fc.doFilter(sRequest, sResponse);}Overridepublic void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}}
}1.3 log配置
配置输出的地址和输出级别
logging.path/user/local/log
logging.level.com.favoritesDEBUG
logging.level.org.springframework.webINFO
logging.level.org.hibernateERROR1.4 数据库操作
重点讲mysql、Spring data jpajpa是利用Hibernate生成各种自动化的 sql如果只是简单的增删改查基本上不用手写Spring 内部已经封装实现了。
添加相应jar包 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency添加配置文件
spring.datasource.urljdbc:mysql://localhost:3306/test
spring.datasource.usernameroot
spring.datasource.passwordroot
spring.datasource.driver-class-namecom.mysql.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.autoupdate
spring.jpa.properties.hibernate.dialectorg.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql truehibernate.hbm2ddl.auto 参数的作用是自动创建|更新|验证数据库表结构有四个值
create每次加载hibernate时都会删除上一次的生成的表然后根据你的model类再重新来生成新表哪怕两次没有任何改变也要这样执行这就是导致数据库表数据丢失的一个重要原因。create-drop: 每次加载hibernate 时根据model 类生成表但是sessionFactory 一关闭表就删除。update最常用的属性第一次加载hibernate 时根据model类会自动建立起表的结构前提是先建立好数据库以后加载hibernate时会根据model类自动更新表结构即使表结构该表了但表中的行仍然存在不会删除以前的行。 要注意的是当第一次部署到服务器后表结构不会立马被建立起来要等到应用第一次运行起来后才会。validate每次加载hibernate时验证创建数据库表结构只会和数据库中的表进行比较不会创建新表但会插入新值。
dialect 主要是指定生成表名的存储引擎为InneoDB。
1.5 添加实体类和Dao
这里后续插入常用实体类的注解dao只要继承jpaRepository 类就可以几乎可以不用写方法
1.6 测试
RunWith(SpringJUnit4ClassRunner.class)
SpringApplicationConfiguration(Application.class)
public class UserRepositoryTests { Autowiredprivate UserRepository userRepository; Testpublic void test() throws Exception {Date date new Date();DateFormat dateFormat DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); String formattedDate dateFormat.format(date);userRepository.save(new User(aa1, aa126.com, aa, aa123456,formattedDate));userRepository.save(new User(bb2, bb126.com, bb, bb123456,formattedDate));userRepository.save(new User(cc3, cc126.com, cc, cc123456,formattedDate));Assert.assertEquals(9, userRepository.findAll().size());Assert.assertEquals(bb, userRepository.findByUserNameOrEmail(bb, cc126.com).getNickName());userRepository.delete(userRepository.findByUserName(aa1));}
}1.7 thymeleaf介绍
thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。 它使用了自然的模板技术。这意味着Thymeleaf的模板语言不会破坏文档结构模板依旧是有效的XML文档。 在运行期替换掉静态值。 velocityFreMakerbeetle也是模板引擎。 下面的代码示例分别使用Velocity、FreeMarker、Thymeleaf打印一条消息
Velocity: p$message/p
FreeMarker: p${message}/p
Thymeleaf: p th:text${message}Hello World!/p注意由于Thymeleaf使用了XML DOM解析器因此它并不适合处理大规模的XML文件。 这里是Thymeleaf使用教程 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
页面即原型Thymeleaf的好处 在传统java web开发过程中前端工程师和后端工程师一样需要一套完整的开发环境然后各类java IDE中修改模板、静态资源。 但实际上前端工程师的职责更多关注页面本身而非后端。使用jspVelocity等传统的java模板引擎很难做到这一点而Thymeleaf通过属性进行模板渲染不会引起新的浏览器不能识别的标签可以直接作为HTML文件在浏览器中打开。
1.8 WebJars
WebJars是一个很神奇的东西可以让大家以jar包的形式来使用前端的各种框架、组件。
什么是WebJars 什么是WebJarsWebJars是将客户端浏览器资源JavaScriptCss等打成jar包文件以对资源进行统一依赖管理。WebJars的jar包部署在Maven中央仓库上。
为什么使用 我们在开发Java web项目的时候会使用像MavenGradle等构建工具以实现对jar包版本依赖管理以及项目的自动化管理但是对于JavaScriptCss等前端资源包我们只能采用拷贝到webapp下的方式这样做就无法对这些资源进行依赖管理。那么WebJars就提供给我们这些前端资源的jar包形势我们就可以进行依赖管理。
如何使用 1、 WebJars主官网 查找对于的组件比如Vuejs
dependencygroupIdorg.webjars.bower/groupIdartifactIdvue/artifactIdversion1.0.21/version
/dependency2、页面引入
link th:href{/webjars/bootstrap/3.3.6/dist/css/bootstrap.css} relstylesheet/link这样就可以正常使用了。
1.9 Gradle 构建工具
类似mavenSpring项目建议使用Gradle进行构建项目相比maven来讲 Gradle更简洁而且Gradle更适合大型项目的构建。
buildscript {repositories {maven { url http://repo.spring.io/libs-snapshot }mavenLocal()}dependencies {classpath(org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE)}
}apply plugin: java //添加 Java 插件, 表明这是一个 Java 项目
apply plugin: spring-boot //添加 Spring-boot支持
apply plugin: war //添加 War 插件, 可以导出 War 包
apply plugin: eclipse //添加 Eclipse 插件, 添加 Eclipse IDE 支持, Intellij Idea 为 ideawar {baseName favoritesversion 0.1.0
}sourceCompatibility 1.7 //最低兼容版本 JDK1.7
targetCompatibility 1.7 //目标兼容版本 JDK1.7repositories { // Maven 仓库mavenLocal() //使用本地仓库mavenCentral() //使用中央仓库maven { url http://repo.spring.io/libs-snapshot } //使用远程仓库
}dependencies { // 各种 依赖的jar包compile(org.springframework.boot:spring-boot-starter-web:1.3.6.RELEASE)compile(org.springframework.boot:spring-boot-starter-thymeleaf:1.3.6.RELEASE)compile(org.springframework.boot:spring-boot-starter-data-jpa:1.3.6.RELEASE)compile group: mysql, name: mysql-connector-java, version: 5.1.6compile group: org.apache.commons, name: commons-lang3, version: 3.4compile(org.springframework.boot:spring-boot-devtools:1.3.6.RELEASE)compile(org.springframework.boot:spring-boot-starter-test:1.3.6.RELEASE)compile org.webjars.bower:bootstrap:3.3.6compile org.webjars.bower:jquery:2.2.4compile(org.webjars:vue:1.0.24)compile org.webjars.bower:vue-resource:0.7.0}bootRun {addResources true
}