mybatis learning: mybatis the environment to build

A, mybatis overview:

    mybatis is a persistence framework, using java

    It encapsulates many of the details jdbc operations, so that developers only need to focus sql statement itself, without concern registration drive, create a connection complicated registration process

    It uses ORM thought to achieve the results set package

    SNAKE:

        Object Relational Mappging object-relational mapping

        simply put:

            Is the entity classes and database tables and attributes of entity class association

            So that we can work with entity classes to achieve operation of the database table

 

Two, mybatis built environment

Step 1: Create project and import coordinates maven

 1 <packaging>jar</packaging>
 2 
 3     <dependencies>
 4         <dependency>
 5             <groupId>org.mybatis</groupId>
 6             <artifactId>mybatis</artifactId>
 7             <version>3.4.5</version>
 8         </dependency>
 9         <dependency>
10             <groupId>mysql</groupId>
11             <artifactId>mysql-connector-java</artifactId>
12             <version>5.1.6</version>
13         </dependency>
14         <dependency>
15             <groupId>log4j</groupId>
16             <artifactId>log4j</artifactId>
17             <version>1.2.12</version>
18         </dependency>
19         <dependency>
20             <groupId>junit</groupId>
21             <artifactId>junit</artifactId>
22             <version>4.10</version>
23         </dependency>
24     </dependencies>

 

Step 2: Create entity classes and interfaces dao

Step 3: Create Mybatis main configuration file: SqlMapConifg.xml

  1. Create SqlMapConifg.xml in the resources directory

  2. Import Config constraints:

  

<?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">

  3. Write the configuration file

  

. 1 <-! MyBatis main configuration file ->
 2 <Configuration>
 . 3      <-! Configuration Environment ->
 . 4      <Environments default = "mysql">
 . 5          <-! Mysql configuration environment ->
 6          < ID = Environment "MySQL">
 . 7              <-! transaction type configuration ->
 . 8              <the transactionManager type = "the JDBC"> </ the transactionManager>
 . 9              <-! configuration data sources, also called connection pooling ->
 10              < type = the dataSource "the POOLED">
 . 11                  <-!. 4 basic configuration database connection information ->
 12 is                  <Property name = "Driver" value = "
com.mysql.jdbc.Driver"/>
13                 <Property name = "URL" value = "JDBC: MySQL: // localhost: 3307 / Test" />
 14                  <Property name = "username" value = "the root" />
 15                  <Property name = "password" value = " flypig "/>
 16              </ the dataSource>
 . 17          </ Environment>
 18 is      </ Environments>
 . 19  
20 is      <-! mapping configuration file specifies the location, mapping configuration file refers to each separate profiles dao ->
 21      < by mappers>
 22 is          <= Resource Mapper "CN / flypig666 / DAO / IUserDao.xml"> </ Mapper>
 23 is      </ by mappers>
 24 </ Configuration>

 

Step Four: Create a mapping configuration file: IUserDao.xml

  For example: cn under resoures directory / flypig666 / dao / IUserDao.xml

  1. Import of constraint Mapper

<?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">
 

  2. Write Configuration

1  <mapper namespace="cn.flypig666.dao.IUserDao">
2     <!--配置查询所有-->
3     <select id="findAll">
4         select * from user
5     </select>
6 
7 </mapper>

 

Guess you like

Origin www.cnblogs.com/flypig666/p/11483165.html