mybatis--第一个mybatis程序

首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下:

create database my;

use my;
create table user(
id int(10) auto_increment,
name varchar(64),
level varchar(256),
phone varchar(256),
primary key(id)
);

insert into user(id,name,level,phone) values(1,'a','a','1234555666');

 

其次,创建mybatis配置文件SqlMapConfig.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="com.zk.pojo.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/my"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
        </dataSource>
        </environment>
    </environments>

    <mappers>
         <!-- // power by http://www.yiibai.com -->
         <mapper resource="com/zk/pojo/UserMapper.xml" />
    </mappers>
</configuration>

再次,创建实体类user.java和映射文件UserMapper.xml

user.java

package com.zk.pojo;

public class user {
    private Integer id;
    
    private String name;
    
    private String level;
    
    private String phone;

    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 getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getPhone() {
        return phone;
    }

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

    @Override
    public String toString() {
        return "user [id=" + id + ", name=" + name + ", level=" + level
                + ", phone=" + phone + "]";
    }
}

UserMapper.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="com.zk.pojo.UserMapper">
	<select id="GetUserByID" parameterType="int" resultType="user">
		select * from user where id = #{id}
    </select>
</mapper>

最后,写出实现类Helloword.java

package com.zk.pojo;

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.zk.pojo.*;

/**
 * 
 * @author yiibai
 * @copyright http://www.yiibai.com
 * @date 2015/09/22
 */
public class HelloWord {
	private static SqlSessionFactory sqlSessionFactory;
	private static Reader reader;

	static {
		try {
			reader = Resources.getResourceAsReader("SqlMapConfig.xml");
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static SqlSessionFactory getSession() {
		return sqlSessionFactory;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SqlSession session = sqlSessionFactory.openSession();
		try {
			user user = (user) session.selectOne("com.zk.pojo.UserMapper.GetUserByID", 1);
			if(user!=null){
				String userInfo =user.getName()+", 所属部门:"+user.getLevel()+", 手机号:"+user.getPhone();
				System.out.println(userInfo);
			}
		} finally {
			session.close();
		}
	}

}

运行Helloword.java

 

转载于:https://www.cnblogs.com/longlyseul/p/11210829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值