mybaties第一个例子

我对MyBatis的理解仅仅是:它以前叫IBtais,现在投到谷歌帐下,改名MyBatis,这个框架把JDBC中的重复代码封装起来,减少代码量。仅仅知道这些而已,慢慢学习吧,从网上找了一个最简单的例子。

MyBatis+MySQL的,记录如下:

①数据库

在MySQL中,test数据库下,简历user表,一共两个字段id和name,建表语句略。

②需导入的包

只有两个:

mybatis-3.0.3.jar

mysql-connector-java-5.1.9-bin.jar(JDBC包)

③目录结构

采用最简单的结构,domain包下有且仅有四个文件

configuration.xml

User.xml

User.java

Test.java

④实体类javabean,总共两个变量

 

 

package domain;

public class User {

 public User() {
  
 }
 public User(int id, String name) { 
        this.id = id; 
        this.name = name; 
 } 

 private int id; 
 private String name = null;
 
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public String toString() {
  return "User [id=" + id + ", name=" + name + "]"; 
 }
}

 

⑤MyBatis总配置文件configuration.xml

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

 

⑥User类的配置文件User.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="User"> 
    <select id="selectUser" parameterType="int" resultType="domain.User">
        select * from user where id = #{id}
    </select> 
</mapper>

 

⑦测试用例

package domain;

import java.io.IOException; 
import java.io.Reader; 
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 = "domain/configuration.xml";
        String resource = "domain/configuration.xml";
        Reader reader = Resources.getResourceAsReader(resource); 
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); 
        SqlSession session = ssf.openSession(); 
        try { 
            User user = (User) session.selectOne("selectUser", "2"); 
            System.out.println(user); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            session.close(); 
        } 
    } 

 

你可能感兴趣的:(mybaties第一个例子)