Setting up an Mybatis development environment
mybatis the environment to build
Step 1: Create maven project and import coordinates
Step 2: Create entity classes and interfaces dao the
third step: to create a master configuration file Mybatis of
SqlMapConifg.xml
Step Four: Create a mapping configuration file
IUserDao.xml
environment Note built:
(1) create IUserDao.xml and IUserDao.java time and name to be consistent with our previous knowledge.
It mapping file name and operator interface persistence layer are also known in Mybatis: Mapper
so: IUserDao and IUserMapper same
mapping profile location (2) mybatis and packet structure must be the same as the interface dao
Namespace attribute value mapper tag (3) mapping configuration file must be fully qualified class name dao interface
(4) operating configuration mapping configuration file (select), the id attribute value must be the name of a method interface dao
When we follow the 2,3,4 point, we have in development without having to write dao implementation class .
Specific steps:
(1) create a maven project
Create New Project ---> Maven ---> do not choose skeleton, direct point next ---> Fill GroupId and ArtifactId ---> next ---> finish to create an ordinary java project.
(2) mybatis introduced into a jar
Use Maven to build the project, you will need the following code in dependency pom.xml file
Add Mybatis3.4.5 in pom.xml file coordinates
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6 </version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency>
(3) write SqlMapConfig.xml profile
<? Xml Version = "1.0" encoding = "UTF-8" ?> <! DOCTYPE the Configuration the PUBLIC "- // mybatis.org//DTD Config 3.0 // EN" "http://mybatis.org/dtd/mybatis config.dtd--3 " > <-! MyBatis main configuration file -> < configuration > <-! configuration environment -> < environments default =" mysql " > <-! mysql configuration environment - > < Environment the above mentioned id = "MySQL" > <-! configure the type of transaction -> <transactionManager type="JDBC"></the transactionManager > <-! configuration data source (connection pool) -> < the dataSource type = "the POOLED" > <-! . 4 basic configuration database connection information (below need to be modified according to actual conditions) -> < Property name = "Driver" value = "com.mysql.jdbc.Driver" /> < Property name = "URL" value = "JDBC: MySQL: // localhost: 3306 / MyBatis" /> < Property name = "username" value = "the root" /> < Property name = "password" value="plj824"/> </the dataSource > </ Environment > </ Environments > <-! position specified mapping configuration file, a mapping configuration file refers to each separate profiles dao -> < by mappers > <-! Method 1: xml configuration ( more trouble) -> <-! <Resource Mapper = "DAO / IUserDao.xml" /> -> ! <- method 2: configuration annotations (recommended) -> < Mapper class = "dao.IUserDao" /> </ by mappers > </ Configuration >
(4) create a mapping profile IUserDao.xml (under annotation mode, the file can be deleted)
mybatis annotation mode:
the IUserDao.xml removed using the method of dao @Select annotation interface, and specifies the SQL statements
also need SqlMapConfig.xml disposed in the mapper, the class attribute specified interface dao fully qualified class name .
<? Xml Version = "1.0" encoding = "UTF-8" ?> <! DOCTYPE Mapper the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN" "http://mybatis.org/dtd/mybatis mapper.dtd--3 " > <-! Note namespace write a relative path IUserDao this interface file -> < Mapper namespace =" dao.IUserDao " > <-! configure Search -> <! - - ID interface method corresponding to IserDao -> < SELECT ID = "the findAll" the resultType = "domain.User" > SELECT * from User </ SELECT > </mapper>
Getting Case:
User database table corresponding to the entity class User.java
package domain; import java.io.Serializable; import java.util.Date; /** * 数据库表对应的实体类 */ public class User implements Serializable { private Integer id; private String username; private Date birthday; private String sex; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}'; } }
IUserDao.java (Interface)
package dao; import domain.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface IUserDao { /** * 查询所有 * @return */ // 注解模式 @Select("select *from user") List<User> findAll(); }
Test categories:
package test; import dao.IUserDao; import domain.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; import java.util.List; public class MybatisTest01 { /** * 入门案例 * @param args */ public static void main(String[] args) throws{Exception // 1. read the configuration file the InputStream in Resources.getResourceAsStream = ( "the SqlMapConfig.xml" ); // 2. Create SqlSessionFactory the SqlSessionFactoryBuilder Builder = new new the SqlSessionFactoryBuilder (); // Create a SqlSessionFactory builder Builder SqlSessionFactory = Factory Builder .build (in); // use builders builder to create a SqlSessionFactory // 3. the SqlSession plant using SqlSession SQLSESSION = factory.openSession (); // 4. use SqlSessions Dao interface objects created proxy object IUserDao userDao = sqlSession. getMapper (IUserDao. class ); //5. proxy object execution method List <the User> Users = userDao.findAll (); for (the User User: Users) { System.out.println (User); } // 6. The release resources sqlSession.close (); in .close (); } }
Step of analyzing the test class code:
Principle Analysis Test Class Code: