从小白开始学习,希望自己学习的过程可以帮助更多需要的人,参考网址: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、项目架构:
2、主配置文件: mybatis-configuration.xml
1 2 34 5 6 7 10 <package name="com.ys.bean"/> 11 12 13 14 17default="development"> 18 19 40 4120 29 3930 31 32 33 3834 35 36 37 42 44 4645
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"> 56 11 16 17 18 21 22 23 24 update person set pname=#{pname},page=#{page} where pid = #{pid} 25 26 27 2829 insert into person(pname,page) values(#{pname},#{page}) 30 31 32 3334 delete from person where pid=#{pid} 35 36 37 38 3940 47 48 49 52 53 54 57 58 5941 42 43 44 45 46 60 64 65 66 69 70 71 74 7561 62 63
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 Liststudents; 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 // 35 String statement = "com.ys.bean.personMapper"+".selectPersonById"; 36 Person p = session.selectOne(statement, 1); 37 System.out.println(p); 38 session.close(); 39 } 40 41 //查询person 表所有数据 42 @Test 43 public void testGetAllPerson(){ 44 String statement = "com.ys.bean.personMapper"+".getAllPerson"; 45 ListlistPerson = session.selectList(statement); 46 System.out.println(listPerson); 47 session.close(); 48 } 49 50 //根据id更新数据 51 @Test 52 public void updateById(){ 53 String statement = "com.ys.bean.personMapper.updatePersonById"; 54 Person p = new Person(); 55 p.setPid(1); 56 p.setPname("aaa"); 57 p.setPage(11); 58 session.update(statement, p); 59 session.commit(); 60 session.close(); 61 } 62 63 //向 person 表插入一条数据 64 @Test 65 public void addPerson(){ 66 String statement = "com.ys.bean.personMapper.addPerson"; 67 Person p = new Person(); 68 //由于我们设置了主键的自增长机制,故这里不需要手动设置 pid 的值 69 //p.setPid(1); 70 p.setPname("addd"); 71 p.setPage(22); 72 session.insert(statement, p); 73 session.commit(); 74 session.close(); 75 } 76 77 //根据 pid 删除person 表中的数据 78 @Test 79 public void deletePersonById(){ 80 String statement = "com.ys.bean.personMapper.deletePersonById"; 81 session.delete(statement, 6); 82 session.commit(); 83 session.close(); 84 } 85 86 @Test 87 public void selectStudent(){ 88 String statement = "com.ys.bean.personMapper.selectStudent"; 89 List student_list = session.selectList(statement); 90 for(Student stu:student_list){ 91 System.out.println(stu.toString()); 92 } 93 session.commit(); 94 session.close(); 95 } 96 97 @Test 98 public void selectClazz(){ 99 String statement = "com.ys.bean.personMapper.selectClazz"; 100 List student_list = session.selectList(statement); 101 for(Clazz clazz:student_list){ 102 System.out.println(clazz.toString()); 103 List students = clazz.getStudents(); 104 for(Student stu:students){ 105 System.out.println(stu.getId()+" "+stu.getName()+" "+stu.getSex()); 106 } 107 } 108 session.commit(); 109 session.close(); 110 } 111 112 }
7、数据库表的设计以及插入语句
1 /** 2 CREATE table tb_clazz( 3 id int PRIMARY KEY AUTO_INCREMENT, 4 code VARCHAR(18) 5 ); 6 INSERT INTO tb_clazz(code) VALUES('J1601'); 7 INSERT INTO tb_clazz(code) VALUES('J1602'); 8 **/ 9 SELECT * FROM tb_clazz 10 11 /** 12 CREATE table tb_STUDENT( 13 id int PRIMARY KEY AUTO_INCREMENT, 14 NAME VARCHAR(18), 15 sex CHAR(3), 16 age INT, 17 class_id INT, 18 FOREIGN KEY(class_id) REFERENCES tb_clazz(id) 19 ); 20 INSERT INTO tb_STUDENT(NAME,sex,age,class_id) VALUES('jack','男',22,1); 21 INSERT INTO tb_STUDENT(NAME,sex,age,class_id) VALUES('rose','女',18,1); 22 INSERT INTO tb_STUDENT(NAME,sex,age,class_id) VALUES('tom','男',25,2); 23 INSERT INTO tb_STUDENT(NAME,sex,age,class_id) VALUES('mary','女',20,2); 24 */ 25 SELECT * FROM tb_STUDENT