SpringBoot整合Mybatis(基于注解方式实现)

Sprintboot整合mybatis(注解方式)

前言:环境配置

idea+jdk8+springboot+mysql+mybatis

第一步:数据库新建Person表

DROP TABLE IF EXISTS `person`;
CREATE TABLE `person`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 
    COLLATE utf8_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 
CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

这个表结构很简单,也就是三个字段id、name、age。并以id为主键且递增。

第二步:新建Springboot项目

选择File->new->Project->Spring Initializr

第三步:导入相关依赖

 
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.1version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.44version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>

OK,我们只需要加上这些依赖即可。在我们的pom.xml文件。

第四步:更改application.yml配置文件

我们只需要把application.properties文件改为yml格式即可。此时添加相关配置

#配置服务器信息
server:
  port: 8080
spring:
  #mysql数据库相关配置
  datasource:
    url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
#mybatis依赖
mybatis:
  type-aliases-package: com.homyit.mybatis.dao

这里的配置有点多,不过还是一个最基本的配置都在这。

第五步:新建dao包,在dao包下新建Person类

public class Person {
    private int id ;
    private String name;
    private int  age;
    public Person() {
    }
    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    //getter和setter方法
    //toString方法
}

这个类是和我们数据库中的Person类一一对应的。

第六步:新建mapper包,在mapper新建PersonMapper类

在这个类中,我们实现基本的增删改查功能接口:

@Mapper
public interface PersonMapper {
    //增加一个Person
    @Insert("insert into person(id,name,age)values(#{id},#{name},#{age})")
    int insert(Person person);
    //删除一个Person
    @Delete("delete from person where id = #{id}")
    int deleteByPrimaryKey(Integer id);
    //更改一个Person
    @Update("update person set name =#{name},age=#{age} where id=#{id}")
    int updateByPrimaryKey(Integer id);
    //查询一个Person
    @Select("select id,name ,age from person where id = #{id}")
    Person selectByPrimaryKey(Integer id);
    //查询所有的Person
    @Select("select id,name,age from person")
    List<Person> selectAllPerson();
}

这就是最基本的一个增删改查操作的接口。

第七步:新建service包,在service包创建PersonService接口

public interface PersonService {
    //增加一个Person
    int insertPerson(Person person);
    //删除一个Person
    int deleteByPersonId(Integer id);
    //更改一个Person
    int updateByPersonId(Person record);
    //查询一个Person
    Person selectByPersonId(Integer id);
    //查询所有的Person
    List<Person> selectAllPerson();
}

第八步:在service包下创建PersonServiceImpl接口实现类

@Service
public class PersonServiceImpl implements  PersonService {
    @Autowired
    private PersonMapper personMapper;
    @Override
    public int insertPerson(Person person) {
        return personMapper.insert(person);
    }
    @Override
    public int deleteByPersonId(Integer id) {
        return personMapper.deleteByPrimaryKey(id);
    }
    @Override
    public int updateByPersonId(Person record) {
        return personMapper.updateByPrimaryKey(record);
    }
    @Override
    public Person selectByPersonId(Integer id) {
        return personMapper.selectByPrimaryKey(id);
    }
    @Override
    public List<Person> selectAllPerson() {
        return personMapper.selectAllPerson();
    }
}

第九步:编写controller层

@RestController
public class PersonController {
    @Autowired
    private PersonService personService;
    @RequestMapping(value = "/add")
    public String students () {
        Person person = new Person();
        person.setId(1);
        person.setName("Homyit Studio");
        person.setAge(18);
        int result = personService.insertPerson(person);
        System.out.println("插入的结果是:"+result);
        return result+"";
    }
    @RequestMapping(value = "/findAll")
    public String findAll () {
        List<Person> people = personService.selectAllPerson();
        people.stream().forEach(System.out.println);
        return people.toString()+"";
    }
}

第十步:在启动主类添加扫描器

@SpringBootApplication
@MapperScan("com.homyit.mybatis.mapper")
public class SpringBootMybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisApplication.class, args);
    }
}

你可能感兴趣的:(javaee)