spring boot +Mybatis -plus 入门学习 搭建我的第一个项目

总结:

1、开始搭建mybatis-plus 时,报org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "USER" not found; 

2、报"unexpected status 1308622848" [90067-180]

以上的异常,我被spring boot 和h2的版本不匹配所引导在里面出不来了。

其实我们在配置这个spring.datasource数据库连接时,采用了h2的org.h2.Driver,因为没有弄清楚其原理,所以被带到里面好长时间没出来,搞得心情很不舒服。个人觉得用h2的org.h2Driver,不清楚其原理的话,还是先不要急着学习,很容易被带到里面一时半会儿出不来的。

 

我们采用com.mysql.cj.jdbc.Driver其实也是可以使用mybatis-plus的。

 

 

 

 

以下是我经过测试运行正常且能返回数据的代码,供大家学习。

一、pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.3.4.RELEASE</version>
    <scope>test</scope>
</dependency> 


<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>

</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
     <optional>true</optional>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

<!--添加Servlet依赖,只在编译时有效 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

<!--JavaServer Pages Standard Tag Library,JSP标准标签库-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

二、application.xml

server.port=8080

spring.datasource.url=jdbc:mysql://127.0.0.1/pxks?serverTimezone=UTC&useUnicode=true&charaterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=helloliuting001
spring.datasource.druid.url=rewriteBatchedStatements=true

spring.h2.console.enabled=false
spring.h2.console.settings.web-admin-password=123456

mybatis-plus.config-location=classpath:/mybatis-config.xml

#延时加载   忽略无法转换的对象
spring.jackson.serialization.fail-on-empty-beans=false

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/pxks

 

三、DemoApplication.java

@MapperScan("com.example.demo.mapper")
@SpringBootApplication
@Controller
public class DemoApplication {
    @Autowired
    private UserMapper userMapper;
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @RequestMapping("/")
    public @ResponseBody
    List<User> index(HttpSession session )throws Exception{
        List<User> list=userMapper.selectList(null);
        return list;
    }
}

四、UserMapper.java

public interface UserMapper  extends BaseMapper<User> {
}

五、User.java

import lombok.Data;

@Data
@TableName(value = "tb_User")
public class User {
    private long id;
@TableField(exist = true,value = "name")//与数据对应列名
    private String name;
    private String age;
    private String email;
    @TableField(exist = false) //exist默认为true(对应数据库字段) false为数据库中未有与之对应的字段,仅是后端操作便利
    private String cz;
}
### Spring BootMyBatis-Plus结合使用的架构图 Spring BootMyBatis-Plus 的结合是一种常见的开发模式,用于快速构建基于 Java 的 Web 应用程序。其核心设计理念是通过分层结构实现高内聚低耦合的目标。 #### 1. **整体架构设计** Spring Boot + MyBatis-Plus 的典型项目通常采用三层架构设计:控制器层(Controller)、服务层(Service)以及数据访问层(Mapper)。以下是各层的功能描述: - **Controller 层**: 负责接收 HTTP 请求并调用 Service 层完成业务逻辑处理。它主要作为前端请求的入口点。 - **Service 层**: 实现具体的业务逻辑操作,并封装复杂的业务流程。该层会调用 Mapper 层来执行数据库交互任务。 - **Mapper 层**: 使用 MyBatis-Plus 提供的基础 CRUD 功能接口 `BaseMapper` 来简化对数据库的操作。开发者只需关注自定义 SQL 或复杂查询部分即可[^1]。 #### 2. **具体代码示例** 下面是一个简单的例子展示如何搭建这样的框架结构: ```java // Controller Layer Example @RestController @RequestMapping("/account") public class AccountController { @Autowired private IAccountService accountService; @GetMapping("/{id}") public ResponseEntity<Account> getAccountById(@PathVariable Long id){ Optional<Account> optionalAccount = Optional.ofNullable(accountService.getById(id)); return optionalAccount.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } } // Service Interface and Implementation Example @Service public class AccountServiceImpl implements IAccountService { @Autowired private AccountMapper accountMapper; @Override public Account getById(Long id) { return this.accountMapper.selectById(id); } } ``` 对于数据库连接设置,在 Maven 中引入 PostgreSQL 驱动依赖项,并在 application.properties 文件里指定端口号和其他必要参数[^2]: ```xml <!-- pom.xml --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> ``` ```properties # application.properties server.port=8086 spring.datasource.url=jdbc:postgresql://localhost:5432/testdb spring.datasource.username=postgres spring.datasource.password=rootpassword spring.datasource.driver-class-name=org.postgresql.Driver ``` #### 3. **架构图示意** 虽然无法直接提供图片形式的架构图,但可以文字化描述如下: ``` +-------------------+ | | | Frontend/UI | -----> (HTTP Request) | | +-------+-----------+ | v +-------+-----------+ | | | Controller | -----> Business Logic Entry Point | | +-------+-----------+ | v +-------+-----------+ | | | Service Layer | -----> Handles Core Business Operations | | +-------+-----------+ | v +-------+-----------+ | | | Mapper/DAO | -----> Database Interaction via MyBatis-Plus | | +-------------------+ ``` 此架构清晰划分职责边界,便于维护扩展。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值