mybatis多表關聯(lián)查詢之resultMap單個對象

resultMap的n+1方式實現(xiàn)多表查詢(多對一)

實體類

創(chuàng)建班級類(Clazz)和學生類(Student)鹰贵,并在Student中添加一個Clazz類型的屬性晴氨,用于表示學生的班級信息康嘉。

mapper層

提供studentMapper和ClazzMapper,studentMapper查詢所有學生的信息籽前,ClazzMapper根據(jù) 編號查詢班級信息亭珍。再StudentMapper中使用<association>配置裝配。

<association> 用于關聯(lián)一個對象

  • property:用于關聯(lián)一個對象枝哄。
  • select:設定要繼續(xù)引用的查詢肄梨,namespace+id
  • column:查詢時需要傳遞的列

service層

由于裝配已經(jīng)完成,serivce層只需調(diào)用mapper即可挠锥,不需要再進行裝配了众羡。

測試代碼

多表關聯(lián)查詢resultMap單個對象_N+1方式實現(xiàn)

[![

](https://upload-images.jianshu.io/upload_images/22757822-6d6ec49922abe158.gif?imageMogr2/auto-orient/strip)](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">1 package com.bjsxt.mapper; 2
3 import com.bjsxt.pojo.Clazz; 4
5 public interface ClazzMapper { 6
7 Clazz selById(int id); 8 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">1 package com.bjsxt.mapper; 2
3 import java.util.List; 4
5 import com.bjsxt.pojo.Student; 6
7 public interface StudentMapper { 8 List<Student> selAll(); 9 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-|3-mapper.dtd">
5 <mapper namespace="com.bjsxt.mapper.ClazzMapper">
6 <select id="selById" resultType="clazz" parameterType="int">
7 select * from t_class where id=#{0}
8 </select>
9
10 </mapper>
11 </pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-|3-mapper.dtd">
5 <mapper namespace="com.bjsxt.mapper.StudentMapper">
6 <resultMap type="student" id="smap">
7 <id property="id" column="id"/>
8 <result property="name" column="name"/>
9 <result property="age" column="age"/>
10 <result property="gender" column="gender"/>
11 <result property="cid" column="cid"/>
12
13 <association property="clazz" select="com.bjsxt.mapper.ClazzMapper.selById" column="cid"></association>
14 </resultMap>
15 <select id="selAll" resultMap="smap">
16 select * from t_student 17 </select>
18
19 </mapper>
20 </pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.pojo; 2
3 import java.io.Serializable; 4
5 public class Clazz implements Serializable{ 6 private int id; 7 private String name; 8 private String room; 9
10 public int hashCode() { 11 final int prime = 31; 12 int result = 1; 13 result = prime * result + id; 14 result = prime * result + ((name == null) ? 0 : name.hashCode()); 15 result = prime * result + ((room == null) ? 0 : room.hashCode()); 16 return result; 17 } 18 public boolean equals(Object obj) { 19 if (this == obj) 20 return true; 21 if (obj == null) 22 return false; 23 if (getClass() != obj.getClass()) 24 return false; 25 Clazz other = (Clazz) obj; 26 if (id != other.id) 27 return false; 28 if (name == null) { 29 if (other.name != null) 30 return false; 31 } else if (!name.equals(other.name)) 32 return false; 33 if (room == null) { 34 if (other.room != null) 35 return false; 36 } else if (!room.equals(other.room)) 37 return false; 38 return true; 39 } 40 public String toString() { 41 return "Clazz [id=" + id + ", name=" + name + ", room=" + room + "]"; 42 } 43 public Clazz() { 44 super(); 45 // TODO Auto-generated constructor stub
46 } 47 public Clazz(int id, String name, String room) { 48 super(); 49 this.id = id; 50 this.name = name; 51 this.room = room; 52 } 53 public int getId() { 54 return id; 55 } 56 public void setId(int id) { 57 this.id = id; 58 } 59 public String getName() { 60 return name; 61 } 62 public void setName(String name) { 63 this.name = name; 64 } 65 public String getRoom() { 66 return room; 67 } 68 public void setRoom(String room) { 69 this.room = room; 70 } 71
72 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.pojo; 2
3 import java.io.Serializable; 4
5 public class Student implements Serializable{ 6 private int id; 7 private String name; 8 private int age; 9 private String gender; 10 private int cid; 11 private Clazz clazz;//用于封裝班級信息
12 public int hashCode() { 13 final int prime = 31;
14 int result = 1;
15 result = prime * result + age; 16 result = prime * result + cid; 17 result = prime * result + ((clazz == null) ? 0 : clazz.hashCode()); 18 result = prime * result + ((gender == null) ? 0 : gender.hashCode()); 19 result = prime * result + id; 20 result = prime * result + ((name == null) ? 0 : name.hashCode()); 21 return result; 22 }
23 public boolean equals(Object obj) { 24 if (this == obj) 25 return true;
26 if (obj == null)
27 return false;
28 if (getClass() != obj.getClass()) 29 return false;
30 Student other = (Student) obj; 31 if (age != other.age) 32 return false;
33 if (cid != other.cid) 34 return false;
35 if (clazz == null) {
36 if (other.clazz != null)
37 return false;
38 } else if (!clazz.equals(other.clazz))
39 return false;
40 if (gender == null) {
41 if (other.gender != null)
42 return false;
43 } else if (!gender.equals(other.gender))
44 return false;
45 if (id != other.id) 46 return false;
47 if (name == null) {
48 if (other.name != null)
49 return false;
50 } else if (!name.equals(other.name))
51 return false;
52 return true;
53 }
54 public String toString() { 55 return "Student [id=" + id + ", name=" + name + ", age=" + age 56 + ", gender=" + gender + ", cid=" + cid + ", clazz=" + clazz + "]";
57 }
58 public Student(int id, String name, int age, String gender, int cid, Clazz clazz) { 59 super();
60 this.id = id; 61 this.name = name; 62 this.age = age; 63 this.gender = gender; 64 this.cid = cid; 65 this.clazz = clazz; 66 }
67 public Student() { 68 super();
69 // TODO Auto-generated constructor stub
70 }
71 public int getId() { 72 return id; 73 }
74 public void setId(int id) { 75 this.id = id; 76 }
77 public String getName() { 78 return name; 79 }
80 public void setName(String name) { 81 this.name = name; 82 }
83 public int getAge() { 84 return age; 85 }
86 public void setAge(int age) { 87 this.age = age; 88 }
89 public String getGender() { 90 return gender; 91 }
92 public void setGender(String gender) { 93 this.gender = gender; 94 }
95 public int getCid() { 96 return cid; 97 }
98 public void setCid(int cid) { 99 this.cid = cid; 100 } 101 public Clazz getClazz() { 102 return clazz; 103 } 104 public void setClazz(Clazz clazz) { 105 this.clazz = clazz; 106 } 107
108 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.service; 2
3 import java.util.List; 4
5 import com.bjsxt.pojo.Student; 6
7 public interface StudentService { 8 List<Student> selAll(); 9
10 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.service.impl; 2
3 import java.util.List; 4
5 import org.apache.ibatis.session.SqlSession; 6
7 import com.bjsxt.mapper.ClazzMapper; 8 import com.bjsxt.mapper.StudentMapper; 9 import com.bjsxt.pojo.Clazz; 10 import com.bjsxt.pojo.Student; 11 import com.bjsxt.service.StudentService; 12 import com.bjsxt.util.MyBatisUtil; 13
14 public class StudentServiceImpl implements StudentService{ 15
16 @Override 17 public List<Student> selAll() { 18 SqlSession session =MyBatisUtil.getSession(); 19
20 StudentMapper stuMapper = session.getMapper(StudentMapper.class); 21 List<Student> list = stuMapper.selAll(); 22
23 session.close(); 24 return list; 25 } 26
27 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.test; 2
3 import java.util.List; 4
5 import com.bjsxt.pojo.Student; 6 import com.bjsxt.service.StudentService; 7 import com.bjsxt.service.impl.StudentServiceImpl; 8
9 public class TestQuery { 10
11 public static void main(String[] args) { 12 StudentService ss=new StudentServiceImpl(); 13 List<Student> list = ss.selAll(); 14 for (Student student : list) { 15 System.out.println(student); 16 } 17
18
19 } 20
21 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.util; 2
3 import java.io.IOException; 4 import java.io.InputStream; 5
6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10
11 public class MyBatisUtil { 12
13 private static SqlSessionFactory factory=null; 14 static{ 15
16 try { 17 InputStream is=Resources.getResourceAsStream("mybatis.xml"); 18 factory=new SqlSessionFactoryBuilder().build(is); 19 } catch (IOException e) { 20 // TODO Auto-generated catch block
21 e.printStackTrace(); 22 } 23 } 24
25 public static SqlSession getSession(){ 26 SqlSession session =null; 27 if(factory!=null){ 28 //true表示開啟
29 session= factory.openSession(true); 30 } 31 return session; 32 } 33 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/java505?useSSL=true&characterEncoding=utf8&useSSL=true
3 jdbc.username=root 4 jdbc.password=root</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 # Set root category priority to INFO and its only appender to CONSOLE.
2 log4j.rootCategory=ERROR, CONSOLE
3 # log4j.rootCategory=DEBUG, CONSOLE, LOGFILE
4
5 # 單獨設置SQL語句的輸出級別為DEBUG級別
6 log4j.logger.com.bjsxt.mapper=DEBUG
7
8 # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
9 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 11 log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n 12
13 # LOGFILE is set to be a File appender using a PatternLayout. 14 log4j.appender.LOGFILE=org.apache.log4j.FileAppender 15 log4j.appender.LOGFILE.File=d:/test.log 16 log4j.appender.LOGFILE.Append=true
17 log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout 18 log4j.appender.LOGFILE.layout.ConversionPattern=- %m %l%n</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 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 <properties resource="db.properties"/>
7 <settings>
8
9 <setting name="logImpl" value="LOG4J"/>
10 </settings>
11
12 <typeAliases>
13
14
15
16 <package name="com.bjsxt.pojo"/>
17 </typeAliases>
18
21 <environments default="dev">
22
25 <environment id="dev">
26
30 <transactionManager type="JDBC"/>
31
37 <dataSource type="POOLED">
38 <property name="driver" value="{jdbc.driver}"/> 39 <property name="url" value="{jdbc.url}"/>
40 <property name="username" value="{jdbc.username}"/> 41 <property name="password" value="{jdbc.password}"/>
42 </dataSource>
43 </environment>
44 </environments>
45
46
47 <mappers>
48
49 <package name="com.bjsxt.mapper"/>
50
51 </mappers>
52 </configuration></pre>

[
復制代碼

](javascript:void(0); "復制代碼")

多表關聯(lián)查詢resultMap單個對象_關聯(lián)方式實現(xiàn)

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">package com.bjsxt.mapper; import java.util.List; import com.bjsxt.pojo.Student; public interface StudentMapper {

List<Student> selAll();

}</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="com.bjsxt.mapper.StudentMapper">
6 <resultMap type="student" id="smap">
7 <id property="id" column="sid" />
8 <result property="name" column="sname" />
9 <result property="age" column="age" />
10 <result property="gender" column="gender" />
11 <result property="cid" column="cid" />
12 <association property="clazz" javaType="clazz">
13 <id property="id" column="cid" />
14 <result property="name" column="cname" />
15 <result property="room" column="room" />
16 </association>
17 </resultMap>
18 <select id="selAll" resultMap="smap">
19 select s.id sid, s.name sname, s.age, s.gender, c.id cid, c.name cname, c.room 20 from t_student s 21 left join t_class c 22 on s.cid=c.id 23 </select>
24 </mapper></pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.pojo; 2
3 import java.io.Serializable; 4
5 public class Clazz implements Serializable { 6
7 private int id; 8 private String name; 9 private String room; 10
11 public Clazz() { 12 super(); 13 } 14
15 public int getId() { 16 return id; 17 } 18
19 public void setId(int id) { 20 this.id = id; 21 } 22
23 public String getName() { 24 return name; 25 } 26
27 public void setName(String name) { 28 this.name = name; 29 } 30
31 public String getRoom() { 32 return room; 33 } 34
35 public void setRoom(String room) { 36 this.room = room; 37 } 38
39 @Override 40 public int hashCode() { 41 final int prime = 31; 42 int result = 1; 43 result = prime * result + id; 44 result = prime * result + ((name == null) ? 0 : name.hashCode()); 45 result = prime * result + ((room == null) ? 0 : room.hashCode()); 46 return result; 47 } 48
49 @Override 50 public boolean equals(Object obj) { 51 if (this == obj) 52 return true; 53 if (obj == null) 54 return false; 55 if (getClass() != obj.getClass()) 56 return false; 57 Clazz other = (Clazz) obj; 58 if (id != other.id) 59 return false; 60 if (name == null) { 61 if (other.name != null) 62 return false; 63 } else if (!name.equals(other.name)) 64 return false; 65 if (room == null) { 66 if (other.room != null) 67 return false; 68 } else if (!room.equals(other.room)) 69 return false; 70 return true; 71 } 72
73 @Override 74 public String toString() { 75 return "Clazz [id=" + id + ", name=" + name + ", room=" + room + "]"; 76 } 77 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.pojo; 2
3 import java.io.Serializable; 4
5 public class Student implements Serializable { 6
7 private int id; 8 private String name; 9 private int age; 10 private String gender; 11 private int cid; 12 private Clazz clazz;// 用于封裝班級信息
13
14 public Student() { 15 super();
16 }
17
18 public int getId() { 19 return id; 20 }
21
22 public void setId(int id) { 23 this.id = id; 24 }
25
26 public String getName() { 27 return name; 28 }
29
30 public void setName(String name) { 31 this.name = name; 32 }
33
34 public int getAge() { 35 return age; 36 }
37
38 public void setAge(int age) { 39 this.age = age; 40 }
41
42 public String getGender() { 43 return gender; 44 }
45
46 public void setGender(String gender) { 47 this.gender = gender; 48 }
49
50 public int getCid() { 51 return cid; 52 }
53
54 public void setCid(int cid) { 55 this.cid = cid; 56 }
57
58 public Clazz getClazz() { 59 return clazz; 60 }
61
62 public void setClazz(Clazz clazz) { 63 this.clazz = clazz; 64 }
65
66 @Override
67 public int hashCode() { 68 final int prime = 31;
69 int result = 1;
70 result = prime * result + age; 71 result = prime * result + cid; 72 result = prime * result + ((clazz == null) ? 0 : clazz.hashCode()); 73 result = prime * result + ((gender == null) ? 0 : gender.hashCode()); 74 result = prime * result + id; 75 result = prime * result + ((name == null) ? 0 : name.hashCode()); 76 return result; 77 }
78
79 @Override
80 public boolean equals(Object obj) { 81 if (this == obj) 82 return true;
83 if (obj == null)
84 return false;
85 if (getClass() != obj.getClass()) 86 return false;
87 Student other = (Student) obj; 88 if (age != other.age) 89 return false;
90 if (cid != other.cid) 91 return false;
92 if (clazz == null) {
93 if (other.clazz != null)
94 return false;
95 } else if (!clazz.equals(other.clazz))
96 return false;
97 if (gender == null) {
98 if (other.gender != null)
99 return false; 100 } else if (!gender.equals(other.gender)) 101 return false; 102 if (id != other.id) 103 return false; 104 if (name == null) { 105 if (other.name != null) 106 return false; 107 } else if (!name.equals(other.name)) 108 return false; 109 return true; 110 } 111
112 @Override 113 public String toString() { 114 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", cid=" + cid 115 + ", clazz=" + clazz + "]"; 116 } 117 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.service; 2
3 import java.util.List; 4
5 import com.bjsxt.pojo.Student; 6
7 public interface StudentService { 8
9 List<Student> selAll(); 10 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.service.impl; 2
3 import java.util.List; 4
5 import org.apache.ibatis.session.SqlSession; 6
7 import com.bjsxt.mapper.StudentMapper; 8 import com.bjsxt.pojo.Student; 9 import com.bjsxt.service.StudentService; 10 import com.bjsxt.util.MyBatisUtil; 11
12 public class StudentServiceImpl implements StudentService { 13
14 public List<Student> selAll() { 15 SqlSession session = MyBatisUtil.getSession(); 16
17 // 學生mapper
18 StudentMapper stuMapper = session.getMapper(StudentMapper.class); 19
20 List<Student> list = stuMapper.selAll(); 21
22 session.close(); 23 return list; 24 } 25
26 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.test; 2
3 import java.util.List; 4
5 import com.bjsxt.pojo.Student; 6 import com.bjsxt.service.StudentService; 7 import com.bjsxt.service.impl.StudentServiceImpl; 8
9 public class TestQuery { 10
11 public static void main(String[] args) { 12 StudentService ss = new StudentServiceImpl(); 13 List<Student> list = ss.selAll(); 14 for (Student student : list) { 15 System.out.println(student); 16 } 17 } 18
19 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 package com.bjsxt.util; 2
3 import java.io.IOException; 4 import java.io.InputStream; 5
6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10
11 public class MyBatisUtil { 12
13 private static SqlSessionFactory factory = null; 14
15 static { 16 try { 17 InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml"); 18 factory = new SqlSessionFactoryBuilder().build(is); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 } 23
24 public static SqlSession getSession() { 25 SqlSession session = null; 26 if (factory != null) { 27 // true表示開啟自動提交 28 // session = factory.openSession(true);
29 session = factory.openSession(); 30 } 31 return session; 32 } 33 }</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/java505
3 jdbc.username=root 4 jdbc.password=root</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 # Set root category priority to INFO and its only appender to CONSOLE.
2 log4j.rootCategory=ERROR, CONSOLE
3 # log4j.rootCategory=DEBUG, CONSOLE, LOGFILE
4
5 # 單獨設置SQL語句的輸出級別為DEBUG級別
6 log4j.logger.com.bjsxt.mapper=DEBUG
7
8 # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
9 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 11 log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n 12
13 # LOGFILE is set to be a File appender using a PatternLayout. 14 log4j.appender.LOGFILE=org.apache.log4j.FileAppender 15 log4j.appender.LOGFILE.File=d:/test.log 16 log4j.appender.LOGFILE.Append=true
17 log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout 18 log4j.appender.LOGFILE.layout.ConversionPattern=- %m %l%n</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin: 0px; padding: 0px; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 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 <settings>
10
11 <setting name="logImpl" value="LOG4J"/>
12 </settings>
13
14 <typeAliases>
15 <package name="com.bjsxt.pojo" />
16 </typeAliases>
17 <environments default="dev">
18 <environment id="dev">
19 <transactionManager type="JDBC" />
20 <dataSource type="POOLED">
21 <property name="driver" value="{jdbc.driver}"/> 22 <property name="url" value="{jdbc.url}"/>
23 <property name="username" value="{jdbc.username}"/> 24 <property name="password" value="{jdbc.password}"/>
25 </dataSource>
26 </environment>
27 </environments>
28 <mappers>
29 <package name="com.bjsxt.mapper" />
30 </mappers>
31 </configuration></pre>

[
復制代碼

](javascript:void(0); "復制代碼")

個人學習筆記,便于查閱及加深蓖租。

https://www.cnblogs.com/wq-9/p/10238824.html

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末粱侣,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蓖宦,更是在濱河造成了極大的恐慌齐婴,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件稠茂,死亡現(xiàn)場離奇詭異柠偶,居然都是意外死亡,警方通過查閱死者的電腦和手機睬关,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門诱担,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人电爹,你說我怎么就攤上這事蔫仙。” “怎么了藐不?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵匀哄,是天一觀的道長。 經(jīng)常有香客問我雏蛮,道長涎嚼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任挑秉,我火速辦了婚禮法梯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己立哑,他們只是感情好夜惭,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著铛绰,像睡著了一般诈茧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上捂掰,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天敢会,我揣著相機與錄音,去河邊找鬼这嚣。 笑死鸥昏,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的姐帚。 我是一名探鬼主播吏垮,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼罐旗!你這毒婦竟也來了膳汪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤尤莺,失蹤者是張志新(化名)和其女友劉穎旅敷,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體颤霎,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡媳谁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了友酱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晴音。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖缔杉,靈堂內(nèi)的尸體忽然破棺而出锤躁,到底是詐尸還是另有隱情,我是刑警寧澤或详,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布系羞,位于F島的核電站,受9級特大地震影響霸琴,放射性物質(zhì)發(fā)生泄漏椒振。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一梧乘、第九天 我趴在偏房一處隱蔽的房頂上張望澎迎。 院中可真熱鬧庐杨,春花似錦、人聲如沸夹供。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽哮洽。三九已至填渠,卻和暖如春姑廉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背磺浙。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工查坪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人娱挨。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親洒缀。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容