这个工程不知道哪里出错了,自己排查不出来,麻烦老师排查解惑

这个工程不知道哪里出错了,自己排查不出来,麻烦老师排查解惑

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.imooc</groupId>
<artifactId>HomeworkSong</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>

</project>


mybatis-config.xml

<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/homeworksong?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="154672"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.imooc.dao"/>
</mappers>

</configuration>


logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%thread] %d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="console"/>
</root>
</configuration>


MyBatisUtils

package com.imooc.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.Reader;

public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory=null;
static {
try{
Reader reader= Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
}catch (Exception e){
e.printStackTrace();
throw new ExceptionInInitializerError();
}
}

public static SqlSession openSession(){
return sqlSessionFactory.openSession();
}

public static void closeSession(SqlSession session){
if(session!=null){
session.close();
}
}
}


实体类Song

package com.imooc.entity;

import java.util.Date;

public class Song {
private Integer id;
private String name;
private String singer;
private String category;
private String writer;
private String language;
private Date issudate;

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

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

public String getSinger() {
return singer;
}

public void setSinger(String singer) {
this.singer = singer;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getWriter() {
return writer;
}

public void setWriter(String writer) {
this.writer = writer;
}

public String getLanguage() {
return language;
}

public void setLanguage(String language) {
this.language = language;
}

public Date getIssudate() {
return issudate;
}

public void setIssudate(Date issudate) {
this.issudate = issudate;
}

@Override
public String toString() {
return "Song{" +
"id=" + id +
", name='" + name + '\'' +
", singer='" + singer + '\'' +
", category='" + category + '\'' +
", writer='" + writer + '\'' +
", language='" + language + '\'' +
", issudate=" + issudate +
'}';
}
}


dao接口SongDAO

package com.imooc.dao;

import com.imooc.entity.Song;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface SongDAO {
@Select("select * from t_song")
public List<Song> selectAllSong();

}


测试类testSong

import com.imooc.dao.SongDAO;
import com.imooc.entity.Song;
import com.imooc.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class testSong {
@Test
public void testSelectAllSong(){
SqlSession session=null;
try{
session= MyBatisUtils.openSession();
SongDAO songDAO=session.getMapper(SongDAO.class);
List<Song> list=songDAO.selectAllSong();
for(Song s:list){
System.out.println(s);
}
session.commit();
}catch(Exception e){
e.printStackTrace();
if(session!=null){
session.rollback();
}
}finally {
MyBatisUtils.closeSession(session);
}
}
}


报错提示:

https://img1.sycdn.imooc.com//climg/614e8c550961732b13910690.jpg

正在回答 回答被采纳积分+1

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

1回答
蒋昌魁 提问者 2021-09-25 11:05:49

这个问题不用回答了,排查出来了,原来是dataSource里的name写错了

  • 同学你好,很棒哦,自己就排除出问题了,继续加油!

    2021-09-25 14:30:16
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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