Spring Boot从根本上讲其实就是一些maven库的集合,在maven项目中导入相应依赖即可使用Spring Boot,而且无需自行管理这些库的版本.
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
org.springframework.boot
spring-boot-dependencies
2.0.5.RELEASE
pom
import
引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。
org.springframework.boot
spring-boot-starter-web
新建一个Controller类
@RestController(类上)=@Controller(类上)+@RequestMapper(方法上)
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
新建启动类(App – Main方法)
@SpringBootApplication
public class SpringbootApp {
public static void main(String[] args) {
SpringApplication.run(SpringbootApp.class);
}
}
运行main方法,输入网址
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-freemarker
# FreeeMarker 模板引擎配置
# 设定ftl文件路径---一定要是templates
spring.freemarker.tempalte-loader-path=classpath:/templates
# 关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
Hello World!
你是 ${msg}
@Controller
public class FreemarkController {
@RequestMapping("/index")
public String freemarkTest(Model model){
//为模板添加数据
model.addAttribute("msg", "蛤蟆皮");
return "index";
}
}
新建启动类,启动即可
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
#配置连接mysql的datasourde
spring.datasource.url = jdbc:mysql://localhost:3306/springboot
spring.datasource.username = root
spring.datasource.password = 92597
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
dao层实现类中注入JdbcTemplate
service层中直接添加事务注解即可,Spring Boot已经集成了事务
//类上
@Transactional(propagation = Propagation.SUPPORTS)
//增删改方法上
@Transactional(propagation = Propagation.SUPPORTS,readOnly = false)
@SpringBootApplication
@ComponentScan("cn.zr")
public class RunApp {
public static void main(String[] args) {
SpringApplication.run(RunApp.class);
}
}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
com.github.pagehelper
pagehelper
4.1.0
可以通过注解写sql
也可以通过mapper.xml写sql
@SpringBootApplication
//扫描mapper包
@MapperScan("cn.zr.mapper")
@ComponentScan("cn.zr")
public class MybatisApp {
public static void main(String[] args) {
SpringApplication.run(MybatisApp.class);
}
}
mybatis.type-aliases-package=cn.zr.domain
新建分页信息类,配置分页信息
package cn.zr.pageconfig;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration //相当于我们建了applicationContext-xxx.xml
public class MyBatisConfiguration {
//@Bean:相当于配置了一个bean
/*
*/
@Bean
public PageHelper pageHelper() {
System.out.println("MyBatisConfiguration.pageHelper()");
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
在service层实现类中的方法里添加分页信息
public List queryPage() {
//分页信息
PageHelper.startPage(1, 2);
return userMapper.query();
}
使用分页还需要在启动类上扫描分页信息类,或者直接扫描所有的类
//扫描mapper包
@MapperScan("cn.zr.mapper")
@ComponentScan("cn.zr")
public class MybatisApp {
public static void main(String[] args) {
SpringApplication.run(MybatisApp.class);
}
}