sqlsession空指针异常

package com.liuyupeng.damain;

public class Student {

    private Integer id;
    private String name;
    private String email;
    private String age;

    public Student() {
    }

    public Student(Integer id, String name, String email, String age) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.age = age;
    }

    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 getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}
package com.liuyupeng.dao;

import com.liuyupeng.damain.Student;

import java.util.List;

public interface StudentDao {

    List<Student> selectStudent();


}
<?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="com.liuyupeng.dao.StudentDao">

    <select id="selectStudent" resultType="com.liuyupeng.damain.Student">

       select id,name,email,age from student order by id
    </select>


</mapper>
package com.liuyupeng.dao.impl;

import com.liuyupeng.Util.MyBatisUtils;
import com.liuyupeng.damain.Student;
import com.liuyupeng.dao.StudentDao;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> selectStudent() {
        //1、获取sqlsession的对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
       
        String sqlId = "com.liuyupeng.dao.StudentDao.selectStudent";
        List<Student> students = sqlSession.selectList(sqlId);
        sqlSession.close();

        return students;
    }
}
package com.liuyupeng.Util;


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.InputStream;


public class MyBatisUtils {

    private  static  SqlSessionFactory factory = null;
    static {
        String config="mybatis.xml"; // 需要和你的项目中的文件名一样
        try {
            InputStream in;
            in = Resources.getResourceAsStream(config);
            //创建SqlSessionFactory对象,使用SqlSessionFactoryBuild
            SqlSessionFactory  factory = new SqlSessionFactoryBuilder().build(in);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //获取SqlSession的方法
    public static SqlSession getSqlSession() {
        SqlSession sqlSession  = null;

        if( factory != null){

            sqlSession = factory.openSession();// 非自动提交事务

        }
        return sqlSession;
    }
}

 

sqlSession = factory.openSession();一直有问题,debug也过不去。
<?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>
    <settings>

        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url"
                          value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&amp;allowPublicKeyRetrieval=true"/>
                <property name="username" value="root"/>
                <property name="password" value="0213"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/liuyupeng/dao/StudentDao.xml"/>
    </mappers>
</configuration>

 

 

 

 

 

 

查看全部
weixin_48050721
weixin_48050721
2020/12/02 13:25
  • java
  • intellij-idea
  • 点赞
  • 收藏
  • 回答
    私信

1个回复