老师看下作业

老师看下作业

<?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.imooc</groupId>
    <artifactId>student</artifactId>
    <version>1.0-SNAPSHOT</version>
    <repositories>
        <repository>
            <id>aliyun</id>
            <name>alliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>


</project>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="dev">
        <environment id="dev">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="uri"
                          value="jdbc:mysql://localhost:3306/newschooluseUnicode=true&amp;characterEncoding=UTF-8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="root1234"/>
            </dataSource>

        </environment>
    </environments>

    <mappers>
        <mapper resource="mappers/students.xml"/>
    </mappers>

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

</configuration>
package com.imooc.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MyBatisUtils {
    private static SqlSessionFactory sqlSessionFactory = null;

    static{
        Reader reader=null;
        try{
            reader= Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }
    public static SqlSession openSession(){
        return sqlSessionFactory.openSession();
    }
    public static void closeSession(SqlSession session){
        if (session!=null){
            session.close();
        }
    }
}
package com.imooc.mybatis.entity;

public class Students {
    private int id;
    private String regNo;
    private String name;
    private String sex;
    private int age;
    private int grade;
    private String major;

    @Override
    public String toString() {
        return "Students{" +
                "id=" + id +
                ", regNo='" + regNo + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", grade=" + grade +
                ", major='" + major + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRegNo() {
        return regNo;
    }

    public void setRegNo(String regNo) {
        this.regNo = regNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }
}
<?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="students">
    <select id="selectAll" resultType="com.imooc.mybatis.entity.Students">
        select * from student order by id
    </select>
</mapper>
package com.imooc.mybatis;

import com.imooc.mybatis.entity.Students;
import com.imooc.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class MyBatisTestor {

    @Test
    public void testSelectAll() throws Exception {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtils.openSession();
            List<Students> list=sqlSession.selectList("students.selectAll");
            for (Students s:list){
                System.out.println(s);
            }
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,报错提示找不到类,如:

http://img1.sycdn.imooc.com//climg/5e5f107409af407004250053.jpg

问一下同学MyBatisTestor类是否存在并保存了呢?如果存在并保存了,建议同学删除target文件,重新运行再试试 。

祝:学习愉快~

好帮手慕阿满 2020-03-03 11:51:55

同学的代码整体完成的不错,但是还有如下问题:

http://img1.sycdn.imooc.com//climg/5e5dd3db09d852cc08940245.jpg

在mybatis-config.xml中,连接数据库的name应该时url,而不是uri。另外数据库名和后边的参数之间应该用问号?隔开。最后建议下次同学将数据库的sql语句也贴一下。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 提问者 慕圣7256090 #1
    修改之后测试运行出现这个问题 Process finished with exit code 1 Class not found: "com.imooc.mybatis.MyBatisTestor"
    2020-03-03 21:43:25
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师