`
Xgw123485
  • 浏览: 84934 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Ibatis实例

阅读更多
<?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>

<!-- Configure a built-in transaction manager. If you're using an app server,
you probably want to use its transaction manager and a managed datasource -->
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:ESB" />
<property name="JDBC.Username" value="system" />
<property name="JDBC.Password" value="ngbss" />
</dataSource>
</transactionManager>

<!-- List the SQL Map XML files. They can be loaded from the classpath,
as they are here (com.domain.data...) -->
<sqlMap resource="User.xml" />
<!-- List more here... <sqlMap resource="com/mydomain/data/Order.xml"/>
<sqlMap resource="com/mydomain/data/Documents.xml"/> -->

</sqlMapConfig>



<?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="User">
<typeAlias alias="UserVo" type="com.huawei.vo.User" />

<resultMap class="UserVo" id="UserResult">
<result property="s_id" column="S_ID" />
<result property="c_id" column="C_ID" />
<result property="s_name" column="S_NAME" />

</resultMap>

<!--  select all User-->
<select id="selectAllUser" resultMap="UserResult">
select * from T_Stu
</select>

<!-- select one user by Id -->
<select id="selectUserById" parameterClass="String" resultClass="UserVo">
  select * from T_Stu where s_id=#s_id#
</select>

<!-- insert a new user -->
<insert id="insertNewUser" parameterClass="UserVo">
insert into T_Stu (s_id,c_id,s_name) values(#s_id#,#c_id#,#s_name#)
</insert>

<!-- update one user by id-->
<update id="updateUserInfo" parameterClass="UserVo">
update T_Stu set s_id=#s_id#,c_id=#c_id#,s_name=#s_name# where s_id=#s_id#
</update>

<delete id="deleteUserByid" parameterClass="String">
delete from T_Stu where s_id=#s_id#
</delete>

</sqlMap>
   


package com.huawei.vo;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class UserEaample
{
    private static SqlMapClient sqlMapper;
   
   public static void initSqlMap() throws IOException
   {
       Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
       sqlMapper=SqlMapClientBuilder.buildSqlMapClient(reader);
       reader.close();
   }

    public static List getAllUser() throws SQLException
    {
        return sqlMapper.queryForList("selectAllUser");
    }

    public static User getOneUserById(String s_id) throws SQLException
    {
        return (User)sqlMapper.queryForObject("selectUserById",s_id);
    }
    public static void addNewUser(User user) throws SQLException
    {
        sqlMapper.insert("insertNewUser", user);
    }
   
    public static void updateUserInfo(User user) throws SQLException
   
    {
        sqlMapper.update("updateUserInfo", user);
    }
   
    public static void deleteUserById(String s_id) throws SQLException
    {
        sqlMapper.delete("deleteUserByid", s_id);
    }
 
}


//////ibatis-2.3.0.677.jar
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics