What is Mybatis
MyBatis is an open source project of apache. In 2010, this project was moved from apache software foundation to Google code and was renamed MyBatis. The term iBATIS comes from the combination of "internet" and "abatis", and is a Java-based persistence layer framework. iBATIS provides persistence layer frameworks including SQL Maps and Data Access Objects (DAO).
MyBatis is an open source project of apache. In 2010, this project was moved from apache software foundation to Google code and was renamed MyBatis. Migrated to Github in November 2013.
In June 2011, iBatis was renamed MyBatis. From iBatis to MyBatis, it is not just a change in name. MyBatis provides more powerful functions without losing its ease of use. On the contrary, it has been simplified in many places with the help of JDK's generics and annotation features. So you know, start learning from MyBatis.
Examples use MyBatis+MySQL to implement a query of a user table, as follows:
1. Database
In MySQL, under the test database, the resume user table, fields: id, name, password, and table creation statement omitted.
2. Packages to be imported
There are only two: mybatis-3.0.3.jar mysql-connector-java-5.1.16-bin.jar (JDBC package)
3. Directory structure
Using the simplest structure, there are only four files under the com.mybatis package: configuration.xml, user.xml, User.java, and Test.java.
4. Mybatis configuration file configuration.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="User" type="com.mybatis.User"></typeAlias> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="user.xml"/> </mappers> </configuration>
5. User table sql file User.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="User"> <select id="selectUser" parameterType="int" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> <select id="selectUsers" resultType="User"> SELECT * FROM user </select> </mapper>6. Table structure file User.java
package com.mybatis; public class User { private int id; private String name; private String password; public User(){} public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + this.id + ", name=" + this.name + ", password=" + this.password + "]"; } } 7. Test case Test.java
package com.mybatis; import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test { public static void main(String[] args) throws IOException { String resource = "configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); SqlSession session = ssf.openSession(); try{ User user = session.selectOne("selectUser", "1"); System.out.println(user.getName()); System.out.println(user); System.out.println("--------------分隔线---------------"); List<User> users = session.selectList("selectUsers"); for(int i=0; i<users.size(); i++) { System.out.println(users.get(i).getName()); } } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } }The above is a simple example of MyBatis, a Java persistence layer framework introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!