学习来源: http://developer.51cto.com/art/200907/138286.htm
http://www.cnblogs.com/youxiuxiaocai/p/3386397.html
仍然以文件的思路展开对SSI项目开发的学习。
1. 实体类:Acount.java
package example; public class Account { private String username; private String password; public String getUsername(){ return username; } public void setUsername(String username){ this.username=username; } public String getPassword(){ return password; } public void setPassword(String password){ this.password=password; } }
2. 对实体类的操作配置文件:Acount.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="Account"﹥ ﹤select id="getAccount" parameterClass="java.lang.String" resultClass="example.Account"﹥ select * from Account where username = #value# <!-- #value# 是将要传进去的参数 --> ﹤/select﹥ ﹤insert id="createAccount" parameterClass="example.Account"﹥ insert into Account (username, password) values ( #username#, #password# ) ﹤/insert﹥ ﹤/sqlMap﹥
3. 配置数据库连接: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" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false" /﹥ ﹤transactionManager type="JDBC" ﹥ ﹤dataSource type="SIMPLE"﹥ ﹤property name="JDBC.Driver" value="sun.jdbc.odbc.JdbcOdbcDriver"/﹥ ﹤property name="JDBC.ConnectionURL" value="jdbc:odbc:Blog"/﹥ ﹤property name="JDBC.Username" value="admin"/﹥ ﹤property name="JDBC.Password" value=""/﹥ ﹤/dataSource﹥ ﹤/transactionManager﹥ ﹤sqlMap resource="Account.xml" /﹥ ﹤/sqlMapConfig﹥
4. 测试
com.ibatis.sqlmap.client.SqlMapClient sqlMap = null; try { java.io.Reader reader = om.ibatis.common.resources.Resources.getResourceAsReader ("sql-map-config.xml"); //初始化iBatis,获取一个SqlMapClient对象,即此处的sqlMap sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); } catch (Exception e) { e.printStackTrace(); }
//通过SqlMapClient对象,对数据库进行操作 try { Account account = new Account(); account.setUsername("micheal"); account.setPassword("1234"); //增 sqlMap.insert("createAccount", account); //查 Account accout = (Account)sqlMap.queryForObject("getAccount", "admin"); }
时间: 2024-10-12 17:10:54