spring boot
在 spring 基础上进行了简化,提供一些配置的默认值,大大减少了配置。
创建spring boot项目
- 方法一 使用向导(web 页面), 要联网
- 方法二 使用 maven 项目, 需要配置 pom.xml
pom.xml 参考:
<!-- 1. parent 中是一些公共的pom 配置, 我们的项目从中继承即可 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <!-- 2. 设置 jdk 版本--> <properties> <java.version>1.8</java.version> </properties> <!-- 3. 添加 spring boot 相关依赖 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 4. 添加打包插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
springboot 推荐的打包方式是 jar
编写spring boot代码及运行
如果是使用向导的方式下载springboot包的话就不需要编写这个代码,人家以及帮你弄好了,如果是手动创建的话,需要在入口类上加一个注解 然@SpringBootApplication,后在 main方法中使用, 来启动springboot 应用程序,如下写在main方法中:
SpringApplication.run(启动类名.class, args); //启动类名也就是这个mian方法所在的类的类名
例子如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
// 启动 spring boot
SpringApplication.run(SpringbootApplication.class, args);
}
}
spring boot 整合 mybatis
1)加入依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
2)mapper 接口上要添加一个 @Mapper 注解,这样子就不需要写service的接口了,直接写service类
3)在 application.properties 配置文件中加入连接数据库的信息,如下:
//serverTimezone 是时区,这里是上海时间 spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai spring.datasource.username=用户名 spring.datasource.password=密码
代码演示
spring boot 结合 mybatis 例子
application.properties 中的配置,连接数据库
spring.datasource.url=jdbc:mysql://localhost:3306/school?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
Student类,用来存储返回的结果
import java.util.Date;
public class Student {
private int sid;
private String sname;
private Date birthday;
private String sex;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
mapper接口,写SQL语句
import com.westos.day47springboot.domain.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentDao {
@Select("select * from student")
public List<Student> findAll();
}
返回调用 mapper 接口方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
public List<Student> findAll() {
return studentDao.findAll();
}
}
控制器类
import com.westos.day47springboot.domain.Student;
import com.westos.day47springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class MyController {
//依赖注入
@Autowired
private StudentService studentService;
@RequestMapping("/all")
//把 java 对象转换成 json 字符串
@ResponseBody
public List<Student> all() {
List<Student> all = studentService.findAll();
//输出取到的集合
for (Student student : all) {
System.out.println(student);
}
//把 json 字符串返回给 all 页面
return all;
}
}
启动 spring boot,写在类的包的外面,管理所有包,如java包下分多个包分别创建类,那么下面这个类就直接写在java包下
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Day47SpringbootApplication {
public static void main(String[] args) {
// 启动 spring boot
SpringApplication.run(Day47SpringbootApplication.class, args);
}
}
结果
浏览器页面
服务器(控制台)
这里就不需要向以前那样写出服务层的接口了,直接写出服务层的类,表面上是从 mapper 接口获得的获得数据,实际还是和原来一样是从服务层接口获得的返回值数据,只不过 spring boot 在底层已经帮忙做了。