The first two days have been engaged in Angularjs, various look at the code, last night to close the collapse of the time, decided to see some other mediation under the mood, changed to the MyBatis.
One, basic configuration
1, the introduction of the MyBatis Jar package (GitHub address: https://github.com/mybatis/mybatis-3/releases), I am using 3.3.1 this version.
2, Core configuration file
Sample configuration file We can find in the source package:mybatis\mybatis-3-mybatis-3.3.1\src\test\java\org\apache\ibatis\submitted\complex_ property\Configuration. xml
To modify our connection string:
It feels like hibernate, don't catch the rush, wait to use it, will find more like.
two, write basic query code test
/** * Classes used to access the database * @author Liuhuichao * */public class DBAccess {public sqlsession getsqlsession () throws ioexception{//by matching File to get database connection Information Reader Reader=resources.getresourceasreader ("Com/lhc/conofig/configuration.xml");// Build Sqlsessionfactorysqlsessionfactory sqlsessionfactory=new Sqlsessionfactorybuilder (). Build (reader) with configuration information;// Open a database session through Sqlsessionfactory sqlsession sqlsession=sqlsessionfactory.opensession (); return sqlsession;}}
MyBatis is mainly done by a sqlsession class, the above code is the process of creating sqlsession.
Write a test for the entity class:
Configure the corresponding relationship of this class to the table and the SQL operation statement through the XML file:
<mapper namespace= "Message" > <resultmap type= "com.lhc.bean.Message" id= "Messageresult" > < ID column= "id" jdbctype= "INTEGER" property= "id"/> <result column= "command" jdbctype= "VARCHAR" property= " Command "/> <result column=" description "jdbctype=" VARCHAR "property=" description "/> <result column= "Content" jdbctype= "VARCHAR" property= "content"/> </resultMap> <!--call SQL statements by ID, The ID is to be unique-- <select id= "querymessagelist" resultmap= "Messageresult" > select Id,command, Description,content from message where 1=1 </select></mapper>
(SQL can also be configured into XML, the amount,, learning, this is what my big hibernate did not ...) )
Finally, don't forget to introduce the XML for the class into the core configuration file (same as Hibernate):
<mappers> <mapper resource= "Com/lhc/conofig/sqlxml/message.xml"/>
Finally, the query operation under test:
List<message> messagelist=new arraylist<message> ();D baccess dbaccess=new DBAccess (); Sqlsession sqlsession=null;try {sqlsession=dbaccess.getsqlsession ();//Execute SQL query Messagelist=sqlsession.selectlist (" Message.querymessagelist ");} catch (IOException e) {e.printstacktrace ();} Finally{sqlsession.close ();} Execute the SQL statement via sqlsession return messagelist;
Not to be continued ... (send me a picture of my collection for a long time.) )
mybatis--Getting Started Select