这个工程不知道哪里出错了,自己排查不出来,麻烦老师排查解惑
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&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);
}
}
}
报错提示:
17
收起
正在回答 回答被采纳积分+1
相似问题
登录后可查看更多问答,登录/注册
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星