Idea creates the mybatis project and implements addition, deletion, modification and query

Time:2021-8-29

Project directory:

 

log4j.xml

1 
 2 
 3 
 4 
 5     
 6         
 7         
 8             
 9         
10     
11     
12         
13     
14     
15         
16     
17     
18         
19         
20     
21

Pom.xml configuration

1 
 2   
 3     
 4       junit
 5       junit
 6       4.11
 7       test
 8     
 9 
10     
11       mysql
12       mysql-connector-java
13       5.1.29
14     
15 
16     
17       org.mybatis
18       mybatis
19       3.4.1
20     
21 
22     
23       log4j
24       log4j
25       1.2.17
26     
27 
28     
29       org.slf4j
30       slf4j-api
31       1.7.12
32     
33 
34     
35     
36       org.mybatis.generator
37       mybatis-generator-core
38       1.3.5
39     
40   
41 
42   
43   
44     
45       
46         src/main/resources
47         
48           **/*.properties
49           **/*.xml
50           **/*.tld
51           **/*.jsp
52         
53         true
54       
55       
56         src/main/java
57         
58           **/*.properties
59           **/*.xml
60           **/*.tld
61         
62         true
63       
64     
65

 

mybatis-config.xml

1 
 2          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 
 6     
 7     
 8 
 9     
10     
11         
12     
13 
14     
15     
16         
17             
18             
19             
20                 
21                 
22                 
23                 
24             
25         
26     
27     
28         
29         
30     
31

mapper.xml

1 
 2 
 3 
 4 
 5     
 6     
 7     
 8         SELECT
 9         stu_id,
10         stu_name,
11         stu_dept,
12         stu_phone
13         FROM
14         students
15     
16 
17     
18     
19         SELECT
20         stu_id,
21         stu_name,
22         stu_dept,
23         stu_phone
24         FROM
25         students
26         WHERE
27 -- variable name has no limit
28         stu_name = #{name}
29     
30 
31     
32     
33         INSERT INTO
34         students(stu_id,stu_name,stu_dept,stu_phone)
35         VALUES
36 -- field must be consistent with entity class
37         (#{stu_id},#{stu_name},#{stu_dept},#{stu_phone})
38     
39 
40     
41     
42         DELETE FROM
43         students WHERE stu_name=#{name}
44     
45 
46     
47     
48         UPDATE
49         students
50         SET
51         stu_name=#{name},
52         stu_dept=#{dept},
53         stu_phone=#{phone}
54         WHERE
55         stu_id=#{id}
56     
57

 

 

Db.properties database configuration file

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

Database design

Simulate several pieces of data

Studentinterface interface class

1 package com.Charon.dao;
 2 
 3 import com.Charon.enty.Student;
 4 
 5 import java.util.HashMap;
 6 import java.util.List;
 7 
 8 /**
 9  * @Description TODO
10  * @Author Charon <[email protected]>
11  * @create 2020-10-20-10:43
12  * @Version 1.0.0
13  */
14 public interface StudentInterface {
15 // query all student information
16     public List  listStudent();
17 
18 // query student information by name
19     public Student chackByName(String name);
20 
21 // add student information
22     public void insertStudent(Student student);
23 
24 // delete the information according to the student's name
25     public void deleteStudent(String name);
26 
27 // modify student information
28     public void updateStudent(HashMap hashMap);
29  }

Mybatisutil tool class

1 package com.Charon.utile;
 2 
 3 import org.apache.ibatis.session.SqlSessionFactory;
 4 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 5 
 6 import java.io.InputStream;
 7 
 8 /**
 9 * @ description todo agent factory class
10  * @Author Charon <[email protected]>
11  * @create 2020-10-21-8:59
12  * @Version 1.0.0
13  */
14 public class MyBatisUtil {
15 
16     private static SqlSessionFactory sqlSessionFactory;
17 
18     public static synchronized SqlSessionFactory getInstance(InputStream inputStream) {
19         if (null == sqlSessionFactory) {
20             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
21         }
22 
23         return sqlSessionFactory;
24     }
25 }

Studenttest test class

