SpringBoot整合Mybatis快速开发项目

SpringBoot整合Mybatis快速开发项目

1.创建springboot项目,file->project->
之后确定groupid等一路next即可,选择自己需要添加的依赖!
pom文件如下

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

<dependencies>
    <!--jdbc        -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- mysql       -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!--    mysql-connector    -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--loombok    -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

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

2.创建项目结构controller:自己要在浏览器里面输入路径,要访问的url
pojo:实体类
mapper:要对实体类进行操作的接口文件
service:对接口里面内容的具体实现,业务逻辑层在该层实现
注意:所有的内容都必须在主类外面,项目启动之后会扫面该包下的内容
resources下面创建mapper,里面存放对数据库操作的sql内容
推荐使用官方推荐使用的yaml文件配置数据源和mybatis存放位置等内容

2 编写代码步骤
实体类:
person:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    Integer id;
    String name;
    int age;
    Date birth;
}

使用lombok可以省略手动配置getter和setter方法,以及有参和无参构造方法
PersonMapper:接口

import com.yzj.pojo.Person;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PersonMapper {
    //根据id进行查询
    Person findById(Integer id);
    //查询所有
    List<Person> findAll();

    //添加
    void addPerson(Person person);

    //删除
    void delById(Integer id);

    //修改
    void updatePerson(Person person);
}

PersonService:接口实现类,业务层逻辑内容

@Service
@Repository
public class PersonService {
    @Autowired
    PersonMapper personMapper;

    //查询根据id
    public Person findById(Integer id){
        return personMapper.findById(id);
    }

    //查询所有
    public List<Person> findAll(){
        return personMapper.findAll();
    }

    //添加
    public void addPerson(Person person){
        personMapper.addPerson(person);
    }

    //删除根据id
    public void delById(Integer id){
        personMapper.delById(id);
    }

    //修改
    public void updateperson(Person person){
        personMapper.updatePerson(person);
    }
}

PersonController:逻辑视图层

import com.yzj.pojo.Person;
import com.yzj.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/test")
public class PersonController {
    @Autowired
    public PersonService personService;

    @GetMapping("person/{id}")
    public String findBy(@PathVariable Integer id){
        System.out.println(personService.findById(id));
        return personService.findById(id).toString();
    }

    @GetMapping("person")
    public List<Person> findAll(){
        return personService.findAll();
    }

    @GetMapping("person/add")
    public List<Person> addPerson(){
        Person person = new Person();
        person.setAge(30);
        person.setBirth(new Date());
        person.setName("Demo");
        person.setId(3);
        personService.addPerson(person);
        return personService.findAll();
    }

    @GetMapping("person/del/{id}")
    public List<Person> delPerson(@PathVariable Integer id){
        personService.delById(id);
        return personService.findAll();
    }
    @GetMapping("person/update")
    public List<Person> updatePer(){
        Person person = new Person(3,"Demo",18,new Date());
        personService.updateperson(person);
        System.out.println(personService.findAll());
        return personService.findAll();
    }

}

3.mapper.xml:该文件下写crud sql语句

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yzj.mapper.PersonMapper">
    <resultMap id="BaseResultMap" type="com.yzj.pojo.Person">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="age" jdbcType="INTEGER" property="age" />
        <result column="birth" jdbcType="TIMESTAMP" property="birth" />
    </resultMap>
    <select id="findById" resultType="com.yzj.pojo.Person">
        select * from person where id = #{id}
    </select>
    <select id="findAll" resultType="com.yzj.pojo.Person">
        select * from person
    </select>

    <insert id="addPerson" >
        insert into person values (#{id},#{name},#{age},#{birth})
    </insert>

    <delete id="delById" >
        delete from person
            where id = #{id}
    </delete>

    <update id="updatePerson" >
        update person
            set name = #{name},age=#{age},birth=#{birth}
            where id = #{id}
    </update>
</mapper>

4.yaml文件

server:
  port: 8080
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*Mapping.xml
#  type-aliases-package: com.yzj.pojo

5项目整体框架

猜你喜欢

转载自blog.csdn.net/qq_43147591/article/details/112556814