swing整合ibatis

最近项目要用到JAVA开发一款桌面程序,不想自己去写ORM,直接用IBATIS,所以研究了一下把log4j、ibtatis、swing三者整合在了一起,做了一个DEMO;

开发所需:myeclipse、ibatis.jar、log4j.jar、mysql.jar;

所使用的数据库为mysql的,大家可以自己去更改数据库连接符;

项目结构如下:


swing整合ibatis

代码如下:

package com.edw.bean;

/**
 * @Title: Contoh.java
 * @Description:
 * @author: lijiasheng
 * @date: 2014-5-16 上午8:31:01
 * @see
 * @Copyright: 2012 www.odbpo.com Inc. All rights reserved
 * @version: V1.0
 */

public class Contoh
{
    private String nama;

    private String alamat;

    public String getNama()
    {
        return nama;
    }

    public void setNama(String nama)
    {
        this.nama = nama;
    }

    public String getAlamat()
    {
        return alamat;
    }

    public void setAlamat(String alamat)
    {
        this.alamat = alamat;
    }
}

 

package com.edw.config;
 
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
 
import java.io.IOException;
import java.io.Reader;
 
public class SqlMapConfig {
 
    protected static final SqlMapClient sqlMap;
 
    static {
        try {
            Reader reader = Resources.getResourceAsReader("com/edw/sqlmap/sqlmapconfig.xml");
            sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        } catch (IOException e) {
            throw new RuntimeException("Fatal Error, ga dapet sqlmapconfignya.  Cause: " + e, e);
        } catch (Exception e){
            throw new RuntimeException("Fatal Error.  Cause: " + e, e);
        }
    }
 
    public static SqlMapClient getSqlMap() {
        return sqlMap;
    }
}

 
 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="contoh">

	<insert id="insertContoh" parameterClass="com.edw.bean.Contoh">
		insert into contoh (nama, alamat)
		values (#nama:VARCHAR#, #alamat:VARCHAR#)
	</insert>
	
	<select id="selectCheck" parameterClass="com.edw.bean.Contoh" resultClass="com.edw.bean.Contoh">
		select * from contoh where nama=#nama:VARCHAR#
	</select>
</sqlMap>

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<settings useStatementNamespaces="true" lazyLoadingEnabled="true"
		enhancementEnabled="true" maxSessions="20" />

	<transactionManager type="JDBC" commitRequired="false">
		<dataSource type="SIMPLE">

			<property name="SetAutoCommitAllowed" value="false" />
			<property name="DefaultAutoCommit" value="false" />

			<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
			<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/test" />
			<property name="JDBC.Username" value="root" />
			<property name="JDBC.Password" value="820913" />

		</dataSource>
	</transactionManager>

	<!-- dont forget to register your sql map configs -->
	<sqlMap resource="com/edw/sqlmap/contoh.xml" />
	
</sqlMapConfig>

 

package com.edw.ui;

import com.edw.bean.Contoh;
import com.edw.config.SqlMapConfig;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FrameUtama extends JFrame implements ActionListener
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JTextField txtNama = new JTextField();

    private JTextField txtAlamat = new JTextField();

    private JButton cmdButton = new JButton("Save");

    public FrameUtama()
    {
        setLayout(new GridLayout(3, 3));
        Container con = this.getContentPane();
        con.add(new JLabel("nama : "));
        con.add(txtNama);
        con.add(new JLabel("Alamat : "));
        con.add(txtAlamat);
        con.add(cmdButton);

        cmdButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == cmdButton)
        {
            SqlMapClient sqlMapClient = SqlMapConfig.getSqlMap();
            
            Contoh c=new Contoh();
            c.setNama("ddd");
            
            try
            {
                @SuppressWarnings("unchecked")
                List<Contoh> list=sqlMapClient.queryForList("contoh.selectCheck", c);
                for (Contoh contoh : list)
                {
                    System.out.println(contoh.getNama());
                }
                System.out.println(list.size());
            }
            catch (SQLException e1)
            {
                e1.printStackTrace();
            }
            
            Contoh contoh = new Contoh();
            contoh.setNama(txtNama.getText());
            contoh.setAlamat(txtAlamat.getText());

            
            try
            {
                sqlMapClient.insert("contoh.insertContoh", contoh);
                System.out.println("Success");
            }
            catch (SQLException ex)
            {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] edw)
    {
        FrameUtama frameUtama = new FrameUtama();
        frameUtama.setVisible(true);
        frameUtama.setSize(300, 150);
        frameUtama.setLocationRelativeTo(null);
    }

}

 

log4j.rootCategory=DEBUG, stdout , R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=/d\:/logs/CheckTool.log
log4j.appender.R.MaxFileSize=500000KB
log4j.appender.R.MaxBackupIndex=20
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %5p (%c:%L) - %m%n

log4j.logger.com.odbpo=DEBUG
log4j.logger.com.odbpo.common=DEBUG
log4j.logger.org.mybatis=DEBUG
log4j.logger.com.opensymphony=DEBUG
log4j.logger.com.opensymphony.xwork2=DEBUG
log4j.logger.org.springframework=DEBUG
log4j.logger.org.apache.struts2  = DEBUG
log4j.logger.org.apache.commons  = DEBUG
log4j.logger.org.apache.ibatis=DEBUG
log4j.logger.java.sql.Connection = DEBUG
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG
log4j.logger.java.sql.ResultSet = DEBUG

以上是项目的全部代码;附件为源码,经测试可以使用,正式生产环境的时候请把log4j配置文件里的debug替换成info

你可能感兴趣的:(swing整合ibatis)