求助学习mybatis的时候,sqlsessionfactory报NullpointerException 20
userMapping.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.o...
userMapping.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="mapper.UserMapper">
<insert id ="save" parameterType="domain.User" useGeneratedKeys="true">
INSERT INTO USERS(name,male,age) VALUES(#{name_stu},#{male},#{age})
</insert>
</mapper>
--------------------------------------
MyBatisTest.java
package spittr.test;
import org.apache.ibatis.session.*;
import spittr.domain.User;
import org.apache.ibatis.io.Resources;
import java.io.InputStream;
public class MyBatisTest {
public static void main(String[] args) throws Exception {
InputStream inputStream= Resources.class.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session =sessionFactory.openSession();
User user = new User("admin",true,26);
session.insert("mapper.UserMapper.save",user);
session.commit();session.close();
}
}
---------------------------------------
mybatis-config.xml
<?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="LOG4J"/>
</settings>
<environments default="PostgreSQL">
<environment id="PostgreSQL">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/postgres"/>
<property name="username" value="pstgres"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration> 展开
<?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="mapper.UserMapper">
<insert id ="save" parameterType="domain.User" useGeneratedKeys="true">
INSERT INTO USERS(name,male,age) VALUES(#{name_stu},#{male},#{age})
</insert>
</mapper>
--------------------------------------
MyBatisTest.java
package spittr.test;
import org.apache.ibatis.session.*;
import spittr.domain.User;
import org.apache.ibatis.io.Resources;
import java.io.InputStream;
public class MyBatisTest {
public static void main(String[] args) throws Exception {
InputStream inputStream= Resources.class.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session =sessionFactory.openSession();
User user = new User("admin",true,26);
session.insert("mapper.UserMapper.save",user);
session.commit();session.close();
}
}
---------------------------------------
mybatis-config.xml
<?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="LOG4J"/>
</settings>
<environments default="PostgreSQL">
<environment id="PostgreSQL">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/postgres"/>
<property name="username" value="pstgres"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration> 展开
1个回答
2019-03-10
展开全部
空指针 在java 很常见
你有判断吗?
你有判断吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询