1、使用IDEA创建springboot项目
![](https://i-blog.csdnimg.cn/blog_migrate/63794006cbc0afbc36421affa674a918.png)
2、修改pom文件,添加依赖
添加mybatis-plus、mysql、lombok
注意:lombok需要在IDEA中安装lombok插件,然后才能使用。
<!--pojo注解生成get、set方法--> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency> <!--mybatis-plus插件--> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> |
3、配置数据库的连接
修改application.properties的后缀为yml
#配置端口
server:
port: 8083
#配置数据源
spring:
datasource:
url: jdbc:mysql://106.15.182.225:3306/mymp?useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver |
4、创建pojo实体类
创建实体类,属性名需要和数据库字段名相同
这里生成get、set方法使用的使用Lombok,注解生成get、set方法
/**
* @author 晓敏
* @create 2019-07-19 12:56
*/
@Getter
@Setter
public class Student {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private Date birthday;
} |
截图:
![](https://i-blog.csdnimg.cn/blog_migrate/043cd3379385d5a4efddda8e7f69f5ea.png)
5、创建我们的mapper接口文件
继承我们mybatis-plus提供的BaseMapper接口,这个接口为我们提供了mapper接口和SQL,进行单表的操作,很方便。不用像我们之前mybatis要写mapper和sql的xml文件。简化了我们单表查询。
/**
* @author 晓敏
* @create 2019-07-19 13:02
*/
public interface StuDao extends BaseMapper<Student> {
} |
6、创建server接口和serviceImpl实现类
server接口
/**
* @author 晓敏
* @create 2019-07-19 13:03
*/
public interface StudentService {
//查询所有的学生信息
public List<Student> selectAll();
//插入学生信息
public int insert();
} |
serviceImpl接口实现类
/**
* @author 晓敏
* @create 2019-07-19 13:04
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StuDao stuDao;
/**
* 查询所有的学生信息
* @return
*/
@Override
public List<Student> selectAll() {
List<Student> list = stuDao.selectList(null);
return list;
}
/**
* 插入学生信息
* @return
*/
@Override
public int insert() {
Student student = new Student();
student.setName("张三");
student.setAge(20);
student.setBirthday(new Date());
int i = stuDao.insert(student);
return i;
}
} |
7、创建我们的controler类
/**
* @author 晓敏
* @create 2019-07-19 11:57
*/
@RestController
public class StuController {
@Autowired
private StudentService studentService;
/**
* 测试方法
* @return
*/
@RequestMapping("/hello")
public String hello(){
return "springboot-mybatis-plus hello world";
}
/**
* 查询所有的学生信息
* @return
*/
@RequestMapping("/selectAll")
public List<Student> selectAll(){
List<Student> list = studentService.selectAll();
return list;
}
@RequestMapping("/insert")
public String insert(){
int i= studentService.insert();
return i+"";
}
} |
8、启动类配置注解扫描
@SpringBootApplication(scanBasePackages = "com.xiaomin") @MapperScan("com.xiaomin.mapper") public class SpringbootMpApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootMpApplication.class, args); }
} |
9、访问
访问地址:http://localhost:8083/selectAll
截图:
![](https://i-blog.csdnimg.cn/blog_migrate/41bc388eb09746ab4da58fe83eae4cf9.png)
项目结构:
![](https://i-blog.csdnimg.cn/blog_migrate/cc64fc404b82869af48c050f240030b4.png)