曾经有过用spring springMVC Hibernate(SSH)框架开发项目的经验,每次看见xml配置文件就头疼。开学之初,在曾师兄的推荐下开始折腾spring boot,学习spring boot就像在黑暗中看到了光明一样,真爽,头也不疼了。今天将会介绍如何整合springboot和mybatis。
环境介绍
在准备搭建项目之前,我们需要准备好开发工具及其环境。
- JDK
- eclipse
- Maven
关于上面的安装及其配置,这里不再介绍,网上有很多教程,可以完成。
在eclipse中生成spring boot项目
在eclipse中添加spring tool插件
- Help -> Eclipse Marketplace
- 选择“Popular”标签去查找Spring Tools 3 Add-On插件,找到后在线安裝
同样,自己也可以官网下载插件后在本地进行安装。
创建spring boot应用
- New -> Project
- Spring Boot ->Sping Starter Project
- 填写项目的信息,其中由于我们用maven管理项目,需要将type选择Maven,其他根据自己填写
- 选择需要Dependency,我们这里需要选择MysqlDriver,Mybatis,Spring Web Starter
5.将version 2.1.5 改为2.1.4,不要问我为什么,因为我用2.1.5一直有错,我就强行给他改了
项目结构
修改配置文件以及编码
在application.properties文件中添加mybatis的配置信息
# 端口
server.port=8080
# mybatis配置
#url
spring.datasource.url=jdbc:mysql://localhost:3306/test1610?characterEncoding=utf-8&useSSL=true
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=root
#驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
bean中的代码
//User.java
package top.datablog.bean;
public class User {
private int id;
private String name;
private String sex;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the sex
*/
public String getSex() {
return sex;
}
/**
* @param sex the sex to set
*/
public void setSex(String sex) {
this.sex = sex;
}
}
controller中的代码
//Student.java
package top.datablog.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.datablog.bean.User;
import top.datablog.service.UserService;
@RestController
public class Student {
@Autowired
private UserService userService;
@RequestMapping("/")
public String hello() {
return "你好";
}
@RequestMapping("/user")
public List<User> findAllUser(){
return userService.findAllUser();
}
}
mapper中的代码
//UserRepository.java
package top.datablog.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import top.datablog.bean.User;
@Mapper
public interface UserRepository {
@Select("select * from mou_user")
List<User> findAllUser();
}
service中的代码
//UserService.java
package top.datablog.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.datablog.bean.User;
import top.datablog.mapper.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> findAllUser(){
return userRepository.findAllUser();
}
}
运行项目
运行DemoApplication 中的main方法:Run As -> Spring Boot App)
DemoApplication的代码
package top.datablog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}