HelloWorld之MyBatis

系统 1871 0

项目的目录结构:


很简单。Account是要组装的Domain对象,AccountDAO是测试类,
mapper/Account.xml保存了两条Account的查询语句,而mybatis.cfg.xml
是数据库连接的配置。
    package com.cdai.orm.mybatis;

public class Account {

	private long id;
	
	private long balance;
	
	public Account() {
	}

	public long getId() {
		return id;
	}

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

	public long getBalance() {
		return balance;
	}

	public void setBalance(long balance) {
		this.balance = balance;
	}

	@Override
	public String toString() {
		return "Account [id=" + id + ", balance=" + balance + "]";
	}
	
}
  

    <?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="Account" type="com.cdai.orm.mybatis.Account" />
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
                <property name="url" value="jdbc:sqlserver://192.168.1.102:1433;databaseName=Bank" />
                <property name="username" value="sa" />
                <property name="password" value="1qaz2wsx" />
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="mapper/Account.xml" />
    </mappers>
</configuration>
  

    <?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="Account">
	<select id="selectAccount" parameterType="int" resultType="Account">
    	<![CDATA[
        	SELECT col_id AS id, col_balance AS balance 
        	FROM tb_account
    	]]>
    </select>
    <select id="selectAccountById" parameterType="int" resultType="Account">
    	<![CDATA[
        	SELECT col_id AS id, col_balance AS balance FROM tb_account WHERE col_id = #{id}
    	]]>
    </select>
</mapper>
  

    package com.cdai.orm.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 AccountDAO {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {

		String resource = "mybatis.cfg.xml";
		try {
			Reader reader = Resources.getResourceAsReader(resource);
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
			SqlSession session = sessionFactory.openSession();
			
			List<Account> accounts = (List<Account>) session.selectList("selectAccount");
			for (Account account : accounts) {
				System.out.println(account);
			}
			
			Account account = (Account) session.selectOne("selectAccountById", 2);
			System.out.println(account);
			
			session.close();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
  

HelloWorld之MyBatis


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论