mybatis entry project

Long time no Laizheliwan (1'ᴗ`1), and review the high number of English and go, long way to go ah. But really very loving and professional schools, I found it worth the effort ヾ (◍ ° ∇ ° ◍) Techno ゙.

 ------------------------------------------------------------------------------------------------------------

First configure pom.xml file

GroupId uniquely identifies the company or organization, the path configuration is also generated to thereby generate

As < the groupId > com.wxy </ the groupId > Maven project labeled jar package will put local path: / com / wxy

 

Unique ID artifactId this item, a plurality of possible groupId following items, distinguished by the artifact.

 

< Version > the SNAPSHOT-1.0 </ Version >  The present project is located version 

< Packaging > jar </ Packaging > mechanism pom packaged, jar, maven-plugin, ejb , war, ear, rar, par default jar
< Dependencies > dependencies defined in the project
Under normal circumstances, it is retrieved by Maven groupId, artifactId, version value of these three elements (commonly known coordinates) of the member, and then introduced into your project. 
If someone would like to quote you now develop this project (provided that has been developed is completed and published to the remote repository),
 
you need to create a dependency node in his pom file, groupId the project, artifactId, version written,
 Maven on you will upload the jar package to his local
 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wxy</groupId>
    <artifactId>second</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <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.48</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>
    </dependencies>
</project>

 

userdao.java

package com.wxy.dao;
import com.wxy.domain.user;

import java.util.List;

public  interface UserDAO {
     // persistence layer user interface 
    List <User> findAll ();
}

user.java

package com.wxy.domain;

import java.io.Serializable;

public class user implements Serializable {
    private Integer empid;
    private char first_name;
    private Integer salary;

    public void setEmpid(Integer empid) {
        this.empid = empid;
    }

    public void setFirst_name(char first_name) {
        this.first_name = first_name;
    }

    public void setSalary(Integer salary) {
        this.salary = salary;
    }

    public Integer getEmpid() {
        return empid;
    }

    public char getFirst_name() {
        return first_name;
    }

    public Integer getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "user{" +
                "empid=" + empid +
                ", first_name=" + first_name +
                ", salary=" + salary +
                "}\n";
    }
}

userdao.xml

mapper constraints

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapprt 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

Mybatis mapping configuration file location and packet structure must be the same as the interface dao

Here userdao.java In com.wxy.dao, so userdao.xml also under com.wxy.dao

The value mapper mapping configuration file of the namespace attribute dao tag must be fully qualified class name when the interface < mapper namespace = "com.wxy.dao.userdao" >

Operating configuration mapping configuration file (e.g., select), ID dao method name attribute value must interface, which eliminates the need dao implementation class

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapprt 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 
< Mapper namespace = "com.wxy.dao.userdao" > 
    <-! configure the query userdao in the method behind all id - > 
    < SELECT ID = "findAll" the resultType = "com.wxy.domain.user" >
            select * from emp where salary>5000
    </select>
</mapper>

mybatis-config.xml

Configuring the types of things < the transactionManager type = "the JDBC" > </ the transactionManager >

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mtbatis.org/dtd/mybatis-3-config.dtd" > 
<-! the mybatis main configuration file -> 
< the Configuration > 
    <-! Configuration Environment -> 
    < Environments default = "MySQL " > 
        <! - configuring MySQL environment -> 
        < environment ID =" MySQL " > 
            < the transactionManager type =" the JDBC " > </ the transactionManager > 
            <! - the configuration data source (connection pool -> 
            < the dataSource type = "the POOLED" > 
                <!- configuration database coupled four basic information -> 
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="2999029"/>
            </dataSource>
        </environment>
    </environments>
    <!- <->Specifies the location mapping configuration file, the map file refers to each separate profile dao
    mappers>
        <mapper resource="com/wxy/dao/userdao.xml"/>
    </mappers>
</configuration>

 

test.java

package com.wxy;

import com.wxy.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.IOException;
import java.io.InputStream;
import java.util.List;

import com.wxy.dao.*;
import com.wxy.domain.*;
public class test {
    public static void main(String[] args) throws IOException {
        /*
        1. Read the configuration file
        2. Create factory SqlSessionFactory
        3. Use an object factory SqlSession
        4. Use SqlSession create Dao interface proxy object
        The object execution method using a proxy
        6. Release Resource
         */
        InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory= builder.build(in);
        SqlSession session= factory.openSession();
        userdao ud=session.getMapper(userdao.class);
        List<user>users=ud.findall();
        for(user user:users){
            System.out.print(user);
        }
        session.close();
        in.close();
    }

 

 

Not finished! !

 

 

Guess you like

Origin www.cnblogs.com/zuiaimiusi/p/11670941.html
Recommended