1. 环境要求
开发环境JDK 1.8
项目管理工具( Maven )
开发工具(Eclipse)
2. 导入Spring Boot依赖
spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
java.version 指定jdk版本号:
<java.version>1.8</java.version>
添加spring-boot-starter-web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 可执行jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3. 热部署
即使修改了输出内容也要重启APP,非常麻烦!可以使用spring-boot-devtools来实现!
1)介绍
spring-boot-devtools 是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去。原理是在发现代码有更改之后,重新启动应用,但是速度比手动停止后再启动还要更快,更快指的不是节省出来的手工操作的时间。
其深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为 restart ClassLoader
,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间(5秒以内)
2)使用
添加依赖包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
以spring-boot 和mybatis集成为例来演示spring-boot的应用
4. Spring boot-mybatis
(1)新建maven project;
新建一个maven project,取名为:spring-boot-mybatis
在pom.xml文件中引入相关依赖;
基本依赖,jdk版本号;
(2)mysql驱动,mybatis依赖包,mysql分页PageHelper:
<!-- spring-boot mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- spring boot mybatis 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
(3)创建启动类App.java
@SpringBootApplication
@MapperScan("cn.itsource.springboot.mybatis.mapper")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
//这里和以往不一样的地方就是MapperScan的注解,这个是会扫描该包下的接口
(4)在application.properties添加配置文件;
#tomcat server port
server.port=80
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
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
(5)编写User测试类;
package cn.itsource.springboot.mybatis.domain;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -2107513802540409419L;
private Long id;
private String name;
getter/setter...
}
(6)编写UserMapper;
注解方式 :
package cn.itsource.springboot.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import cn.itsource.springboot.mybatis.domain.User;
@Mapper
public interface UserMapper {
@Select("select * from t_user t_user name = #{name}")
List<User> likeName(String name);
@Select("select * from t_user where id = #{id}")
User getById(long id);
@Select("select name from t_user where id = #{id}")
String getNameById(long id);
}
XML方式:
package cn.itsource.springboot.mybatis.mapper;
import java.util.List;
import cn.itsource.springboot.mybatis.domain.User;
public interface UserMapper {
List<User> likeName(String name);
User getById(long id);
String getNameById(long id);
}
然后在resources下增加mapper.xml文件
/cn/itsource/springboot/mybatis/mapper/UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itsource.springboot.mybatis.mapper.UserMapper">
<select parameterType="string" resultType="User" id="likeName">
select * from t_user where name like concat('%',#{name},'%')
</select>
<select parameterType="long" resultType="User" id="getById">
select * from t_user where id = #{id}
</select>
<select parameterType="long" resultType="string" id="getNameById">
select name from t_user where id = #{id}
</select>
<insert parameterType="User" id="save" keyColumn="id"
keyProperty="id" useGeneratedKeys="true">
insert into t_user(name) values(#{name})
</insert>
</mapper>
最后需要在application.properties中增加别名包和mapper xml扫描包的配置
## Mybatis config
mybatis.typeAliasesPackage=cn.itsource.springboot.mybatis.domain
mybatis.mapperLocations=classpath:mapper/*.xml
#Yaml 配置
# Mybatis配置
mybatis:
typeAliasesPackage: cn.itsource.domain
mapperLocations: classpath:cn/itsource/dao/mapper/*.xml
(7)编写UserService
package cn.itsource.springboot.mybatis.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.itsource.springboot.mybatis.domain.User;
import cn.itsource.springboot.mybatis.mapper.UserMapper;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User get(Long id){
return userMapper.getById(id);
}
}
(8)编写UserController;
package cn.itsource.springboot.mybatis.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import cn.itsource.springboot.mybatis.domain.User;
import cn.itsource.springboot.mybatis.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/{id}")
@ResponseBody
public User get(@PathVariable Long id) {
return userService.get(id);
}
}
//运行访问:http://127.0.0.1/user/1 就可以看到返回的数据了
5.4.2. 使用PageHelper分页
在application.properties中配置分页插件
#pagehelper.
pagehelper.autoDialect=true
pagehelper.closeConn=true
在调用mapper的前面开启分页功能
package cn.itsource.springboot.mybatis.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.itsource.springboot.mybatis.domain.User;
import cn.itsource.springboot.mybatis.mapper.UserMapper;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User get(Long id){
return userMapper.getById(id);
}
public PageInfo<User> likeName(String name,Integer p) {
PageHelper.startPage(p, 1);
List<User> users = userMapper.likeName(name);
return new PageInfo<>(users);
}
}