Spring Boot

  • 1.什么是Spring Boot

Springboot就是一些写好了maven的模块,我们在使用SPring就不需以传统的方式来用,只需要以maven导入对应的springboot模块,就能完成一大堆操作。简单的说,它使用maven的方式对Spring应用开发进行进一步封装和简化。

Springboot就是为了简化spring应用搭建,开发,部署,监控的开发工具。

  • 2.Spring boot入门

开发环境JDK 1.8
项目管理工具( Maven )
开发工具(Eclipse/idea)

2.1导入Spring Boot依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
	</parent>

<child>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


启动类完成启动 @SpringBootApplication SpringApplication.run
@SpringBootApplication//标识该应用为springboot的应用
public class HelloApplication {
public static void main(String[] args) {
//启动springboot应用
SpringApplication.run(HelloApplication.class);
    }
}
controller,启动后完成测试
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/hi1")
@ResponseBody //直接作为字符串返回而不是页面地址
public String sayHello(String name){
return name +" say hello";
    }
}


  • 3 热部署
添加依赖包: 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>	

添加spring-boot-maven-plugin:
<build>
		<plugins>
		<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<configuration>
			<!--fork :  如果没有该项配置,可能devtools不会起作用,即应用不会restart -->
	<fork>true</fork>
	</configuration>
	</plugin>
		</plugins>
</build>

注意:在idea中需要手动编译一下,ctrl+F9
  • 4.Spring boot web
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<!-- servlet 依赖. -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<scope>provided</scope>
	</dependency>

	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
	</dependency>

	<!-- tomcat 的支持. -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
		<scope>provided</scope>
	</dependency>

4.1 配置application.properties对jsp支持

添加src/main/resources/application.properties:

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp

4.1. Freemaker支持

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>

4.1.2 配置-application.properties

#       设定ftl文件路径
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

4.3.3. controller

@Controller
public class IndexController {
@RequestMapping("/index")
public String index(Model model){
        model.addAttribute("message", "呵呵!");
return "index";
    }
}

4.4. 获取Json数据

@Controller
@RequestMapping("/json1")
public class JsonController {

//字符串
@RequestMapping("/str")
@ResponseBody
public String json1(){
return "yhptest";
    }
//对象-日期

@RequestMapping("/obj")
@ResponseBody
public Person json2(){
return new Person(1L,"yhptest",new Date());
    }

//数组
@RequestMapping("/array")
@ResponseBody
public List<Person> json3(){
return Arrays.asList(new Person(1L,"yhptest",new Date())
                ,new Person(2L,"yaohuaipeng",new Date()));
    }
}

  • 5.springboot配置

YAML是YAML Ain’t Markup Language递归缩写,是YAML不是标记语言的意思,读音“yamel”(或者“雅梅尔”)。YAML是便于人阅读基于unicode编码的各种语言的序列号标准。它的用途广泛,用于配置文件,日志文件,跨语言数据共享,对象持久化,复杂的数据结构。

打包配置一:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>utf-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                    <mainClass>cn.itsource.freemark.FreemarkTest</mainClass> <!--主类 包含main-->
                    <layout>JAR</layout>
            </configuration>
        </plugin>
    </plugins>
</build>


spring:
  profiles:
    active: test
server:
  port: 9001
server:
  port: 9002
  • 6.SpringBoot测试
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>

SpringBoot测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class) //这事一个Spring测试,要告诉它在哪儿加载Spring配置文件,其实告诉它应用类型就ok
Public class SpringbootTest {
	@Test
	publicvoid test() throws Exception {
		System.out.println(1111);
	}
}

  • 7.集成三层架构(后面springboot持久化完成)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

8.2.2. 数据库信息配置

在application.properties文件中配置mysql连接配置文件
########################################################
###datasource
########################################################
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

1)事务管理器
2)开启注解,就是能够识别@Transactional
3)在要控制事务的Service的或方法上面加上@Transactional
建议:类级别为只读事务,需要写事务的方法上面加写事务

  • 8.3. Spring boot-mybtis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<!-- mysql 数据库驱动. -->
<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
</dependency>	

<!-- 	
			spring-boot mybatis依赖:
			
			请不要使用1.0.0版本,因为还不支持拦截器插件,
		1.1.1 是博主写帖子时候的版本,大家使用最新版本即可
	     -->
	<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.1.1</version>
	</dependency>
<!-- 
	MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
	将其作为一个plugin装入到SqlSessionFactory中。 
		Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。 
		Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper
     -->	
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>4.1.0</version>
	</dependency>	

(4)在application.properties添加配置文件;
########################################################
###datasource
########################################################
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

一:加上Mapper扫描路径
@MapperScan(“cn.itsource.springboot.mapper”)

1.打注解

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

2.xml版本

<insert id="savexml" parameterType="User">
        insert into t_user(name) values(#{name})
</insert>

配置别名
别名:mybatis.type-aliases-package=

8.3.2. 使用PageHelper分页

@Configuration //相当于我们建了applicationContext-xxx.xml <beans></beans>
public class MyBatisConfiguration {
	
    //相当于配置了一个bean
    //<bean class="com.github.pagehelper.PageHelper">
    //<property key="offsetAsPageNum"  value="true"></property>
    //<property key="rowBoundsWithCount"  value="true"></property>
    //<property key="reasonable"  value="true"></property>
    // <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;
    }
}

UserServiceImpl

 PageHelper.startPage(1, 3);
        List<User> users = userMapper.queryPageAll();
        System.out.println(users);
        System.out.println("assssssssssssss");
        for (User user:users){
            System.out.println(user);
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值