idea建立MyBatis項目,並實現增,刪,改,查

項目目錄:

 

log4j.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 3 
 4 <log4j:configuration >
 5     <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
 6         <param name="Encoding" value="UTF-8" />
 7         <layout class="org.apache.log4j.PatternLayout">
 8             <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" />
 9         </layout>
10     </appender>
11     <logger name="java.sql">
12         <level value="debug" />
13     </logger>
14     <logger name="org.apache.ibatis">
15         <level value="info" />
16     </logger>
17     <root>
18         <level value="debug" />
19         <appender-ref ref="STDOUT" />
20     </root>
21 </log4j:configuration>

pom.xml配置

 1 <!--依賴-->
 2   <dependencies>
 3     <dependency>
 4       <groupId>junit</groupId>
 5       <artifactId>junit</artifactId>
 6       <version>4.11</version>
 7       <scope>test</scope>
 8     </dependency>
 9 
10     <dependency>
11       <groupId>mysql</groupId>
12       <artifactId>mysql-connector-java</artifactId>
13       <version>5.1.29</version>
14     </dependency>
15 
16     <dependency>
17       <groupId>org.mybatis</groupId>
18       <artifactId>mybatis</artifactId>
19       <version>3.4.1</version>
20     </dependency>
21 
22     <dependency>
23       <groupId>log4j</groupId>
24       <artifactId>log4j</artifactId>
25       <version>1.2.17</version>
26     </dependency>
27 
28     <dependency>
29       <groupId>org.slf4j</groupId>
30       <artifactId>slf4j-api</artifactId>
31       <version>1.7.12</version>
32     </dependency>
33 
34     <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
35     <dependency>
36       <groupId>org.mybatis.generator</groupId>
37       <artifactId>mybatis-generator-core</artifactId>
38       <version>1.3.5</version>
39     </dependency>
40   </dependencies>
41 
42   <!--加載資源-->
43   <build>
44     <resources>
45       <resource>
46         <directory>src/main/resources</directory>
47         <includes>
48           <include>**/*.properties</include>
49           <include>**/*.xml</include>
50           <include>**/*.tld</include>
51           <include>**/*.jsp</include>
52         </includes>
53         <filtering>true</filtering>
54       </resource>
55       <resource>
56         <directory>src/main/java</directory>
57         <includes>
58           <include>**/*.properties</include>
59           <include>**/*.xml</include>
60           <include>**/*.tld</include>
61         </includes>
62         <filtering>true</filtering>
63       </resource>
64     </resources>
65   </build>

 

mybatis-config.xml

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!--加載數據庫配置文件-->
 7     <properties resource="db.properties"/>
 8 
 9     <!--給類名指定一個別名-->
10     <typeAliases>
11         <typeAlias type="com.Charon.enty.Student" alias="student"/>
12     </typeAliases>
13 
14     <!-- 數據庫鏈接環境的配置 -->
15     <environments default="development">
16         <environment id="development">
17             <transactionManager type="JDBC" />
18             <!-- 數據源-->
19             <dataSource type="POOLED">
20                 <property name="driver" value="${jdbc.driver}"/>
21                 <property name="url" value="${jdbc.url}" />
22                 <property name="username" value="${jdbc.username}" />
23                 <property name="password" value="${jdbc.password}" />
24             </dataSource>
25         </environment>
26     </environments>
27     <mappers>
28         <!--必須全包名,否則找不到-->
29         <mapper resource="com/Charon/dao/mapper.xml"></mapper>
30     </mappers>
31 </configuration>

mapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <!--namespace裏面的值必須是dao層接口的全包名限定-->
 4 <mapper namespace="com.Charon.dao.StudentInterface">
 5     <!--id的值必須是接口裏面的查詢全部學生信息的方法名-->
 6     <!--student返回值類型,是給類指定的別名(Studenta==student)-->
 7     <select id="listStudent" resultType="student">
 8         SELECT
 9         stu_id,
10         stu_name,
11         stu_dept,
12         stu_phone
13         FROM
14         students
15     </select>
16 
17     <!--根據名字查詢全部學生-->
18     <select id="chackByName" resultType="student" parameterType="string">
19         SELECT
20         stu_id,
21         stu_name,
22         stu_dept,
23         stu_phone
24         FROM
25         students
26         WHERE
27 --         變量名沒有限制
28         stu_name = #{name}
29     </select>
30 
31     <!--添加學生信息-->
32     <insert id="insertStudent" parameterType="student">
33         INSERT INTO
34         students(stu_id,stu_name,stu_dept,stu_phone)
35         VALUES
36 --         字段必須與實體類一致
37         (#{stu_id},#{stu_name},#{stu_dept},#{stu_phone})
38     </insert>
39 
40     <!--根據學生姓名刪除信息-->
41     <delete id="deleteStudent" parameterType="string">
42         DELETE FROM
43         students WHERE stu_name=#{name}
44     </delete>
45 
46     <!--修改學生信息-->
47     <update id="updateStudent" parameterType="hashmap">
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     </update>
57 </mapper>

 

 

db.properties數據庫配置文件

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

數據庫設計

 模擬幾條數據

StudentInterface接口類

 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 <1819248612@qq.com>
11  * @create 2020-10-20-10:43
12  * @Version 1.0.0
13  */
14 public interface StudentInterface {
15     //查詢全部學生信息
16     public List<Student>  listStudent();
17 
18     //根據名字查詢學生信息
19     public Student chackByName(String name);
20 
21     //添加學生信息
22     public void insertStudent(Student student);
23 
24     //根據學生姓名刪除信息
25     public void deleteStudent(String name);
26 
27     //修改學生信息
28     public void updateStudent(HashMap<String,String> hashMap);
29  }

MyBatisUtil工具類

 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  代理工廠類
