Home > Java > javaTutorial > How to use Mybatis in Java

How to use Mybatis in Java

PHP中文网
Release: 2017-06-23 11:51:36
Original
1613 people have browsed it

In June 2011, iBatis changed its name to MyBatis. From iBatis to MyBatis, it is not just a change in name. MyBatis provides more powerful functions without losing its ease of use. On the contrary, in many places All are simplified with the help of JDK's generics and annotation features. So you know, start learning from MyBatis.

The example uses MyBatis+MySQL to implement a user table query, as follows:

①Database In MySQL, under the test database, the resume user table, fields: id, name, password , the table creation statement is abbreviated.

②There are only two packages that need to be imported: mybatis-3.0.3.jar mysql-connector-java-5.1.16-bin.jar (JDBC package)

③The directory structure adopts the simplest structure. There are only four files configuration.xml user.xml under the com.mybatis package. User.java Test.java

④mybatis configuration file configuration.xml,

[html]


##

<?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="User" type="com.mybatis.User"></typeAlias>  
    </typeAliases>  
      
    <environments default="development">  
        <environment id="development">  
            <transactionManager type="JDBC"></transactionManager>  
            <dataSource type="POOLED">  
                <property name="driver" value="com.mysql.jdbc.Driver" />  
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />  
                <property name="username" value="root" />  
                <property name="password" value="123456" />  
            </dataSource>  
        </environment>  
    </environments>  
      
    <mappers>  
        <mapper resource="user.xml"/>  
    </mappers>  
  
</configuration>
Copy after login


⑤User table sql file User.xml,

[html]


<?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="User">  
    <select id="selectUser" parameterType="int" resultType="User">  
        SELECT * FROM user WHERE id = #{id}  
    </select>  
      
    <select id="selectUsers" resultType="User">  
        SELECT * FROM user  
    </select>  
      
</mapper>
Copy after login

⑥Table structure file User .java,

[java]


package com.mybatis;  
public class User {  
    private int id;  
    private String name;  
    private String password;  
      
    public User(){}  
    public User(int id, String name) {  
        this.id = id;  
        this.name = name;  
    }  
      
    public int getId() {  
        return this.id;  
    }  
      
    public void setId(int id) {  
        this.id = id;  
    }  
      
    public String getName() {  
        return this.name;  
    }  
      
    public void setName(String name) {  
        this.name = name;  
    }  
      
    public String getPassword() {  
        return this.password;  
    }  
      
    public void setPassword(String password) {  
        this.password = password;  
    }  
      
    @Override  
    public String toString() {  
        return "User [id=" + this.id + ", name=" + this.name + ", password=" + this.password + "]";  
    }  
      
}
Copy after login

⑦Test case Test.java

[java]


package com.mybatis;  
  
import java.io.IOException;  
import java.io.Reader;  
import java.util.List;  
  
import org.apache.ibatis.io.Resources;  
import org.apache.ibatis.session.SqlSession;  
import org.apache.ibatis.session.SqlSessionFactory;  
import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  
public class Test {  
    public static void main(String[] args) throws IOException {  
        String resource = "configuration.xml";  
        Reader reader = Resources.getResourceAsReader(resource);  
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);  
        SqlSession session = ssf.openSession();  
        try{  
            User user = session.selectOne("selectUser", "1");  
            System.out.println(user.getName());  
            System.out.println(user);  
            System.out.println("--------------分隔线---------------");  
          
            List<User> users = session.selectList("selectUsers");  
            for(int i=0; i<users.size(); i++) {  
                System.out.println(users.get(i).getName());  
            }  
              
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            session.close();  
        }  
    }  
}
Copy after login


The above is the detailed content of How to use Mybatis in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template