第0篇文章:springboot完整开发一个接口(Conrtroller-Service-Mapper-Entity/Dao体系)

 实现功能:查询出数据表中所有数据

 开发从底层往上层开发(先entity-mapper/dao-service-controller),调用,从上层往下层调用(controller-service-mapper/dao-entity)

注意:如果用mybatisplus代替mybatis,甚至连mapper.xml都不用写

准备数据:先是在数据库中新建一张表animal,里面有四个字段:id,name,age,email

 在application.properties文件中(或者事application.yml)进行数据库信息的配置:

server:
    port: 8089    #服务器端口

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver   #数据库驱动,固定的写法

    #3306表示数据库的端口,mybatis_plus是数据库的名字
    url: jdbc:mysql://127.0.0.1:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    
    username: root
    password: 123456

mybatis:
  mapper-locations:classpath: Mapper/*.xml

step1:新建有一个Entity包(或者事Dao包),将数据表映射成类

public class Animal
{
    private int id;
    private String name;
    private  int age;
    private String email;
}

step2:创建Mapper包,在Mapper包里面创建AnimalMapper接口(注意这里也有人不取名为Mapper,而是取名为Dao包,然后在Dao包里面创建AnimalDao接口)

@Mapper
public interface AnimalMapper
{
   public List find3();
}

同时在resources文件夹下新建一个Mapper包,在Mapper包下创建AnimalMapper.xml

将接口中的方法转化为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">

<!--命名空间是AnimalMapper接口的路径,选择这个接口中的哪个方法(通过id选择),返回类型是一个数据表映射的类的对象-->
<mapper namespace="com.example.springboot_test.Mapper.AnimalMapper">
      <select id="find3"  resultType="com.example.springboot_test.entity.Animal">
                SELECT * FROM animal
      </select>
</mapper>

step3:创建Service包,在Service包里面创建AnimalService类,在这个类本身的find2方法中调用AnimalMapper类的find3方法

    @Autowired
    private AnimalMapper animalMapper;

    public List<Animal> find2()
    {
       return animalMapper.find3();
    }

step4:创建Controller包,在Controller包里面创建AnimalController类,在这个类本身的find1方法中调用AnimalService类的find2方法

    @Autowired
    private AnimalService animalService;

    @RequestMapping("/test")
    public List<Animal> find1()
    {
        return animalService.find2();
    }

最后启动服务器,在浏览器地址栏中输入:localhost:8080/test:

最后浏览器显示:

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 Spring Boot 项目中,通常会采用 MVC 架构的设计思路,将代码按照功能分为 MapperEntityService 和 Controller 四个层次。 1. Mapper Mapper 层是连接数据库和业务逻辑的桥梁,它使用 MyBatis 等 ORM 工具来实现数据库操作。在 Mapper 层中,通常会定义以下内容: - 定义 SQL 语句和参数映射 - 定义查询条件和排序方式 - 定义对数据库的增删改查操作 Mapper 层的示例代码如下: ``` @Mapper public interface UserMapper { @Select("select * from user where id=#{id}") User getUserById(Long id); @Insert("insert into user(name, age) values(#{name}, #{age})") int addUser(User user); @Update("update user set name=#{name}, age=#{age} where id=#{id}") int updateUser(User user); @Delete("delete from user where id=#{id}") int deleteUser(Long id); } ``` 2. Entity Entity 层是与数据库表进行映射的实体类。在 Entity 层中,通常会定义以下内容: - 定义实体类的属性和对应的数据库表字段 - 定义实体类之间的关系 Entity 层的示例代码如下: ``` @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; // 省略 getter 和 setter 方法 } ``` 3. Service Service 层是业务逻辑的处理层,它主要负责处理业务逻辑和调用 Mapper 层完成数据库操作。在 Service 层中,通常会定义以下内容: - 定义业务逻辑的方法 - 定义事务的管理方法 Service 层的示例代码如下: ``` @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.getUserById(id); } public int addUser(User user) { return userMapper.addUser(user); } public int updateUser(User user) { return userMapper.updateUser(user); } public int deleteUser(Long id) { return userMapper.deleteUser(id); } } ``` 4. ServiceImpl ServiceImpl 层是 Service 层的实现类,它实现了 Service 层定义的业务逻辑方法,并调用 Mapper 层完成数据库操作。在 ServiceImpl 层中,通常会定义以下内容: - 实现 Service 层定义的业务逻辑方法 - 定义事务的管理方法 ServiceImpl 层的示例代码如下: ``` @Service @Transactional public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.getUserById(id); } @Override public int addUser(User user) { return userMapper.addUser(user); } @Override public int updateUser(User user) { return userMapper.updateUser(user); } @Override public int deleteUser(Long id) { return userMapper.deleteUser(id); } } ``` 以上就是 Spring Boot 中 MapperEntityServiceServiceImpl 四个层次的概述和示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值