ResultMap of MyBatis, MyBatisresultMap
1. When you learn about MyBatis, you may encounter different entity-class attributes and database fields.
For example, database ------ entity class
Stuname ----> name
That is, the name attribute of the fact body class corresponding to the stuname field in the database
If the stuname value cannot be correctly queried using the regular query method, It will be displayed as null
In this case, we can use our resultMap to solve this problem...
Source code introduction and comparison:
1. Student. java (entity class)
Package cn. zhang. entity; import java. util. date;/*** Student Entity class **/public class Student {private Integer stuno; private String name; private Integer stuage; private Date studate; @ Override public String toString () {return "Student [stuno =" + stuno + ", name =" + name + ", stuage =" + stuage + ", studate =" + studate + "]";} public Integer getStuno () {return stuno;} public void setStuno (Integer stuno) {this. stuno = stuno;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Integer getStuage () {return stuage;} public void setStuage (Integer stuage) {this. stuage = stuage;} public Date getStudate () {return studate;} public void setStudate (Date studate) {this. studate = studate ;}}View Code
2. mybatis-config.xml (configuration file for MyBatis)
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configurationPUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Configure the alias --> <typeAliases> <! -- Method 1: Customize aliases by type name --> <typeAlias type = "cn. zhang. entity. Student" alias = "Student"/> <! -- Method 2: Use the simple class name under the current specified package as the alias --> <! -- <Package name = "cn. zhang. entity"/> --> </typeAliases> <environments default = "mysql"> <environment id = "mysql"> <! -- Use jdbc transactions --> <transactionManager type = "JDBC"/> <! -- Use the built-in connection pool --> <dataSource type = "POOLED"> <property name = "driver" value = "com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: // localhost: 3306/y2161 "/> <property name =" username "value =" root "/> <property name =" password "value =" root "/> </dataSource> </ environment> </environments> <mappers> <mapper resource = "cn/zhang/dao/StudentDAO. xml "/> </mappers> </configuration>
View Code
3. MybatisUtil. java (Tool class for obtaining sessions)
Package cn. zhang. util; import java. io. IOException; import java. io. reader; import org. apache. ibatis. io. resources; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder;/*** get the tool class of the session **/public class MybatisUtil {private static String config = "mybatis-config.xml"; static Reader reader; static {try {reader = Resources. getResourceAsReader (config);} catch (IOException e) {e. printStackTrace () ;}} private static SqlSessionFactory factory = new SqlSessionFactoryBuilder (). build (reader); // provides a method for obtaining the session public static SqlSession getSession () throws IOException {SqlSession session = factory. openSession (); return session ;}}View Code
4. StudentDao. java (interface for defining methods)
Package cn. zhang. dao; import java. io. IOException; import java. util. list; import cn. zhang. entity. student; public interface StudentDao {/*** query all records * @ return * @ throws IOException */public List <Student> findAll () throws IOException ;}View Code
5. StudentDaoImpl. java (Implementation class for implementing interface methods)
Package cn. zhang. dao. impl; import java. io. IOException; import java. util. list; import org. apache. ibatis. session. sqlSession; import cn. zhang. dao. studentDao; import cn. zhang. entity. student; import cn. zhang. util. mybatisUtil; public class studentdaoimplements StudentDao {SqlSession session; public StudentDaoImpl () throws IOException {session = MybatisUtil. getSession ();}/*** query all */public java. util. list <Student> findAll () throws IOException {List <Student> list = session. selectList ("findAll"); session. close (); return list ;}}View Code
6. StudentDAO. xml (corresponding ing file)
The most common configuration method:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapperPUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" cn. zhang. dao "> <! -- Query all --> <select id = "findAll" resultType = "Student"> select * from student </select> </mapper>
The method of using resultMap:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapperPUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" cn. zhang. dao "> <! -- Result ing, the corresponding value in the database and object classes is specified --> <resultMap type = "Student" id = "findstudent"> <result property = "name" column = "stuname"/> </ resultMap> <! -- Query all --> <select id = "findAll" resultMap = "findstudent"> select * from student </select> </mapper>
7. log4j. properties
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c\:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=debug, stdoutView Code
8 .. MyTest. java (test class)
Package cn. zhang. test; import java. io. IOException; import java. util. list; import org. junit. before; import org. junit. test; import cn. zhang. dao. studentDao; import cn. zhang. dao. impl. studentDaoImpl; import cn. zhang. entity. student; public class MyTest {StudentDao; @ Before public void initData () throws IOException {dao = new StudentDaoImpl ();} /*** query all students * @ throws IOException */@ Test public void findAll () throws IOException {List <Student> list = dao. findAll (); for (Student student: list) {System. out. println ("No.:" + student. getStuno () + "name:" + student. getName ());}}}View Code
The result of the regular matching method in StudentDAO. xml:
Corresponding to the resultMap matching result in StudentDAO. xml:
In this way, the value can be obtained successfully, which is the role of resultMap.
Isn't it easy? Here is just a record ..