How to get started with Mybatis and how to get started with Mybatis
The basic tutorial of mybatis is mainly from the document on the official website. Describes how to create a mybatis instance.
(Ps. here only the mybatis download link is provided. Please download the corresponding SQL driver library. Here I use Mysql)
First, go to the drawing:
Re-build the mybatis Core Profile mybatis-config.xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Load the configuration file. Here, I stepped on a pitfall. The order of his label is required: Content Model: (properties ?, Settings ?, TypeAliases ?, TypeHandlers ?, ObjectFactory ?, ObjectWrapperFactory ?, ReflectorFactory ?, Plugins ?, Environments ?, DatabaseIdProvider ?, Mappers ?) At first, the location of properties and typeAliases was changed and an error was reported. It's not very user-friendly. Aggregate (aggregate metadata) aggregate --> <properties resource = "dbinfo. properties"> </properties> <! -- Is Aliase, alias => type Internal Conversion --> <typeAliases> <typeAlias type = "com. denny. dao. course "alias =" course "/> </typeAliases> <! -- Environment configuration, load the driver --> <environments default = "development"> <environment id = "development"> <transactionManager type = "JDBC"/> <dataSource type = "POOLED"> <property name = "driver" value = "$ {driver}"/> <property name = "url" value = "$ {url}"/> <property name = "username" value = "$ {username}"/> <property name = "password" value = "$ {password}"/> </dataSource> </environment> </environments> <! -- Here, mappers corresponds to multiple different SQL curd statements. There are multiple SQL CURD statements .. --> <Mappers> <mapper resource = "jdbc1Mapper. xml"/> </mappers> </configuration>
Create some information about database dbinfo. properties to read the mybatis-config.xml core configuration file:
url = jdbc:mysql://localhost/jdbc1?autoReconnect=true&useSSL=falsedriver = com.mysql.jdbc.Driverusername = mysqlpassword = mysql
The next step is to create a mapper. The corresponding SQL statement is jdbc1Mapper. xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- Namespace refers to the interface package. The query is a new feature of mybatis, which is described later, course refers to replacing Aliase with core configuration --> <mapper namespace = "com. denny. mapper. courseMapper "> <select id =" selectCourse "resultType =" course "> select * from course where C =#{ id} </select> </mapper>
The configuration is completed, and the next step is to write java code. The number of codes is small, and only a few lines are done. Compared with jbdc, It is a convenient introduction. java:
Package com. denny. intro; import java. io. IOException; import java. io. inputStream; import javax. annotation. resource; import javax. annotation. resources; import javax. xml. transform. source; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; import com. denny. dao. course; import com. denny. mapper. courseMapper; publ Ic class introduction {public introduction () {// TODO Auto-generated constructor stub} // here, SqlSessionFactory is constructed by reading xml. If it is built directly from a java program, I don't want to talk about it. Public void loadMybatis () {String srouce = "mybatis-config.xml"; // uri SqlSessionFactory factory = null for the core configuration file of mybatis; // temporarily understood as a factory, can generate many executable CURD // load the factory try {InputStream iStream = org. apache. ibatis. io. resources. getResourceAsStream (srouce); factory = new SqlSessionFactoryBuilder (). build (iStream);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace (); System. out. println (E. getMessage (). toString ();} // SqlSession is the method for executing SQL statements. The official introduction: // SqlSession fully contains all the methods required to execute SQL commands for databases. SqlSession session = factory. openSession (); course c = null; try {/** can execute any SQL operation after an object such as session is obtained. * There are two methods, one is to operate through session. selectOne, and the other is to operate through an interface. The latter is selected here. * The reason is described in this document: * "use interfaces (such as BlogMapper) that can reasonably describe parameters and return values for a given statement. class), * You can not only execute clearer and more secure type code, but also do not have to worry about the error-prone String Literal Value and forced type conversion "*/courseMapper cm = session. getMapper (courseMapper. class); c = cm. selectCourse (1); // returns the ID to be queried} catch (Exception e) {// TODO: handle exception System. out. println (e. getMessage (). toString ();} System. out. println (c. getCName ();} public static void main (String [] args) {new introduction (). loadMybatis (); System. out. println ("Test ");}}
The following are some DAO and interface classes. Let's write a summary:
Package com. denny. dao; public class course {int C; String CName; int T ;//........... the get and set methods of these attributes are omitted.} package com. denny. mapper; import com. denny. dao. course; public interface courseMapper {course selectCourse (int id );}
A database table (course) has three columns: C (int), Cname (String), and T (int ). I will not go into detail;
If it succeeds, you can find the number of columns corresponding to C equal to 1.
After reading the entry content of Mybatis, sort it out first. If you have any questions, please click it. Withdrawal ~~