iBatis

1.加入iBatis.jar和数据库驱动包

2.编写全局配置文件(数据库配置)sqlMapConfig.xml

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

<sqlMapConfig>
	<settings cacheModelsEnabled="true" enhancementEnabled="true"
		lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
		maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
			<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
			<property name="JDBC.ConnectionURL"
				value="jdbc:mysql://localhost:3306/cs" />
			<property name="JDBC.Username" value="root" />
			<property name="JDBC.Password" value="1234" />
		</dataSource>
	</transactionManager>
	<sqlMap resource="User.xml" />
</sqlMapConfig>

 3.编写一个POJO

package entity;

import java.io.Serializable;

@SuppressWarnings("serial")
public class User implements Serializable
{
    private String code;
    private String name;
    private String pwd;

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public String getName()
    {
        return name;
    }

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

    public String getPwd()
    {
        return pwd;
    }

    public void setPwd(String pwd)
    {
        this.pwd = pwd;
    }

}

 

4.编写POJO==SQL映射文件User.xml

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

<sqlMap namespace="User">
	<typeAlias alias="user" type="entity.User" />
	<select id="getUser" parameterClass="java.lang.String"
		resultClass="user">
		<![CDATA[
			select
			name,
			pwd
			from user
			where code = #code#
		]]>
	</select>
	<select id="pageList" resultClass="user">
		<![CDATA[
			select * from user
		]]>
	</select>
	<select id="pageList2" parameterClass="user" resultClass="user">
		select * from user
		<dynamic prepend="where">
			<isNotEmpty prepend="AND" property="name">
				(name like #name#)
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="pwd">
				(pwd like #pwd#)
			</isNotEmpty>
		</dynamic>
	</select>
</sqlMap>

 5.测试啦

         Reader reader = null;
        SqlMapClient sqlMapClient = null;
        try
        {
            reader = Resources.getResourceAsReader("sqlMapConfig.xml");
            sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
            User user = new User();
            user.setName("%g%");
            user.setPwd("%1%");
            List list2 = sqlMapClient.queryForList("pageList2", user);
            for(Object object : list2)
            {
                User u = (User) object;
                System.out.println(u.getName());
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

 

你可能感兴趣的:(sql,mysql,xml,ibatis,jdbc)