首页 > 代码库 > iBatis:第一步,思路学习
iBatis:第一步,思路学习
学习来源: 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="http://www.mamicode.com/sun.jdbc.odbc.JdbcOdbcDriver"/﹥
﹤property name="JDBC.ConnectionURL" value="http://www.mamicode.com/jdbc:odbc:Blog"/﹥
﹤property name="JDBC.Username" value="http://www.mamicode.com/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"); }
iBatis:第一步,思路学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。