mybatis学习成长之路(一)

  从小白开始学习,希望自己学习的过程可以帮助更多需要的人,参考网址:https://www.cnblogs.com/ysocean/p/7237499.html

    1、mybatis的jar包下载地址:https://github.com/mybatis/mybatis-3/releases

    2、浅谈mybatis # $区别:https://www.cnblogs.com/dato/p/7027949.html

  一、mybatis项目讲解

    1、项目架构:

      mybatis学习成长之路(一)_第1张图片

    2、主配置文件: mybatis-configuration.xml  

 1 
 2 
 3 
 4 
 5 
 6 
 7     
10     <package name="com.ys.bean"/>
11 
12 
13 
14 
17  default="development">
18  
19     
20     
29       
30     
31       
32       
33         
34         
35         
36         
37       
38     
39   
40   
41     
42        
44        
45     
46 

    3、数据库配置信息:db.properties

1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql://localhost:3306/mybatisdemo
3 username=root
4 password=ROOT

    4、数据库实体类映射文件:personMapper.xml 

 1 
 2 DOCTYPE mapper
 3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6   
11   
16    
17   
18   
21    
22   
23   
24     update person set pname=#{pname},page=#{page} where pid = #{pid}
25   
26    
27   
28   
29     insert into person(pname,page) values(#{pname},#{page})
30   
31    
32   
33   
34     delete from person where pid=#{pid}
35   
36    
37   
38    
39    
40            
41            
42            
43            
44            
45            
46    
47    
48    
49    
52    
53    
54    
57    
58    
59    
60            
61            
62            
63    
64    
65    
66    
69    
70    
71    
74    
75 

    5、实体类

      (1)、Clazz

 1 package com.ys.bean;
 2 
 3 import java.util.List;
 4 
 5 
 6 public class Clazz {
 7     private Integer id;
 8     private String code;
 9     private List students;
10     
11     public Integer getId() {
12         return id;
13     }
14     public void setId(Integer id) {
15         this.id = id;
16     }
17     public String getCode() {
18         return code;
19     }
20     public void setCode(String code) {
21         this.code = code;
22     }
23     public List getStudents() {
24         return students;
25     }
26     public void setStudents(List students) {
27         this.students = students;
28     }
29     
30     @Override
31     public String toString() {
32         return "Clazz [code=" + code + ", id=" + id + "]";
33     }
34 }

      (2)、Person

package com.ys.bean;
 
public class Person {
    private int pid;
    private String pname;
    private int page;
     
    public int getPid() {
        return pid;
    }
    public void setPid(int pid) {
        this.pid = pid;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    @Override
    public String toString() {
        return "Person [pid=" + pid + ", pname=" + pname + ", page=" + page
                + "]";
    }
}

      (3)、Student

package com.ys.bean;

public class Student {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private Clazz clazz;
    
    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 getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Clazz getClazz() {
        return clazz;
    }
    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }
    
    @Override
    public String toString() {
        return "Student [age=" + age + ", clazz=" + clazz.toString() + ", id=" + id
                + ", name=" + name + ", sex=" + sex + "]";
    }
    
    
}

    6、测试代码

  1 package com.ys.test;
  2  
  3 import java.io.InputStream;
  4 import java.util.List;
  5  
  6 import org.apache.ibatis.session.SqlSession;
  7 import org.apache.ibatis.session.SqlSessionFactory;
  8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  9 import org.junit.Before;
 10 import org.junit.Test;
 11  
 12 import com.ys.bean.Clazz;
 13 import com.ys.bean.Person;
 14 import com.ys.bean.Student;
 15  
 16 public class MyBatisTest {
 17     SqlSession session;
 18      
 19     @Before
 20     public void beforeLoadXML(){
 21         //加载 mybatis 配置文件
 22         InputStream inputStream = MyBatisTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml");
 23         //构建sqlSession的工厂
 24         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 25         //根据 sqlSessionFactory 产生 session
 26         session = sqlSessionFactory.openSession();
 27     }
 28      
 29     //根据 pid 查询 person 表中的数据
 30     @Test
 31     public void testSelectById(){
 32         //这个字符串有 personMapper.xml 文件中 两个部分构成
 33         // 的 namespace 的值
 34         //