前言:
Spring Boot 解决java web开发中复杂的xml配置,集成tomcat简化tomcat相关配置等。
改博客是本人一边搭建一边编写,转载请注明出处,谢谢。
开发环境 :
jdk 1.8
开发工具 IntelliJ IDEA 2017.1 (注册码 请购买正版!!!)
其它开发工具去官网下载demo即可(spring boot官网)
数据库 mysql
maven 环境
先装好上面的环境,不会的查一下,都是很简单的
Spring Boot 官方demo:
打开idea,选择Project..
选择Spring Initializr
点击next
没什么好说的点 next
选择jar包
Spring Boot 这里是 1.5.2的版本
选择web
选择jpa,里面包含spring jpa和hibernate
选择mysq,里面l包含mysql驱动
点击next
设置项目路径点击finish。
Spring boot 启动:
项目如上图所示。
java 目录对应src
resource下面的static是放html,js等前端资源文件夹
application.properties是spring相关配置(下载的demo里面没有什么东西)。
com.tgp下面的java类是启动spring boot的包含main函数的类,
注意:spring boot 是用main函数启动的
配置mysql数据库
spring boot 配置官网(spring boot配置信息)
注意: 这里使用了数据库,spring boot会自动去配置数据库,如果不配置数据源就启动的话会报找不到datasourse的错误。
在application.properties添加如下配置
###datasource
########################################################
spring.datasource.url=jdbc:mysql://localhost:3306/testguest?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=yc
spring.datasource.password=1234
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
spring.datasource.name=dataSource
配置后运行上面的实体类启动spring boot;
启动成功后。
访问 http://localhost:8080/ 出现下图所示
出现上图所示表示成功了。
Controller:
新建一个controller如下
代码:
packagecom.tgb.controller;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.ResponseBody;
/**
* Created by onion on 2017-03-27 17:25.
*/
@Controller
@RequestMapping("/")
public classUserController {
@ResponseBody
@RequestMapping("/")
publicString name(){
return"Welcome Spring Boot";
}
}
重新启动服务访问http://localhost:8080/
出现 Welcome Spring Boot
注意:spring boot 只会扫描SpringBootStudyApplication
类同包和下层包的类和配置资源
Spring Boot 数据库操作:
在application.properties添加(其中spring.jpa.properties.后面的配置写hibernate有的配置)
# Specify the DBMS
spring.jpa.database=MYSQL
# Show or not log for each sql query
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=false
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update
添加model包并添加User.java
User代码:
注释:ALT+Insert 可以快捷生成get,set等等
packagecom.tgb.model;
importjavax.persistence.*;
/**
* Created by onion on 2017-03-28 08:48.
*/
@Entity
public classUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
privateLongid;
@Column(nullable = false)
privateStringname;
@Column(nullable = false)
privateIntegerage;
publicLong getId() {
returnid;
}
public void setId(Long id) {
this.id= id;
}
publicString getName() {
returnname;
}
public void setName(String name) {
this.name= name;
}
publicInteger getAge() {
returnage;
}
public void setAge(Integer age) {
this.age= age;
}
@Override
publicString toString() {
return"User{"+
"id="+id+
", name='" +name+'\''+
", age=" +age+
'}';
}
}
创建好后启动服务,因为之前配置了hibernate更新表,所以可以在数据库看到User表被创建。
在resources的static下面添加addUser.html
代码:
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>添加用户</title>
<!-- Compiled and minified CSS -->
<linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css">
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Compiled and minified JavaScript -->
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/js/materialize.min.js"></script>
</head>
<body>
<br><br>
<divclass="container">
<h1>添加用户</h1>
<hr>
<formaction="add">
<divclass="row card-panel">
<divclass="input-field col s12 m6">
<labelclass="active"for="userName">userName:</label>
<inputid="userName"type="text"name="name"class="validate"/>
</div>
<divclass="input-field col s12 m6">
<labelclass="active"for="userAge">userAge:</label>
<inputid="userAge"type="number"name="age"class="validate"/>
</div>
<buttontype="submit"class="waves-effect white black-text waves-yellow btn">保存
</button>
</div>
</form>
</div>
</body>
</html>
在com.tgb下新建dao包,并创建UserDao
代码:
packagecom.tgb.dao;
importcom.tgb.model.User;
importorg.springframework.data.jpa.repository.JpaRepository;
importorg.springframework.data.jpa.repository.Query;
importorg.springframework.data.repository.query.Param;
/**
* Created by onion on 2017-03-28 09:07.
*/
public interfaceUserDaoextendsJpaRepository<User,Long> {
}
继承JpaRepository后spring会自动扫描到,所以在service中用Autowired可以取到。
在com.tgb下新建service包,并创建UserService
代码:
packagecom.tgb.service;
importcom.tgb.dao.UserDao;
importcom.tgb.model.User;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
/**
* Created by onion on 2017-03-28 09:11.
*/
@Service
public classUserService {
@Autowired
privateUserDaouserDao;
publicUser addUser(User user) {
returnuserDao.save(user);
}
}
结构如下:
修改UserController,修改后代码为:
packagecom.tgb.controller;
importcom.tgb.model.User;
importcom.tgb.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.ResponseBody;
/**
* Created by onion on 2017-03-27 17:25.
*/
@Controller
@RequestMapping("/")
public classUserController {
@Autowired
privateUserServiceuserService;
@RequestMapping("/")
publicString name(){
return"redirect:addUser.html";
}
@ResponseBody
@RequestMapping("/add")
publicUser addUser(User user){
returnuserService.addUser(user);
}
}
启动服务访问http://localhost:8080 添加用户后可以看到返回了当前添加到数据。去数据库查看是否添加成功。因为设置了hibernate显示sql和格式化sql以及显示调试信息等可以在控制台看到输出的信息。
在UserService中添加代码如下:
publicList<User> getAll() {
returnuserDao.findAll();
}
在UserController中添加代码如下:
@ResponseBody
@RequestMapping("/getall")
publicList<User> getAllUser() {
returnuserService.getAll();
}
并将addUser方法改为如下:
@RequestMapping("/add")
publicString addUser(User user) {
userService.addUser(user);
return"redirect:allUser.html";
}
在static下面新建一个allUser.html,并新建app添加allUser.js,如下:
allUser.html代码:
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>用户表</title>
<!-- Compiled and minified CSS -->
<linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css">
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Compiled and minified JavaScript -->
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/js/materialize.min.js"></script>
<scriptsrc="app/allUser.js"></script>
</head>
<bodyng-app="userApp"ng-controller="userCtrl">
<br><br>
<divclass="container">
<h1>用户列表</h1>
<hr>
<divclass="card-panel">
<ahref="addUser.html"class="waves-effect white black-text waves-yellow btn">
添加用户
</a>
<divclass="divider"></div>
<tableclass="highlight centered bordered">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<trng-repeat="user in users">
<tdng-bind="user.id"></td>
<tdng-bind="user.age"></td>
<tdng-bind="user.name"></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
allUser.js代码:
/**
* Created by Administrator on 2017/3/28 0028.
*/
letapp= angular.module('userApp', []);
app.controller('userCtrl', ($scope, $http) => {
$http.post('getall', {})
.success(function(response) {
$scope.users=response;
});
});
这里用到了angular不懂得可以自己查一下,这里面不会将前端用到的东西。
热部署:
每次修改都要重新启动影响开发效率
在idea中选择:File>Setting..
搜索Compiler
在修改的文件里面右键选择Recompiler或者按ctrl+alt+f9完成对修改文件的热部署
到此spring-boot的基本使用已经完成。可以看出来确实是减少了spring和其它框架的配置。在环境搭建好后实现了零xml配置的开发环境。
代码下载:http://download.csdn.net/detail/qq_36224522/9796264
下面继续讲解:
重写数据源:
一般数据库会加密,我们要重新编写数据源去完成数据库的解密
重新配置hibernate:
在上面搭建的环境中是通过spring-jpa去完成数据库操作的我没有发现hibernate的接口entityInterceptor,这个接口是hibernate的sql拦截接口,为什么需要是因为hibernate生成的sql里面的表名称和字段是在启动的时候缓存好了的,有的公司业务上需要一个月换表或者按照不同的条件去修改表名称或字段等。
以及配置hibernate的事物处理。
配置druid数据源:
使用阿里巴巴的数据源完成sql监控
配置p6spy:
将驱动换成p6spy完成对sql的预编译?符号替换成对应的参数,
并生成sql的日志
等等。。。
请继续关注我的博客http://blog.csdn.net/qq_36224522
改文档下载地址 http://download.csdn.net/detail/qq_36224522/9796263