1. Create Project
<groupId>com.hope</groupId>
<artifactId>day01_eesy_01mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
2, add mybatis coordinates
Add the following coordinates in pom.xml:
<dependencies>
<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.12</version>
</dependency>
</dependencies>
3, the preparation of the entity classes and interfaces dao
4. Preparation member mapping file interface persistence layer IUserDao.xml
Requirements:
Create positions: persistence layer interfaces must be on the same package.
Name: the file name must be named to the persistence layer interface name, extension is .xml.
IUserDao.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="com.hope.dao.IUserDao">
<!--配置查询所有-->
<select id="findAll">
select * from user;
</select>
</mapper>
5, 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 ">
<configuration>
<-! configuration environment ->
<environments default =" mysql ">
<-! mysql configuration environment ->
<environment ID =" mysql ">
<! - - transaction type configuration ->
<the transactionManager type = "the JDBC" />
<-! configuration data source (connection pool) ->
<type the dataSource = "the POOLED">
<-! four basic system configuration information of the database - ->
<Property name = "Driver" value = "com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
<Property name = "username" value = "the root" />
<Property name = "password" value = "123" />
</ the dataSource>
</ Environment>
</ Environments>
<-! mapping configuration file specifies the location , mapping configuration file refers to each separate profiles dao ->
<by mappers>
<Resource Mapper = "COM / Hope / dao / IUserDao.xml" />
</ by mappers>
</ configuration>
. 6, write test classes
write test classes 2.2.7
/ **
*
* <the p-> the Title: MybatisTest </ the p->
* <the p-> the Description: test mybatis environment </ the p->
* <the p-> Company: http://www.itheima.com / </ p>
* /
Public class MybatisTest {
public static void main (String [] args) throws Exception {
//. 1. Read the configuration file
= Resources.getResourceAsStream in InputStream ( "SqlMapConfig.xml");
. 2 // create SqlSessionFactory of builders target
the SqlSessionFactoryBuilder the SqlSessionFactoryBuilder Builder = new new ();
. // 3 using builders to create a factory object SqlSessionFactory
SqlSessionFactory Factory's builder.build = ( in);
.. 4 // the SqlSession produced using SqlSessionFactory
SqlSession factory.openSession the session = ();
.. 5 // SqlSession created using the interface proxy object dao
IUserDao userDao = session.getMapper (IUserDao.class);
// use. 6. All methods proxy object executes the query
List <the User> Users userDao.findAll = ();
for (the User User: Users) {
System.out.println (User);
}
.. 7 // release resources
Session.close ();
in. Close ();
}
}