1 import com.Charon.dao.StudentInterface;
  2 import com.Charon.enty.Student;
  3 import com.Charon.utile.MyBatisUtil;
  4 import jdk.internal.util.xml.impl.Input;
  5 import org.apache.ibatis.io.Resources;
  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 java.io.IOException;
 13 import java.io.InputStream;
 14 import java.io.Reader;
 15 import java.util.HashMap;
 16 import java.util.List;
 17 import java.util.UUID;
 18 
 19 /**
 20  * @Description TODO
 21  * @Author Charon <[email protected]>
 22  * @create 2020-10-20-10:55
 23  * @Version 1.0.0
 24  */
 25 public class StudentTest {
 26 
 27     SqlSession session = null;
 28 
 29     @Before
 30     public void test() throws IOException {
 31         String resources = "mybatis-config.xml";
 32         InputStream inputStream = Resources.getResourceAsStream(resources);
 33         SqlSessionFactory instance = MyBatisUtil.getInstance(inputStream);
 34         session = instance.openSession();
 35     }
 36 
 37     /**
 38 * query all student information
 39      */
 40     @Test
 41     public void test1(){
 42 // through proxy: generate the implementation class name of the interface
 43         StudentInterface mapper = session.getMapper(StudentInterface.class);
 44 // get the method to query all information in the interface
 45         List students = mapper.listStudent();
 46 // traverse the collection to get data
 47         for (int i = 0; i < students.size(); i++) {
 48             Student student = students.get(i);
 49 system.out.println ("No.: + student.getstu_ ID () + "\ t \ tstudent Name:" + student.getstu_ Name() + "\ t \ t \ tstudent major:" + student.getstu_ Dept() + "\ t \ t \ tstudent phone:" + student.getstu_ phone());
 50         }
 51     }
 52 
 53 
 54     /**
 55 * query data according to student name
 56      */
 57     @Test
 58     public void test2(){
 59         StudentInterface mapper = session.getMapper(StudentInterface.class);
 60 string name = "Xiao Fang";
 61         Student student = mapper.chackByName(name);
 62 system.out.println ("Student Name:" + student.getstu_ Name() + "\ t \ tstudent phone:" + student.getstu_ phone());
 63     }
 64 
 65     /**
 66 * add student information
 67      */
 68     @Test
 69     public void test3(){
 70         StudentInterface mapper = session.getMapper(StudentInterface.class);
 71 // random ID
 72         String string = UUID.randomUUID().toString();
 73 student student = new student (string, "rhubarb", "Java", "666666");
 74         mapper.insertStudent(student);
 75 // submit, do not commit, do not execute SQL
 76         session.commit();
 77 system.out.println ("added successfully");
 78     }
 79 
 80     /**
 81 * delete information based on student name
 82      */
 83     @Test
 84     public void test4(){
 85         StudentInterface mapper = session.getMapper(StudentInterface.class);
 86 string name = "Xiao Fang";
 87         mapper.deleteStudent(name);
 88 // submit, do not commit, do not execute SQL
 89         session.commit();
 90 system.out.println ("deleted successfully");
 91     }
 92 
 93     /**
 94 * modify student information
 95      */
 96     @Test
 97     public void test5(){
 98         StudentInterface mapper = session.getMapper(StudentInterface.class);
 99         HashMap hashMap = new HashMap<>();
100 // key value must be consistent with the modified #{name}
101 hashmap.put ("name", "Nana");
102         hashMap.put("dept","UI");
103         hashMap.put("phone","9999999");
104 // the original ID of the database is modified by the ID
105         hashMap.put("id","50d235cd-f62e-418d-925f-a215aac8f692");
106         mapper.updateStudent(hashMap);
107 // submit, do not commit, do not execute SQL
108         session.commit();
109 system.out.println ("modified successfully");
110     }
111 }

Student entity class:

 1 package com.Charon.enty;
 2 
 3 /**
 4  * @Description TODO
 5  * @Author Charon <[email protected]>
 6  * @create 2020-10-20-10:38
 7  * @Version 1.0.0
 8  */
 9 public class Student {
10     private String stu_id;
11     private String stu_name;
12     private String stu_dept;
13     private String stu_phone;
14 
15     public String getStu_id() {
16         return stu_id;
17     }
18 
19     public void setStu_id(String stu_id) {
20         this.stu_id = stu_id;
21     }
22 
23     public String getStu_name() {
24         return stu_name;
25     }
26 
27     public void setStu_name(String stu_name) {
28         this.stu_name = stu_name;
29     }
30 
31     public String getStu_dept() {
32         return stu_dept;
33     }
34 
35     public void setStu_dept(String stu_dept) {
36         this.stu_dept = stu_dept;
37     }
38 
39     public String getStu_phone() {
40         return stu_phone;
41     }
42 
43     public void setStu_phone(String stu_phone) {
44         this.stu_phone = stu_phone;
45     }
46 
47     public Student(String stu_id, String stu_name, String stu_dept, String stu_phone) {
48         this.stu_id = stu_id;
49         this.stu_name = stu_name;
50         this.stu_dept = stu_dept;
51         this.stu_phone = stu_phone;
52     }
53 
54     public Student() {
55     }
56 }

Studenttest test class

1 import com.Charon.dao.StudentInterface;
  2 import com.Charon.enty.Student;
  3 import com.Charon.utile.MyBatisUtil;
  4 import jdk.internal.util.xml.impl.Input;
  5 import org.apache.ibatis.io.Resources;
  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 java.io.IOException;
 13 import java.io.InputStream;
 14 import java.io.Reader;
 15 import java.util.HashMap;
 16 import java.util.List;
 17 import java.util.UUID;
 18 
 19 /**
 20  * @Description TODO
 21  * @Author Charon <[email protected]>
 22  * @create 2020-10-20-10:55
 23  * @Version 1.0.0
 24  */
 25 public class StudentTest {
 26 
 27     SqlSession session = null;
 28 
 29     @Before
 30     public void test() throws IOException {
 31         String resources = "mybatis-config.xml";
 32         InputStream inputStream = Resources.getResourceAsStream(resources);
 33         SqlSessionFactory instance = MyBatisUtil.getInstance(inputStream);
 34         session = instance.openSession();
 35     }
 36 
 37     /**
 38 * query all student information
 39      */
 40     @Test
 41     public void test1(){
 42 // through proxy: generate the implementation class name of the interface
 43         StudentInterface mapper = session.getMapper(StudentInterface.class);
 44 // get the method to query all information in the interface
 45         List students = mapper.listStudent();
 46 // traverse the collection to get data
 47         for (int i = 0; i < students.size(); i++) {
 48             Student student = students.get(i);
 49 system.out.println ("No.: + student.getstu_ ID () + "\ t \ tstudent Name:" + student.getstu_ Name() + "\ t \ t \ tstudent major:" + student.getstu_ Dept() + "\ t \ t \ tstudent phone:" + student.getstu_ phone());
 50         }
 51     }
 52 
 53 
 54     /**
 55 * query data according to student name
 56      */
 57     @Test
 58     public void test2(){
 59         StudentInterface mapper = session.getMapper(StudentInterface.class);
 60 string name = "Xiao Fang";
 61         Student student = mapper.chackByName(name);
 62 system.out.println ("Student Name:" + student.getstu_ Name() + "\ t \ tstudent phone:" + student.getstu_ phone());
 63     }
 64 
 65     /**
 66 * add student information
 67      */
 68     @Test
 69     public void test3(){
 70         StudentInterface mapper = session.getMapper(StudentInterface.class);
 71 // random ID
 72         String string = UUID.randomUUID().toString();
 73 student student = new student (string, "rhubarb", "Java", "666666");
 74         mapper.insertStudent(student);
 75 // submit, do not commit, do not execute SQL
 76         session.commit();
 77 system.out.println ("added successfully");
 78     }
 79 
 80     /**
 81 * delete information based on student name
 82      */
 83     @Test
 84     public void test4(){
 85         StudentInterface mapper = session.getMapper(StudentInterface.class);
 86 string name = "Xiao Fang";
 87         mapper.deleteStudent(name);
 88 // submit, do not commit, do not execute SQL
 89         session.commit();
 90 system.out.println ("deleted successfully");
 91     }
 92 
 93     /**
 94 * modify student information
 95      */
 96     @Test
 97     public void test5(){
 98         StudentInterface mapper = session.getMapper(StudentInterface.class);
 99         HashMap hashMap = new HashMap<>();
100 // key value must be consistent with the modified #{name}
101 hashmap.put ("name", "Nana");
102         hashMap.put("dept","UI");
103         hashMap.put("phone","9999999");
104 // the original ID of the database is modified by the ID
105         hashMap.put("id","50d235cd-f62e-418d-925f-a215aac8f692");
106         mapper.updateStudent(hashMap);
107 // submit, do not commit, do not execute SQL
108         session.commit();
109 system.out.println ("modified successfully");
110     }
111 }