SpringBoot+mybatis 详细步骤

SpringBoot+mybatis简单例子

采用idea工具

1、新建工程


2、选择springboot,点击下一步


 

3、勾选web项目


4、完成,等待项目部署完成

5、新建数据库person表,以及添加测试数据,附加sql语句


DROP TABLE IF EXISTS `t_person`;
CREATE TABLE `t_person` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_person
-- ----------------------------
INSERT INTO `t_person` VALUES ('1', '张三', '23');
INSERT INTO `t_person` VALUES ('2', '李四', '26');
INSERT INTO `t_person` VALUES ('3', '王五', '32');

6、创建实体类

package com.example.domain;

public class Person {
    private int id;//id
    private String name;//名称
    private int age;//年龄

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

7、创建mapper

package com.example.mapper;

import com.example.domain.Person;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;


@Mapper//mapper注解说明
public interface PersonMapper {

    //采用注解方式查询实体
    @Select("select id,name,age from t_person")
    public List<Person> selectList();

}

8、创建service接口

package com.example.service;

import com.example.domain.Person;

import java.util.List;

public interface PersonService {

    public List<Person> selectList();
}

9、创建service类

package com.example.service.impl;

import com.example.domain.Person;
import com.example.mapper.PersonMapper;
import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service //service注解
public class PersonServiceImpl implements PersonService {

    @Autowired
    PersonMapper mapper;//引用对象mapper

    @Override
    public List<Person> selectList() {
        return mapper.selectList();
    }
}

10、创建controller类

package com.example.controller;

import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PersonController {
    @Autowired
    PersonService personService;

    @RequestMapping("selectlist")
    public Object selectList(){
        return personService.selectList();
    }
}

11、application.properties 配置mybaitls数据库连接信息


spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

12、在启动类添加配置信息,扫描controller注解,service注解,mapper注解

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.example.*")
@MapperScan("com.example.mapper")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

13、启动项目,访问路径 http://localhost:8080/selectlist,拉取person集合



发布了19 篇原创文章 · 获赞 5 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_42165041/article/details/80242826
今日推荐