[Spring Boot + MyBatis + MySQL框架搭建]

目录

🐉创建一个新的Spring Boot项目

🐉配置文件

🐉application.properties配置:

🐉创建实体类

🐉创建Mapper接口

🐉创建Mapper XML文件

🐉创建Service和Controller

🐉创建一个Controller类,用于处理HTTP请求和响应。在src/main/java目录下创建一个名为“com.example.demo.controller”的包,并在其中创建一个名为“UserController”的Java类,代码如下:

🐉运行应用程序


🐉创建一个新的Spring Boot项目

首先,我们需要在Maven中创建一个新的Spring Boot项目。在pom.xml中添加所需的依赖,包括Spring Boot Web、MyBatis、MySQL驱动等。具体的依赖项如下:

<dependencies>
    <!-- Spring Boot Web依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- MyBatis依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.3</version>
    </dependency>

    <!-- MySQL驱动依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
    </dependency>
</dependencies>

 

🐉配置文件

在application.properties或application.yml文件中配置应用程序,包括端口、数据库连接信息等。

🐉application.properties配置:

# 服务端口号
server.port=8080

# MySQL相关配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# MyBatis配置
mybatis.type-aliases-package=com.example.demo.entity
mybatis.mapper-locations=classpath:/mapper/*.xml

 

🐉创建实体类

我们需要定义一个实体类来映射数据库中的表。在src/main/java目录下创建一个名为“com.example.demo.entity”的包,并在其中创建一个名为“User”的Java类,代码如下:

public class User {
    private Long id;
    private String name;
    private int age;

    // getter和setter方法
}

🐉创建Mapper接口

创建一个Mapper接口,用于定义访问表的CRUD方法。在src/main/java目录下创建一个名为“com.example.demo.mapper”的包,并在其中创建一个名为“UserMapper”的Java接口,代码如下:

@Mapper
public interface UserMapper {
    List<User> findAll();
    User findById(Long id);
    int addUser(User user);
    int updateUser(User user);
    int deleteUser(Long id);
}

在这里,我们使用了@Mapper注释来标识这是一个Mapper接口。接口中定义了一些CRUD方法。

🐉创建Mapper XML文件

在src/main/resources目录下创建一个名为“mapper”的文件夹,并在其中创建一个名为“UserMapper.xml”的文件,

<?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.example.demo.mapper.UserMapper">
    <resultMap id="userResultMap" type="com.example.demo.entity.User">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
    </resultMap>

    <select id="findAll" resultMap="userResultMap">
        select id, name, age from user
    </select>

    <select id="findById" resultMap="userResultMap" parameterType="Long">
        select id, name, age from user where id = #{id}
    </select>

    <insert id="addUser" parameterType="com.example.demo.entity.User" useGeneratedKeys="true" keyProperty="id">
        insert into user(name, age) values(#{name}, #{age})
    </insert>

    <update id="updateUser" parameterType="com.example.demo.entity.User">
        update user set name = #{name}, age = #{age} where id = #{id}
    </update>

    <delete id="deleteUser" parameterType="Long">
        delete from user where id = #{id}
    </delete>
</mapper>

 

🐉创建Service和Controller

创建一个Service类,用于实现业务逻辑。在src/main/java目录下创建一个名为“com.example.demo.service”的包,并在其中创建一个名为“UserService”的Java类,代码如下:

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> findAll() {
        return userMapper.findAll();
    }

    public User findById(Long id) {
        return userMapper.findById(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);
    }
}

在这里,我们使用了@Service注释来标识这是一个Service类,使用@Autowired注释来自动注入Mapper对象。

🐉创建一个Controller类,用于处理HTTP请求和响应。在src/main/java目录下创建一个名为“com.example.demo.controller”的包,并在其中创建一个名为“UserController”的Java类,代码如下:

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/")
    public List<User> findAll() {
        return userService.findAll();
    }

    @GetMapping("/{id}")
    public User findById(@PathVariable Long id) {
        return userService.findById(id);
    }

    @PostMapping("/")
    public void addUser(@RequestBody User user) {
        userService.addUser(user);
    }

    @PutMapping("/")
    public void updateUser(@RequestBody User user) {
        userService.updateUser(user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
    }
}

在这里,我们使用了@RestController注释来标识这是一个Controller类,使用@Autowired注释来自动注入Service对象。使用@GetMapping、@PostMapping、@PutMapping和@DeleteMapping注释来定义HTTP请求方法。

🐉运行应用程序

运行DemoApplication.java中的main方法,启动应用程序。

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

在这里,我们使用了@MapperScan注释来扫描Mapper接口并将其注册为MyBatis映射器。

在浏览器或Postman中输入http://localhost:8080/user就是代码访问 列表接口

以上是一个简单的Spring Boot + MyBatis + MySQL框架搭建的详细过程示例,具体实现过程中还需要根据实际情况进行完善和调整

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
layui是一款基于JavaScript的前端UI框架,用于快速构建美观、交互友好的网页界面。它提供了丰富的UI组件和交互功能,可以帮助开发人员高效地进行前端开发。 Spring Boot是一个用于简化Spring应用程序开发的框架,它通过自动配置和约定优于配置的方式,让开发人员可以更专注于业务逻辑的实现,而不是繁琐的配置。Spring Boot还提供了很多常用的功能库和第三方插件的集成,可以大大提高开发效率。 MyBatis是一款优秀的持久层框架,可以帮助开发人员将数据库操作与业务逻辑分离,提供了灵活、简单且强大的数据访问方式。MyBatis提供了很多注解和XML配置文件的方式,可以方便地进行SQL语句的编写和执行。同时,MyBatis还提供了缓存机制和插件机制,可以进一步优化数据库操作的性能。 MySQL是一种关系型数据库管理系统,它被广泛应用于各种规模的应用程序中。MySQL提供了稳定、可靠和高性能的数据库服务,支持标准的SQL查询语言和事务处理。在开发过程中,我们可以通过连接MySQL数据库来存储和管理应用程序的数据。 综上所述,layui、Spring BootMyBatisMySQL可以一起使用来构建具有美观、高效和可靠性的Web应用程序。layui提供了丰富的前端UI组件和交互功能,Spring Boot可以简化后端业务逻辑的开发,MyBatis可以实现数据库操作的分离和优化,而MySQL可以提供稳定和高性能的数据库服务。这样的组合可以大大提高开发效率和系统性能,是一种常见的技术栈选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是汤圆丫

怎么 给1分?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值