mybatis涔嬫敞瑙e紡寮€鍙戜箣鍏宠仈鏌ヨ
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis涔嬫敞瑙e紡寮€鍙戜箣鍏宠仈鏌ヨ相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/equals' title='equals'>equals
put pack error val deb null sql 绾у埆1 package com.bjsxt.mapper; 2 3 import org.apache.ibatis.annotations.Select; 4 5 import com.bjsxt.pojo.Clazz; 6 7 public interface ClazzMapper { 8 9 @Select("select * from t_class where id=#{0}") 10 Clazz selById(int id); 11 }
1 package com.bjsxt.mapper; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.One; 6 import org.apache.ibatis.annotations.Result; 7 import org.apache.ibatis.annotations.Results; 8 import org.apache.ibatis.annotations.Select; 9 10 import com.bjsxt.pojo.Student; 11 12 public interface StudentMapper { 13 14 @Select("select * from t_student") 15 @Results(value = { 16 @Result(column="id", property="id", id=true), 17 @Result(column="name", property="name"), 18 @Result(column="age", property="age"), 19 @Result(column="gender", property="gender"), 20 @Result(column="cid", property="cid"), 21 @Result(property="clazz", [email protected](select="com.bjsxt.mapper.ClazzMapper.selById"), column="cid") 22 }) 23 List<Student> sel(); 24 }
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 }
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 }
1 package com.bjsxt.test; 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.util.MyBatisUtil; 10 11 public class TestStu { 12 13 public static void main(String[] args) { 14 SqlSession session = MyBatisUtil.getSession(); 15 16 StudentMapper mapper = session.getMapper(StudentMapper.class); 17 18 List<Student> sel = mapper.sel(); 19 for (Student student : sel) { 20 System.out.println(student); 21 } 22 23 session.close(); 24 } 25 26 }
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 }
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/java505 3 jdbc.username=root 4 jdbc.password=root
# Set root category priority to INFO and its only appender to CONSOLE. log4j.rootCategory=ERROR, CONSOLE # log4j.rootCategory=DEBUG, CONSOLE, LOGFILE # 鍗曠嫭璁剧疆SQL璇彞鐨勮緭鍑虹骇鍒负DEBUG绾у埆 log4j.logger.com.bjsxt.mapper=DEBUG # CONSOLE is set to be a ConsoleAppender using a PatternLayout. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n # LOGFILE is set to be a File appender using a PatternLayout. log4j.appender.LOGFILE=org.apache.log4j.FileAppender log4j.appender.LOGFILE.File=d:/test.log log4j.appender.LOGFILE.Append=true log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=- %m %l%n
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鍔犺浇澶栭儴鏂囦欢 --> 7 <properties resource="db.properties" /> 8 <!-- settings鏍囩 --> 9 <settings> 10 <!-- 璁剧疆MyBatis浣跨敤log4j鏃ュ織鏀寔 --> 11 <setting name="logImpl" value="LOG4J"/> 12 </settings> 13 <!-- typeAliases缁欑被鍨嬭捣鍒悕 --> 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>
以上是关于mybatis涔嬫敞瑙e紡寮€鍙戜箣鍏宠仈鏌ヨ的主要内容,如果未能解决你的问题,请参考以下文章
KoP 姝e紡寮€婧愶細鍦?Apache Pulsar 涓婃敮鎸佸師鐢?Kafka 鍗忚
銆愬睆骞曞姞宸ュ厬鎹?鍩硅銆戞洸灞?iPad 鎵嬭〃 浼樺娍鍔犲伐