MyBatis入门实例

手写文章,错误之处请不吝指出!

转载请注明出处!粗人

原文地址:http://www.cnblogs.com/curen/p/5909223.html

开发环境介绍:

window7、jdk8、mysql5.6、tomcat8、ecplise、maven、navicat for mysql

 

1、数据表建立

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `age` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=gbk;

 

 

2、MyBatis相关的包引入

路径:/pom.xml

<!--引入MyBatis支持包-->
<dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.8</version>
</dependency>

<!--引入Mysql支持包-->
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
</dependency>

 

3、新建POJO类

路径:/batis/User.java

package batis;

public class User {

    private int id;
    private String name;
    private int age;
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

 

4、配置mybatis SQL映射配置文件

路径:/mapper/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">
<!-- namespace为唯一命名空间,通常定义规则为包名+文件名 -->
<mapper namespace="mapper.User">
    <!-- 
    1、select标签用于编写查询的SQL语句
    2、id为标签的唯一标识
    3、parameterType为参数类型
    4、resultType为返回结果类型,此处则反射为User类
     -->
    <select id="getUser" parameterType="int" resultType="batis.User" >
        select * from user where id=#{id}
    </select>
</mapper>

 

5、配置MyBatis配置文件

路径:conf.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>
    <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:数据库端口/数据库名称" />
                <property name="username" value="数据库连接用户名" />
                <property name="password" value="数据库连接密码" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- 注册映射文件-->
        <mapper resource="mapper/User.xml" />
    </mappers>
</configuration>

 

6、测试运行一下

路径:batis/test.java

package batis;

import java.io.InputStream;

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) {
        //mybatis的配置文件
        String resource = "conf.xml";
        
        //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
        InputStream is = test.class.getClassLoader().getResourceAsStream(resource);
        
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
       
        //创建能执行映射文件中sql的sqlSession
        SqlSession session = sessionFactory.openSession();
       
        //映射sql的标识字符串
        String statement = "mapper.User.getUser";
        
        //查询配置,配置文件命名空间+标签ID
        User user = session.selectOne(statement,2);
        System.out.println("ID:"+user.getId());
        System.out.println("姓名:"+user.getName());
        System.out.println("年龄:"+user.getAge());
    }
}

 

posted @ 2016-09-26 14:59  粗人-  阅读(205)  评论(0编辑  收藏  举报