请帮我指正一下!这个问题我已经改了3天了
网上的解决方法我大概都试过了
1.映射requesMapping url错误,我试过把控制类userdao方法注解掉是可以返回页面的(已排除)
2.pom文件中thymeleaf依赖注入问题(这个问题因为我对专业知识的不够了解待指正)
3.pom指出配置文件的地址(已排除)因为我在pom文件中加入了配置文件的地址了
4.还有说是因为yml文件中内有加入映射路径的 我也加了(已排除)但是这明明是404的报错应该跟500报错没关系吧~不过改了就是了
5.注解用法错误,这个我也看过了但是不知道会不会出错(待指正)
6.没有在html文件中加入thymeleaf依赖这个我也加了(已排除)
7.还有说启动类没在根目录的 (已排除)
8.yml文件格式(排除)
9.rebuild项目(排除)
10.resource文件没有加入classpath(这个resource文件是项目自动生成的应该可以排除)
那么进入正题吧
目录
控制类
import com.fish.marketgoods.dao.UserDao;
import com.fish.marketgoods.pojo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
@Controller
public class indexController {
@Resource
UserDao userDao;
@RequestMapping("/")
/*@ResponseBody*/
//表示返回json字符串给前端,否者return会返回名叫"hellow,bro"的前端页面
//因为如果方法类型为String会返回return中的字符的值的页面,而不是console输出
public String index(){
System.out.println("22222222222222222222");
User user=userDao.selectByPrimaryKey(1);
return "index";
}
}
服务类还没有写想试试能不能获取到数据库信息再写service服务层
UserDao接口
package com.fish.marketgoods.dao;
import com.fish.marketgoods.pojo.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao {
/*根据主键查询*/
User selectByPrimaryKey(Integer userId);
}
UserDao.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.fish.marketgoods.dao.UserDao">
<resultMap id="BaseResultMap" type="com.fish.marketgoods.pojo.entity.User">
<id column="user_id" jdbcType="INTEGER" property="userId"/>
<result column="nick_name" jdbcType="VARCHAR" property="nickName" />
<result column="real_name" jdbcType="VARCHAR" property="realName" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="last_login_time" jdbcType="TIMESTAMP" property="lastLoginTime" />
<result column="last_login_ip" jdbcType="VARCHAR" property="lastLoginIp" />
</resultMap>
<sql id="Base_Column_List">
user_id, nick_name, real_name, phone, `password`, last_login_time, last_login_ip
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where user_id = #{userId,jdbcType=INTEGER}
</select>
</mapper>
application.yml文件
server:
port: 8080 #当前运行的端口
spring:
datasource: #数据源配置
type: com.alibaba.druid.pool.DruidDataSource #连接池的类型
username: root
password: 123456
url: jdbc:mysql://localhost:3306/goods_market_db?serverTimezone=GMT #mysql8.0以上需要加入时区不然会连接不上
driver-class-name: com.mysql.cj.jdbc.Driver #数据库的驱动程序类 #如果是mysql5.*的版本 不需要加入cj!!!!!!
thymeleaf:
suffix: .html
mybatis:
configuration:
map-underscore-to-camel-case: true #数据库中的下划线自动转化成驼峰
mapper-location: classpath:mybatis/mapper/**/*.xml #mapper文件的路径,这个路径下面的mapper.xml文件会被自动加载成mybatis的映射文件
#classpath resource和view文件都是classpath #**表示多级目录下的
#所有xml文件
POM.xml(个人怀疑是thymeleaf的配置问题)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<!--<relativePath/> 如果有相对路径的话需要安装-->
</parent>
<groupId>com.fish</groupId>
<artifactId>market-goods</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>market-goods</name>
<description>我的一个商城项目</description>
<properties>
<java.version>1.8</java.version>
<thymeleaf.version>3.1.0.M1</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
<!-- jar包管理的-->
<dependencies>
<!-- 启动器就是SpringBoot的启动场景,比如我们要使用web相关的,那么就直接引入spring-boor-starter-web,
那么他就会帮我们自动导入web环境下所有必需的依赖。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 导入Thymeleaf的依赖视图解析器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<!-- 配置mysql的数据库驱动程序类 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!-- 阿里巴巴的德鲁伊连接 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!-- 开发辅助工具类 省略getter setter toString方法 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<!-- 打包需要的模块-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
启动类
package com.fish.marketgoods;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/这个是启动程序的主类/
@MapperScan("com.fish.marketgoods.dao")
@SpringBootApplication
public class MarketGoodsApplication {
public static void main(String[] args) {
SpringApplication.run(MarketGoodsApplication.class, args);
}
}
maven下dependence排布(个人觉得thymeleaf排放可能出错了,因为之前我删除过maven的依赖包)
谢谢!