10  * @Author Charon <1819248612@qq.com>
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測試類

  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 <1819248612@qq.com>
 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      * 查詢學生全部信息
 39      */
 40     @Test
 41     public void test1(){
 42         //經過代理:生成接口的實現類名稱
 43         StudentInterface mapper = session.getMapper(StudentInterface.class);
 44         //獲取接口中查詢全部信息的方法
 45         List<Student> students = mapper.listStudent();
 46         //遍歷集合,獲取數據
 47         for (int i = 0; i < students.size(); i++) {
 48             Student student = students.get(i);
 49             System.out.println("編號:"+student.getStu_id()+"\t\t學生姓名:"+student.getStu_name()+"\t\t\t學生專業:"+student.getStu_dept()+"\t\t\t學生電話:"+student.getStu_phone());
 50         }
 51     }
 52 
 53 
 54     /**
 55      * 根據學生姓名查詢數據
 56      */
 57     @Test
 58     public void test2(){
 59         StudentInterface mapper = session.getMapper(StudentInterface.class);
 60         String name= "小芳";
 61         Student student = mapper.chackByName(name);
 62         System.out.println("學生姓名:"+student.getStu_name()+"\t\t學生電話:"+student.getStu_phone());
 63     }
 64 
 65     /**
 66      * 添加學生信息
 67      */
 68     @Test
 69     public void test3(){
 70         StudentInterface mapper = session.getMapper(StudentInterface.class);
 71         //隨機id
 72         String string = UUID.randomUUID().toString();
 73         Student student = new Student(string,"大黃","java","666666");
 74         mapper.insertStudent(student);
 75         //提交,不提交不執行sql
 76         session.commit();
 77         System.out.println("添加成功");
 78     }
 79 
 80     /**
 81      * 根據學生姓名刪除信息
 82      */
 83     @Test
 84     public void test4(){
 85         StudentInterface mapper = session.getMapper(StudentInterface.class);
 86         String name = "小芳";
 87         mapper.deleteStudent(name);
 88         //提交,不提交不執行sql
 89         session.commit();
 90         System.out.println("刪除成功");
 91     }
 92 
 93     /**
 94      * 修改學生信息
 95      */
 96     @Test
 97     public void test5(){
 98         StudentInterface mapper = session.getMapper(StudentInterface.class);
 99         HashMap<String,String> hashMap = new HashMap<>();
100         //key值必須與修改的#{name}一致
101         hashMap.put("name","娜娜");
102         hashMap.put("dept","UI");
103         hashMap.put("phone","9999999");
104         //數據庫原有的id,經過id修改
105         hashMap.put("id","50d235cd-f62e-418d-925f-a215aac8f692");
106         mapper.updateStudent(hashMap);
107         //提交,不提交不執行sql
108         session.commit();
109         System.out.println("修改爲功");
110     }
111 }

Student實體類:

 1 package com.Charon.enty;
 2 
 3 /**
 4  * @Description TODO
 5  * @Author Charon <1819248612@qq.com>
 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測試類

  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 <1819248612@qq.com>
 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      * 查詢學生全部信息
 39      */
 40     @Test
 41     public void test1(){
 42         //經過代理:生成接口的實現類名稱
 43         StudentInterface mapper = session.getMapper(StudentInterface.class);
 44         //獲取接口中查詢全部信息的方法
 45         List<Student> students = mapper.listStudent();
 46         //遍歷集合,獲取數據
 47         for (int i = 0; i < students.size(); i++) {
 48             Student student = students.get(i);
 49             System.out.println("編號:"+student.getStu_id()+"\t\t學生姓名:"+student.getStu_name()+"\t\t\t學生專業:"+student.getStu_dept()+"\t\t\t學生電話:"+student.getStu_phone());
 50         }
 51     }
 52 
 53 
 54     /**
 55      * 根據學生姓名查詢數據
 56      */
 57     @Test
 58     public void test2(){
 59         StudentInterface mapper = session.getMapper(StudentInterface.class);
 60         String name= "小芳";
 61         Student student = mapper.chackByName(name);
 62         System.out.println("學生姓名:"+student.getStu_name()+"\t\t學生電話:"+student.getStu_phone());
 63     }
 64 
 65     /**
 66      * 添加學生信息
 67      */
 68     @Test
 69     public void test3(){
 70         StudentInterface mapper = session.getMapper(StudentInterface.class);
 71         //隨機id
 72         String string = UUID.randomUUID().toString();
 73         Student student = new Student(string,"大黃","java","666666");
 74         mapper.insertStudent(student);
 75         //提交,不提交不執行sql
 76         session.commit();
 77         System.out.println("添加成功");
 78     }
 79 
 80     /**
 81      * 根據學生姓名刪除信息
 82      */
 83     @Test
 84     public void test4(){
 85         StudentInterface mapper = session.getMapper(StudentInterface.class);
 86         String name = "小芳";
 87         mapper.deleteStudent(name);
 88         //提交,不提交不執行sql
 89         session.commit();
 90         System.out.println("刪除成功");
 91     }
 92 
 93     /**
 94      * 修改學生信息
 95      */
 96     @Test
 97     public void test5(){
 98         StudentInterface mapper = session.getMapper(StudentInterface.class);
 99         HashMap<String,String> hashMap = new HashMap<>();
100         //key值必須與修改的#{name}一致
101         hashMap.put("name","娜娜");
102         hashMap.put("dept","UI");
103         hashMap.put("phone","9999999");
104         //數據庫原有的id,經過id修改
105         hashMap.put("id","50d235cd-f62e-418d-925f-a215aac8f692");
106         mapper.updateStudent(hashMap);
107         //提交,不提交不執行sql
108         session.commit();
109         System.out.println("修改爲功");
110     }
111 }
相關文章
相關標籤/搜索