mybatis环境配置

开发环境:jdk 1.8 myeclipse 2013 mybatis3.4.5 .jar mysql-connect-java5.1.41.jar
因为本次主要是学习mybatis,所以,只是新建了java项目
一.新建java项目
二.导入上述的jar包
三.创建数据库,并且添加数据
这里写图片描述
这里写图片描述
四.创建mybatis配置文件 mybatis-config.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">
<!-- mybatis配置 -->
<configuration>
    <!-- 设置mybatis别名 type属性就是实体类 -->
    <typeAliases>
        <typeAlias type="com.helloworld.model.User" alias="user" />
    </typeAliases>
    <!-- 环境配置 -->
    <environments default="development">
        <environment id="development">
        <!-- 事务配置,其中,包括:sessionFactory session transaction -->
            <transactionManager type="JDBC">
            </transactionManager>
        <!-- 配置数据库   包括数据库驱动,url,用户名 ,密码 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/test" />
                <property name="username" value="root" />
                <property name="password" value="110119" />
            </dataSource>
        </environment>
    </environments>
    <!-- 加载实体类映射文件 -->
    <mappers>
        <mapper resource="com/helloworld/model/UserMapper.xml" />
    </mappers>
</configuration>

详细看注释,由于对mybatis的机制了解不是很透彻,表述可能不准确,希望大家提意见,互相提高
五.创建实体类

/**
 * @author wuchao
 * @time 上午1:10:18
 * @description TODO
 */
package com.helloworld.model;

/**
 * @author wuchao
 * @time 上午1:10:18
 * 
 */
public class User {

    private Integer id;
    private String name;
    private String dept;
    private String phone;
    private String website;

    /**
     * @author wuchao
     * @time 上午1:38:11
     */
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @author wuchao
     * @time 下午2:06:22
     */
    public User(Integer id, String name, String dept, String phone,
            String website) {
        super();
        this.id = id;
        this.name = name;
        this.dept = dept;
        this.phone = phone;
        this.website = website;
    }



    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

}

六.创建实体类映射文件

<?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="com.helloworld.model.UserMapper">
    <!-- id 在session.select等方法中key使用, resultType 与Mybatis配置文件中的别名保持一致 -->
    <select id="getUserByID" parameterType="int" resultType="user">
        select
        * from user where id=#{0}
    </select>
</mapper>

注释中parameterType的表述不是很准确,进一步学习之后,再更新
七.创建测试类

/**
 * @author wuchao
 * @time 上午2:24:09
 * @description TODO
 */
package test;

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;

import com.helloworld.model.User;

/**
 * @author wuchao
 * @time 上午2:24:09
 *
 */
public class Test {

    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;
    static{
        try {
            //读取配置文件
            reader = Resources.getResourceAsReader("Mybatis-config.xml");
            //创建sessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            //打印错误信息
            e.printStackTrace();
        }
    }
    //创建获取session的方法  创建sessionFactory  session  
    public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }
    public static void main(String args[]){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            User user = new User(); 
            user.setId(new Integer(1));
            user = (User)session.selectOne("com.helloworld.model.UserMapper.getUserByID",user.getId() );
            System.out.println("用户名为:"+user.getName());
            if(user!=null){
                String userInfoString="名字:"+user.getName()+",所属部门:"+user.getDept()+",主页:"+user.getWebsite();
                System.out.println(userInfoString);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            session.close();
        }
    }
}

至此,一个mybatis入门级的项目实现
项目结构图以及测试结果
这里写图片描述

猜你喜欢

转载自blog.csdn.net/wuchao_codingforever/article/details/79811412