连接MySQL数据库获取数据总是报空指针异常是什么原因呢,求解

连接MySQL数据库获取数据总是报空指针异常是什么原因呢?

在连接数据库的过程中Sqlsession获取不到

代码如下 category

package com.iweb.entity;

import java.util.List;

public class Category {
private Integer cid;
private String cname;
private Integer level;
private Integer parentld;
private List products;

public Integer getCid() {
    return cid;
}

public void setCid(Integer cid) {
    this.cid = cid;
}

public String getCname() {
    return cname;
}

public void setCname(String cname) {
    this.cname = cname;
}

public Integer getLevel() {
    return level;
}

public void setLevel(Integer level) {
    this.level = level;
}

public Integer getParentld() {
    return parentld;
}

public void setParentld(Integer parentld) {
    this.parentld = parentld;
}

public List<Product> getProducts() {
    return products;
}

public void setProducts(List<Product> products) {
    this.products = products;
}

@Override
public String toString() {
    return "Category{" +
            "cid=" + cid +
            ", cname='" + cname + '\'' +
            ", level=" + level +
            ", parentld=" + parentld +
            '}';
	}

}

categoryMapper.xml

<?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.iweb.mapper.CategoryMapper”>
< resultMap id=“categoryProductMap” type=“Category”>
< id column=“cid” property=“cid”/>
< result column=“cname” property=“cname”/>
< result column=“level” property=“level”/>
< result column=“parentId” property=“parentId”/>
< collection property=“products” ofType=“Product”>
< id column=“pid” property=“pid”/>
< result column=“category_level1” property=“category_level1”/>
< result column=“category_level2” property=“category_level2”/>
< /collection>
< /resultMap>
< select id=“getCategoryById” resultMap=“categoryProductMap”>
select c.,p. from category c inner join product p on (c.cid = p.category_level1 or c.cid = p.category_level2) where
c.cid = #{cid};
< /select>
< /mapper>

测试类

package com.iweb.test;

import com.iweb.entity.Category;
import com.iweb.entity.Product;
import com.iweb.mapper.CategoryMapper;
import com.iweb.mapper.ProductMapper;
import com.iweb.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class CategoryMapperTest {
private Logger logger = Logger.getLogger(CategoryMapper.class);
SqlSession sqlSession=null;
@Before
public void before(){ SqlSession sqlSession = MyBatisUtil.openSession();}
@After
public void after(){ sqlSession.close();}
@Test
public void test1(){
Category category = sqlSession.getMapper(CategoryMapper.class).getCategoryById(2);
logger.debug(category.toString());
for (Product product:category.getProducts()){
logger.debug(product.toString());
}
}
}

报错

![Alt]连接MySQL数据库获取数据总是报空指针异常是什么原因呢,求解
##初学小萌新,跪求各位大佬指点,谢谢!

上一篇:搭建一个简易的学生成绩管理数据库


下一篇:java day50【综合案例day02】