发表于: 2017-09-12 21:50:08

2 962


今天完成的事

【1】开始搭建mybatis经过无数次调试也算成功了。配置文件如下。

SqlMapConfig.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>
   <!-- 和spring整合后 environments配置将废除-->
   <environments default="development">
       <environment id="development">
           <!-- 使用jdbc事务管理-->
           <transactionManager type="JDBC" />
           <!-- 数据库连接池-->
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.jdbc.Driver" />
               <property name="url" value="jdbc:mysql://127.0.0.1:3306/mysql"/>
               <property name="username" value="root" />
               <property name="password" value="123" />
           </dataSource>
       </environment>
   </environments>
   <mappers>
       <mapper resource="mapper/User.xml"/>
   </mappers>
</configuration>

User.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="test">
   <!--
   id=sql语句唯一标识
   parameterType指定传入参数类型
   resultType返回结果 集类型
   -->
   <select id="findUserById" parameterType="java.lang.Integer" resultType="User">
        select * from student3 where id=#{id}
   </select>
       </mapper>

一个测试类

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 org.junit.Test;

import java.io.InputStream;

public class UserTest {
   @Test
   public void testFindUserById() throws Exception{
       String resource="SqlMapConfig.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);
       SqlSessionFactory factory =new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession openSession =factory.openSession();
       User user=openSession.selectOne("test.findUserById",1);
       System.out.println(user);
       openSession.close();
   }
}

新建一个Use类

public class User{
   private Integer id;
   private String username;
   private Integer userqq;
   private String userschool;
   private String will;
   private Integer  creatat;
   private Integer updateat;

   public int getId() {
       return id;
   }
   public void setId(Integer id) {
       this.id = id;
   }
    public String getUsername(){
       return username;
    }
   public void getUsername(String username) {
       this.username = username;
   }
   public Integer getUserqq(){
        return userqq;
   }
   public void setUserqq(Integer userqq) {
       this.userqq=userqq;
   }
   public String getUserschool(){
       return userschool;
   }
   public void setUserschool(String userschool){
       this.userschool=userschool;
   }
   public String getWill(){
       return will;
   }
   public void setWill(String will){
       this.will=will;
   }
   public Integer getCreatat(){
       return creatat;
   }
   public void setCreatat(Integer creatat){
       this.creatat=creatat;
   }
   public Integer getUpdateat(){
       return updateat;
   }
   public void setUpdateat(Integer updateat){
       this.updateat=updateat;
   }
}

出现的问题

比较多,搭建过程中出现的问题,我都列举下。

【1】异常1

解决方法

因为之前路径设置的是User.xml,然而文件被我放到mapper下面,故报错。

【2】异常2

搭建Mybatis框架的时候,发现idea一直加载不了xml文件,解决方法。

解决方法

如果发现需要idea编译java文件夹里面xml文件需要加入以下的配置文件:

1
2
3
4
5
6
7
8
9
10
11
<build>
    <finalName>mybatis-base</finalName>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
    </resources>
  </build>

【3】异常3

测试的时候偶尔会出错,

提示找不到。"SqlMapConfig.xml"

但是MAVEN -clean以下再运行就好了,想不懂什么原因。。师兄看这里,高亮!


明天要做的事

【1】重新搭建一遍mybatis.再理解一下各个配置文件。



返回列表 返回列表
评论

    分享到