weixin_44134888
鑫鑫爸
采纳率0%
2021-01-15 18:20 阅读 14

通用mapper报错,浏览器访问后报错,有没有高手能解惑

 

数据库

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- springboot 父工程依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>itheima_springboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 设置 JDK 版本号 -->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!-- 各种依赖坐标 -->
    <dependencies>
        <!-- 添加 spring-boot 的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 添加 druid 的依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <!-- 不传递依赖 -->
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- 事务相关启动器依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mysql依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

UserMapper.java接口

package com.itheima.mapper;

import com.itheima.pojo.User;
//import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.Mapper;

//@Component(value = "userMapper")
public interface UserMapper extends Mapper<User> {
}

Application 启动类

package com.itheima;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;


/**
 * spring boot 工程都有一个启动引导类,这是工程的入口类
 * 并在引导类上添加 @SpringBootApplication
 */
@SpringBootApplication
// 扫描 mybatis 所有的业务 mapper 接口
//@MapperScan("com.itheima.mapper")
@MapperScan("com.itheima.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

User 实体类

package com.itheima.pojo;

import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
//import lombok.extern.slf4j.Slf4j;
//import org.slf4j.Logger;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

// 在编译阶段会根据注解自动生成对应的方法;data包含get/set/hashCode/equals/toString 等方法
@Data
@Table(name = "tb_user")
public class User {

    @Id
    // 主键回填
    @KeySql(useGeneratedKeys = true)
    private Long id;

    // 在数据库和实体类属性名相同或者是驼峰命名法的,()中就可以不写
    // 当数据库字段名和实体类属性名不一致的时候,填写数据库字段名
    @Column(name = "user_name")
    private String userName;
    @Column(name = "password")
    private String password;
    @Column(name = "name")
    private String name;
    @Column(name = "age")
    private Integer age;
    @Column(name = "sex")
    private Integer sex;
    @Column(name = "birthday")
    private Date birthday;
    @Column(name = "note")
    private String note;
    @Column(name = "created")
    private Date created;
    @Column(name = "update")
    private Date update;

}

UserService

 

HelloController

package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;

@RestController
public class HelloController {

    @Autowired
    private DataSource dataSource;

    @Value("${itcast.url}")
    private String itcastUrl;

    @Value("${itheima.url}")
    private String itheimaUrl;

    @Autowired
    private UserService userService;

    /**
     * 根据用户id查询用户
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    private User queryById(@PathVariable Long id){
        return userService.queryById(id);
    }

//    @RequestMapping
    @GetMapping("hello")
    public String hello(){
        System.out.println("DataSource="+dataSource);
        System.out.println("itcastUrl="+itcastUrl);
        System.out.println("itheimaUrl="+itheimaUrl);
        return "Hello,Spring Boot";
    }
}

application.yml配置

jdbc:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/springboot_test
  username: root
  password: root

# 激活配置文件,需要指定其他的配置文件名称
spring:
  profiles:
    active: abc,def
  # 配置事务
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot_test
    username: root
    password: root

# tomcat 端口修改
server:
  port: 80

# 日志记录级别
logging:
  level:
    com.itheima: debug
    org.springframework: info

mybatis:
  # 配置实体类别名包路径
  type-aliases-package: com.itheima.pojo
  # 映射文件路径
#  mapper-locations: classpath:mappers/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

启动浏览器

 

报错

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • weixin_44915226 你的代码没bug 2021-01-15 18:25

    根据错误提示的话,应该是sql语句有问题,更新语句

    应该是这样的格式吧:UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing',WHERE LastName = 'Wilson'

    点赞 评论 复制链接分享
  • a1767028198 a1767028198 2021-01-15 18:25

    update from tb_user where id = 8 建议多写写sql

    点赞 评论 复制链接分享
  • weixin_44134888 鑫鑫爸 2021-01-15 18:27

    这个用的是springboot,上面不需要写sql语句,但是报这些错误

    点赞 评论 复制链接分享
  • weixin_44915226 你的代码没bug 2021-01-15 18:37

    没写sql语句应该不会报这些错误,会不会运行错了项目或者导入了其他项目的东西。我也第一次见这种错误,哈哈!

    点赞 评论 复制链接分享
  • qqzhuimengren PythonJavaC++go 2021-01-15 19:08

    写sql了,用的mybatis

    sql在mapper文件夹下

    报错的sql那个应该是在UserMapper.java对应的的那个xml

    点赞 评论 复制链接分享
  • a1767028198 a1767028198 2021-01-15 19:13

    那就自己打断点,看看tk mybatis到底生成sql的逻辑是啥,学好debug,啥bug都不怕

    点赞 评论 复制链接分享

相关推荐