springboot与mybatis的集成注解版

1.整体目录
在这里插入图片描述
2.代码

  • person
 package com.example.demo.bean;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@ToString
@Getter
@Setter
public class Person {
    private int id;
    private String name;
    private int age;
}
  • PersonController
package com.example.demo.controller;

import com.example.demo.bean.Person;
import com.example.demo.sercive.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
    private PersonService personService;

    @RequestMapping(value = "/add")
    public String students(){
        Person person = new Person();
        person.setId(2);
        person.setName("哒哒");
        person.setAge(18);
        int result = personService.insertPerson(person);
        return result+"";
    }

    @RequestMapping(value = "/selectAll")
    public void  selectAllStudent(){
        System.out.println( personService.selectAllPerson());
    }
}
  • PersonMapper
package com.example.demo.mapper;

import com.example.demo.bean.Person;
import org.apache.ibatis.annotations.*;

import java.util.List;

@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(Person person);

    //查询一个person
    @Select(" select * from person where id = #{id}")
    Person selectByprimaryKey(Integer id);

    //查询所有额Person
    @Select(" select * from person ")
    List<Person> selectAllPerson();
}
  • PersonServiceImpl
package com.example.demo.sercive.impl;

import com.example.demo.bean.Person;
import com.example.demo.mapper.PersonMapper;
import com.example.demo.sercive.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    public int insertPerson(Person person) {
        return personMapper.insert(person);
    }
    @Override
    public int deleteByprimaryKey(Integer id){
      return personMapper.deleteByprimaryKey(id);
    }
    @Override
    public int updateByPrimaryKey(Person person){
        return personMapper.updateByPrimaryKey(person);
    }
    @Override
    public Person selectByprimaryKey(Integer id){
        return personMapper.selectByprimaryKey(id);
    }
    @Override
    public List<Person> selectAllPerson(){
        return personMapper.selectAllPerson();
    }

}
  • PersonService
package com.example.demo.sercive;

import com.example.demo.bean.Person;

import java.util.List;

public interface PersonService {

    //增加一个person
    int insertPerson(Person person);
    int deleteByprimaryKey(Integer id);
    int updateByPrimaryKey(Person person);

    Person selectByprimaryKey(Integer id);

    List<Person> selectAllPerson();
}

 
  • DemoApplication
package com.example.demo;

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

@SpringBootApplication
//@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • application.yml
server:
  port: 8082
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  type-aliases-package: com.example.demo.mapper

 
  • pom.xml
 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- spring boot 开发web 项目的起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--加载mybatis 整合 spring boot  -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- mysql的jdbc 驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <!--测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--lombok get set -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.sql文件

DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(255) CHARACTER SET gbk DEFAULT NULL,
  `age` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `person` VALUES ('1', '小小', '18');
INSERT INTO `person` VALUES ('2', '哒s哒', '18');

4.百度网盘链接
链接:https://pan.baidu.com/s/1YP7pggr-LpQA6bn4lAH6tA
提取码:d880

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值