springboot入门

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:

org.springframework.boot spring-boot-starter-web mysql mysql-connector-java
	<!-- 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);
	}
}



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值