初识SpringBoot

SpringBoot

  • 一.SpringBoot认识
  • 二.SpringBoot对SpringMVC的支持
  • 三.SpringBoot对Jsp的支持
  • 四. SpringBoot对freemark 支持
  • 5.SpringBoot对json支持
  • 6.配置ymal的方式
  • 7.SpringBoot测试的使用
  • 8.SpringBoot对springjdbc的支持
  • 9.SpringBoot对Mybatis支持
    • ==需要在这里开启mapper层的扫描==

一.SpringBoot认识

SpringBoot 它Spring里面提供一个框架,或者一些maven的集合,(提供很多starter)
我们通过它,可以快速的构建项目 编译和部署,监控;

二.SpringBoot对SpringMVC的支持

如果需要跑一个springweb程序,只需要添加spring-boot-starter-web依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
  • 新建一个Controller类
    @RestController的效果 相当于在方法上面@ResponseBody
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world222";
    }
}
  • 新建启动类(App – Main方法)
@SpringBootApplication
public class SpringBootApp {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApp.class);
    }
}
  • 测试代码
    运行:App
    浏览器:http://localhost:8080/hello

三.SpringBoot对Jsp的支持

需要的依赖

<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-webartifactId>
	dependency>

	
	<dependency>
		<groupId>javax.servletgroupId>
		<artifactId>javax.servlet-apiartifactId>
		<scope>providedscope>
	dependency>

	<dependency>
		<groupId>javax.servletgroupId>
		<artifactId>jstlartifactId>
	dependency>

	
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-tomcatartifactId>
		<scope>providedscope>
	dependency>
	<dependency>
		<groupId>org.apache.tomcat.embedgroupId>
		<artifactId>tomcat-embed-jasperartifactId>
		<scope>providedscope>
	dependency>

配置application.properties – 不要写错

  • application.properties:
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
  • 启动 配置run -配置当前模块的工作目录
    初识SpringBoot_第1张图片

四. SpringBoot对freemark 支持

需要的依赖

<dependencies>
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-webartifactId>
	dependency>
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-freemarkerartifactId>
	dependency>
dependencies>

配置:application.properties

  • application.properties
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
注意一点:如果使用上面的配置 模板的名称使用templates

5.SpringBoot对json支持

  • @RestControll -->在方法上面就不用配置@ResponseBody
  • @Controller + @ResponseBody
  • 返回String
  • 返回对象 --时间类型加 @JsonFormat
  • 返回List

6.配置ymal的方式

  • application.yml --特点:结构比较清晰,容易阅读 ,主要配置文件
    比如 server:
    ----------- port : 9001

7.SpringBoot测试的使用

需要的依赖

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.12version>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
dependency>
  • 测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringTestApp.class)
public class SpringTest {
    @Test
    public void test() throws Exception{
        System.out.println(111);
    }
}

8.SpringBoot对springjdbc的支持

需要的依赖

<dependencies>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
    dependency>

    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-jdbcartifactId>
    dependency>
dependencies>

项目结构

初识SpringBoot_第2张图片

  • 持久层接口
public interface IUserDao {

    public void save(User user);
}

  • 持久层实现
@Repository
public class UserDaoImpl implements IUserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void save(User user) {
        jdbcTemplate.update("insert into t_user(name) values (?)",user.getName());
    }
}

9.SpringBoot对Mybatis支持

需要的依赖

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-testartifactId>
dependency>

<dependency>
		<groupId>mysqlgroupId>
		<artifactId>mysql-connector-javaartifactId>
dependency>	


<dependency>
	<groupId>org.mybatis.spring.bootgroupId>
	<artifactId>mybatis-spring-boot-starterartifactId>
	<version>1.1.1version>
dependency>
	
<dependency>
	<groupId>com.github.pagehelpergroupId>
	<artifactId>pagehelperartifactId>
	<version>4.1.0version>
dependency>	
  • 持久层

public interface UserMapper {

    @Insert("insert into t_user(name) values(#{name})")
    public void save(User user);

    public void saveXml(User user);

    //分页查询
    public List<User> queryPageAll();
}

  • service层实现
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public void save(User user) {
        userMapper.save(user);
    }

    @Override
    public void saveXml(User user) {
        userMapper.saveXml(user);
    }

    @Override
    public List<User> queryPageAll() {
        PageHelper.startPage(2,3);
        List<User> users = userMapper.queryPageAll();
        return users;
    }
}
  • 启动类
@SpringBootApplication
@MapperScan("cn.wing.mapper")
public class SpringMybatisApp {

    public static void main(String[] args) {
        SpringApplication.run(SpringMybatisApp.class);
    }
}

需要在这里开启mapper层的扫描

  • 分页插件
@Configuration
public class MyBatisConfiguration {

    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        //配置分页的参数设置
        //和startPage中的pageNum效果一样
        p.setProperty("offsetAsPageNum", "true");
        //设置为true时,使用RowBounds分页会进行count查询
        p.setProperty("rowBoundsWithCount", "true");
        // 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }

}

你可能感兴趣的:(java,java,SpringBoot)