Spring案例无法运行不报错

项目结构(配置文件只用到第三个student那个)

img


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>
    <groupId>com.itheima</groupId>
    <artifactId>chapter09</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- jdbc包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <!-- spring-tx包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <!-- MySQL数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
            <scope>runtime</scope>
        </dependency>
        <!-- aspectjweaver依赖 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
            <scope>runtime</scope>
        </dependency>
        <!-- aopalliance依赖包 -->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

测试类StudentController

package demo666;
import demo666.StudentDao;
import demo666.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.Scanner;
public class StudentController {
    public static void main(String[] args) {
        System.out.println("欢迎来到学生管理系统");
        System.out.println("请输入用户名:");
        Scanner sca = new Scanner(System.in);
        String name = sca.nextLine();
        // 加载配置文件
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext-student.xml");
        // 获取AccountDao实例
        StudentDao studentDao = (StudentDao) applicationContext.getBean("studentDao");
        // 执行findAllAccount()方法,获取Account对象的集合
        List<Student> student = studentDao.findAllStudent();
        // 循环输出集合中的对象
        for (Student stu : student) {
            if (name.equals(stu.getUsername())) {
                System.out.println("请输入" + stu.getUsername() + "的密码:");
                String mima = sca.nextLine();
                if (mima.equals(stu.getPassword())) {
                    System.out.println("用户登录成功!");
                    System.out.println(stu.getUsername() + "是" + stu.getCourse() + "班的");
                    return;
                }
            } else {
                System.out.println("账号密码错误!");
                return;
            }
        }
    }
}

配置文件applicationContext-student(数据库8.0)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- 1.配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!--数据库驱动 -->
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <!--连接数据库的url -->
        <property name="url" value="jdbc:mysql://localhost/spring?useSSL=false"/>
        <!--连接数据库的用户名 -->
        <property name="username" value="root"/>
        <!--连接数据库的密码 -->
        <property name="password" value="root"/>
    </bean>
    <!-- 2.配置JDBC模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 默认必须使用数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 3.定义id为studentDao的Bean -->
    <bean id="studentDao" class="demo666.StudentDaoImpl">
        <!-- 将jdbcTemplate注入到AccountDao实例中 -->
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <!-- 4.事务管理器,依赖于数据源 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 5.注册事务管理器驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

Student类

package demo666;
public class Student {
    //学生ID
    private Integer id;
    //学生姓名
    private String username;
    //学生密码
    private String password;
    //学生班级
    private String course;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
}

StudentDao接口

package demo666;
import demo666.Student;
import java.util.List;

public interface StudentDao {
    //查询所有账户
    public List<Student> findAllStudent();
}

StudentDaoImpl实现类

package demo666;
import demo666.StudentDao;
import demo666.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
    // 声明JdbcTemplate属性及其setter方法
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    // 查询所有用户信息
    public List<Student> findAllStudent() {
        // 定义SQL语句
        String sql = "select * from student";
        // 创建一个新的BeanPropertyRowMapper对象
        RowMapper<Student> rowMapper =
                new BeanPropertyRowMapper<Student>(Student.class);
        // 执行静态的SQL查询,并通过RowMapper返回结果
        return this.jdbcTemplate.query(sql, rowMapper);
    }
}

运行结果

img


调试结果

img

不报错 也不往下进行下面的过程 直接就结束 这是怎么回事呢 求解答!


 Scanner sca = new Scanner(System.in);
        String name = sca.nextLine();
        // 加载配置文件
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext-student.xml");
        // 获取AccountDao实例
这一块多加几个print或者debug模式运行看看。加几个断点

可能是你获取学生列表的时候获取为空,可以对这个列表进行判断,如果为空打印一行日志看看

获取到的student列表为空就直接退出了

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^