mybatis-plus+mybatis+Spring的使用

mybatis+mybatis-plus+spring的使用:

1.添加依赖:

<properties>
  <spring.version>5.1.6.RELEASE</spring.version>
</properties>
<dependencies>

  <!--Mysql-->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
  </dependency>

  <!-- mybatis-plus插件依赖 -->
  <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.1.1</version>
  </dependency>

  <!--连接池-->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.11</version>
  </dependency>

  <!--简化bean代码的⼯具包-->
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.4</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.1</version>
    <scope>compile</scope>
  </dependency>

</dependencies>

2.配置文件:

3.实体类:

 

4.接口文件:

 

5.测试:

注意:

1.当实体类的名称和数据库表名不一致的时候,可以使用注解:@TableName("  ")

 

 2.当数据库中字段名和实体类属性名不一致时,可使用注解:@TableField(value="  ")

 

3.当不想要使用者能够直接查询到数据库中某个记录的所有字段信息,想要屏蔽某些信息时,可使用注解:@TableField(select=false)

 4.使用mybatis后,不用在mapper接口文件中自己写方法,可直接继承BaseMapper接口,从而直接使用其中的CRUD方法。定义泛型类型,直接使用即可。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是基于 Spring Boot、MyBatis-Plus、Thymeleaf 和 Bootstrap 的留言板的实现步骤: 1. 创建 Spring Boot 项目 在 IDEA 中创建一个 Spring Boot 项目,选择 Web 和 Thymeleaf 作为依赖。 2. 添加 MyBatis-Plus 依赖 在 pom.xml 文件中添加 MyBatis-Plus 依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> ``` 3. 创建留言板数据表 在 MySQL 中创建一个名为 message_board 的数据表,包含以下字段: ```sql CREATE TABLE `message_board` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL COMMENT '留言人姓名', `email` varchar(50) NOT NULL COMMENT '留言人邮箱', `content` varchar(255) NOT NULL COMMENT '留言内容', `create_time` datetime NOT NULL COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='留言板'; ``` 4. 创建 MyBatis-Plus 实体类和 Mapper 使用 MyBatis-Plus 的代码生成器,生成 Message 实体类和 MessageMapper 接口。 5. 编写 Service 层 创建 MessageService 接口和 MessageServiceImpl 实现类,其中 MessageServiceImpl 实现类注入 MessageMapper,实现增删改查等方法。 6. 编写 Controller 层 创建 MessageController 类,其中注入 MessageService,实现留言板的展示、添加留言和删除留言等功能。 7. 编写 Thymeleaf 页面 在 templates 目录下创建 message.html 页面,使用 Thymeleaf 和 Bootstrap 实现留言板的展示和添加留言的表单。 8. 运行项目 使用 IDEA 运行项目,在浏览器中访问 http://localhost:8080/message 即可看到留言板页面。 以上就是基于 Spring Boot、MyBatis-Plus、Thymeleaf 和 Bootstrap 的留言板的实现步骤,你可以参考这些步骤来实现自己的留言板。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值