Eclipse 3.72
工程目录图:

table : tes
coulme: id(int),name(string),factory(string),date(Date)
实体类:Tes.java
sql语句的配置文件tes.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">
<!-- 表的命名空间,和sql语句配置,注意类型为对应的实体类 -->
<sqlMap namespace="Tes">
<typeAlias alias="tes" type="com.iba.Tes"/>
<select id="getTes" parameterClass="java.lang.String" resultClass="tes">
select id,name,factory,date from tes where name=#name#
</select>
<update id="updateTes" parameterClass="tes">
update tes set name=#name#,factory=#factory#,date=#date# where id=#id#
</update>
<insert id="insertTes" parameterClass="tes">
INSERT INTO tes (id, name,factory,date) VALUES (#id#, #name#, #factory#,#date#)
</insert>
<delete id="deletetes" parameterClass="java.lang.String">
delete from tes where id = #value#
</delete>
</sqlMap>
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
<!-- Ibatis 的功能属性配置-->
cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<!-- JDBC驱动 -->
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<!-- 数据库URL -->
<property name="JDBC.ConnectionURL" value="jdbc:mysql://172.16.52.107:3306/ioffice52" />
<!-- 数据库用户名 -->
<property name="JDBC.Username" value="root" />
<!-- 数据库密码 -->
<property name="JDBC.Password" value="root" />
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingQuery" value="select 1 from ACCOUNT" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>
<!-- 指定映射文件的位置,配置中可出现多个sqlMap节点,以指定项目内所有映射文件 -->
<sqlMap resource="com/iba/Ibatis.xml" />
<sqlMap resource="com/iba/tes.xml" />
</sqlMapConfig> 测试类TestMain.java
package com.test;
import java.io.IOException;
import java.io.Reader;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;
import com.iba.Ibatis;
import com.iba.Tes;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class TestMain {
public static void main(String[] args) throws IOException, SQLException {
// insIbatis();
// insTes();
// getTes();
delTes();
// updTes();
System.out.println("DataBass option is success!");
}
public static SqlMapClient aa() throws IOException, SQLException
{
String resource ="SqlMapConfig.xml";
Reader reader;
reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
sqlMap.startTransaction();
return sqlMap;
}
public static void insIbatis() throws IOException, SQLException{
SqlMapClient sqlMap=aa();
Ibatis ibatis = new Ibatis();
ibatis.setId(new Integer(2));
ibatis.setName("Tom");
ibatis.setAge(new Integer(22));
sqlMap.insert("insertIbatis",ibatis);
sqlMap.commitTransaction();
}
public static void insTes() throws IOException, SQLException{
SqlMapClient sqlMap=aa();
Tes ibatis = new Tes();
ibatis.setId(new Integer(55));
ibatis.setName("Tom");
ibatis.setFactory("factory");
sqlMap.insert("insertTes",ibatis);
sqlMap.commitTransaction();
}
public static void getTes() throws IOException, SQLException{
SqlMapClient sqlMap=aa();
List<Tes> tes = (List<Tes>)sqlMap.queryForList("getTes", "Tom");
System.out.println(tes.toString());
sqlMap.commitTransaction();
}
public static void delTes() throws IOException, SQLException{
SqlMapClient sqlMap=aa();
Tes tes = new Tes();
sqlMap.delete("deletetes","1");
sqlMap.commitTransaction();
}
public static void updTes() throws IOException, SQLException{
SqlMapClient sqlMap=aa();
Tes tes = new Tes();
tes.setDate(new Date(System.currentTimeMillis()));
tes.setFactory("upde_tes");
tes.setName("shuohaoln e ");
tes.setId(1);
sqlMap.update("updateTes", tes);
sqlMap.commitTransaction();
}
}
本文介绍了MyBatis的基本配置及使用方法,包括实体类、SQL语句配置、连接池设置等关键步骤,并通过示例展示了如何进行增删改查操作。
1183

被折叠的 条评论
为什么被折叠?



