发表于: 2021-11-01 00:28:36

1 983


今天完成的事:学习mybaits 链接数据方法

练习mybatisdemo

创建User.java

package Dao;

public class User {
private int id;
   private String userName;
   private String userAge;
   private String userAddress;

   public int getId() {
return id;
   }
public void setId(int id) {
this.id = id;
   }
public String getUserName() {
return userName;
   }
public void setUserName(String userName) {
this.userName = userName;
   }
public String getUserAge() {
return userAge;
   }
public void setUserAge(String userAge) {
this.userAge = userAge;
   }
public String getUserAddress() {
return userAddress;
   }
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
   }
}

创建UserMapper.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="Dao.UserMapper">
   <select id="selectUserByID" parameterType="int" resultType="User">
       select * from `user` where id = #{id}
</select>
</mapper>

创建db_conf.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>

   <environments default="development">
       <environment id="development">
           <transactionManager type="JDBC" />
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.jdbc.Driver" />
               <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true" />
               <property name="username" value="root" />
               <property name="password" value="deng796443" />
           </dataSource>
       </environment>
   </environments>

   <mappers>
       <mapper resource="Dao/UserMapper.xml" />
   </mappers>

</configuration>
   

创建Test.java

package  Dao;

import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {
private static SqlSessionFactory sqlSessionFactory;
       private static Reader reader;

       static{
try{
reader    = Resources.getResourceAsReader("Dao/UserMapper.xml");
               sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
           }catch(Exception e){
               e.printStackTrace();
           }
       }

public static SqlSessionFactory getSession(){
return sqlSessionFactory;
       }

public static void main(String[] args) {
           SqlSession session = sqlSessionFactory.openSession();
           try {
               User user = (User) session.selectOne("Dao.UserMapper.selectUserByID", 1);
               System.out.println(user.getUserAddress());
               System.out.println(user.getUserName());
           } finally {
               session.close();
           }

}

运行结果报错:

/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=54068:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /var/root/IdeaProjects/Mydatis/target/classes:/Users/kiven/App/repository/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar:/Users/kiven/App/repository/com/google/protobuf/protobuf-java/2.6.0/protobuf-java-2.6.0.jar:/Users/kiven/App/repository/org/mybatis/mybatis/3.5.6/mybatis-3.5.6.jar:/Users/kiven/App/repository/log4j/log4j/1.2.12/log4j-1.2.12.jar Dao.Test

java.io.IOException: Could not find resource Dao/UserMapper.xml

at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)

at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)

at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:160)

at Dao.Test.<clinit>(Test.java:15)

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.apache.ibatis.session.SqlSessionFactory.openSession()" because "Dao.Test.sqlSessionFactory" is null

at Dao.Test.main(Test.java:27)







返回列表 返回列表
评论

    分享到