SpringBoot环境搭建
见之前博客内容:SpringBoot环境搭建
SpringBoot整合MyBatis
工程结构目录图
pom文件中导入相关依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
配置application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/testdemo?useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
创建相应数据库以及数据库表
创建pojo实体对象,使用lombok插件生成getset方法
package com.ljctest.pojo;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
private Integer id;
private String username;
private String password;
}
编写dao接口
package com.ljctest.dao;
import com.ljctest.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface HelloDao {
@Select("select * from user")
List<User>findAll();
}
编写controller、service
package com.ljctest.service;
import com.ljctest.dao.HelloDao;
import com.ljctest.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HelloService {
@Autowired
HelloDao dao;
public List<User> findAll(){
return dao.findAll();
}
}
package com.ljctest.controller;
import com.ljctest.pojo.User;
import com.ljctest.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("hello")
public class HelloController {
@Autowired
HelloService service;
@RequestMapping("testOne")
public String testOne(){
return "success";
}
@RequestMapping("findAll")
public List<User>findAll(){
return service.findAll();
}
}
使用了阿里的json解析相关依赖。
编写引导类,启动测试。
package com.ljctest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 引导类
*/
@SpringBootApplication
public class ApplicationTest {
public static void main(String[] args) {
SpringApplication.run(ApplicationTest.class,args);
}
}