老师看下作业
<?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&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&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); } } }
33
收起
正在回答
2回答
同学你好,报错提示找不到类,如:
问一下同学MyBatisTestor类是否存在并保存了呢?如果存在并保存了,建议同学删除target文件,重新运行再试试 。
祝:学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星