1. MyBatis' HelloWord
1. Create a SqlSessionFactory object based on the xml configuration file (global configuration file) and has some running environment information.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN" "/dtd/"> <configuration> <environments default="development"> <environment > <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <!-- Write ussqlMapping files()Be sure to register with the global configuration file()middle --> <mappers> <mapper resource="" /> </mappers> </configuration>
Map files; configure each SQL, as well as SQL encapsulation rules, etc.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/"> <mapper namespace=""> <!-- namespace:Namespace;Specified as the full class name of the interface id:Unique ID resultType:Return value type #{id}: Take out the id value from the passed parameter public Employee getEmpById(Integer id); Separate implementation and interface --> <select resultType=""> select id,last_name lastName,email,gender from tbl_employee where id = #{id} </select> </mapper>
3. Register the sql mapping file in the global configuration file
<mappers> <mapper resource="" /> </mappers>
4. Write code:
1). Obtain SqlSessionFactory based on the global configuration file;
String resource = ""; InputStream inputStream = (resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
2). Use the sqlSession factory to obtain the sqlSession object and use it to perform addition, deletion, modification and search. A sqlSession represents a session with the database and closes it after use.
SqlSession openSession = ();
3). Use the unique flag of sql to tell MyBatis which sql to execute. SQL is stored in SQL mapping file
try { Employee employee = ( "", 1); // spacename + sqlId (employee); } finally { (); }
2. MyBatis interface programming
mybatis: (interface) ====> (accomplish)
The advantage of interface programming is that it can separate functions from implementations
1. SqlSession represents a session with the database; it must be closed after use;
2. SqlSession is like connection and is non-thread-safe. You should get a new object every time you use it.
3. The interface does not implement the class, but mybatis will generate a proxy object for this interface. (Bind the interface and xml)
EmployeeMapper empMapper = ();
4. Two important configuration files:
- Mybatis' global configuration file: contains database connection pool information, transaction manager information, etc. System operation environment information
- sql mapping file: saves the mapping information of each sql statement: extracts sql.
This is the article about the precautions for my first experience of MyBatis. For more information about MyBatis usage, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!