Mybatis, as an ORM lightweight framework, attracted countless people's attention as soon as it appeared. It is simpler and easier to get started than hibernate. Let's start my first mybatis program.
1. Download mybatis package
We know that any framework will have its package. We download its package from its official website. The official website address is: http://www.mybatis.org/mybatis-3/, and the version I use here is 3.3.0. After the download is completed, you can see the following directory structure:
mybatis-3.3.0.jar is its package, and in the lib directory is its dependency package. We put these packages into our project. What I created here is a javaweb project, which is convenient for web testing in the future. The program I wrote is an ordinary java program.
2. Configuration environment
After putting the mybatis package in the project's lib directory, configure the mybatis environment next. We know that mybatis, as an ORM framework, belongs to the DAO layer in development and deals with the database, so we must have data. Here, take mysql data as an example, and the specific database construction and table construction will not be explained here.
Create mybatis configuration file in the src directory, the file name is: configuratin.xml, and the file content is as follows:
<?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="Message" type="com.cn.imooc.entity.Message"/> </typeAliases> <environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/weixin?useUnicode=true&characterEncoding=UTF-8" /><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><!--Mapping File--> <mappers><mapper resource="com/cn/mappers/message.xml"/></mappers></configuration>
There are still many configuration items in the mybatis configuration file.
<typeAliases> alias configuration, that is, the entity class is aliased. The purpose is to not use full class names when using entity classes in the mapping file, but use alias, which plays a simple role
<environments> Configure some environments such as data configuration, here we configure the data source
<mappers> configure the mapping file, here is the message.xml mapping file under the com.cn.mappers package configured.
The following is an explanation of the Message entity class. This entity class contains some properties, as follows:
package com.cn.imooc.entity;public class Message {private String id;private String command;private String description;private String comment;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getCommand() {return command;}public void setCommand(String command) {this.command = command;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getComment() {return comment;}public void setComment(String comment) {this.comment = comment;}@Overridepublic String toString() {return "Message [id=" + id + ", command=" + command + ", description="+ description + ", comment=" + comment + "]";}}The getXXX and setXXX methods are provided, and the setXXX method is very critical. The properties here are the same as the field names of the database. They can be easily reflected into the entity class by using mybatis to query the results. Of course, they can also be inconsistent with the field names of the database tables. They will be explained later.
The message.xml mapping file is as follows,
<mapper namespace="com.cn.inter.IMessageOperation"><select id="selectUserByID" parameterType="int" resultType="com.cn.imooc.entity.Message">select * from `message` where id = #{id}</select><select id="selectMessages" resultType="Message">select id,command,description,commentfrom message;</select></mapper>This is my mapper mapping file, there are two methods inside, one is: selectUserById query based on id, and the other is selectMessages query all
Okay, so far, our mybatis environment has been built, and we can test it below.
3. Test
Below is the test code,
package com.cn.test;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.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.cn.imooc.entity.Message;public class MyTest {public static void main(String[] args) {// TODO Auto-generated method stubReader reader;SqlSession sqlSession=null;try {//1. Obtain sqlSessionFactoryreader = Resources.getResourceAsReader("Configuration.xml");SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);//2. Obtain sqlSessionsqlSession=sqlSessionFactory.openSession();//3. Query Message message=sqlSession.selectOne("com.cn.inter.IMessageOperation.selectUserByID", 1);System.out.println(message);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{sqlSession.close();}}}As can be seen from the above, first a SqlSessionFactory is needed, and then a sqlSessionFactory is obtained. The sqlSession is executed by the sqlSession, and the selectOne method is used. The first parameter is the nameSpace+"." method name in the mapping file, and the second parameter is the query parameter.
The above is the full description of how MyBatis is used (I) introduced to you by the editor. I hope it will be helpful to you. Other versions will be introduced in the future. Please pay attention to Wulin.com for more content!