Springboot和mybatis-plus整合

1、使用IDEA创建springboot项目

 

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;

}

 

截图:

 

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

截图:

项目结构:

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值