标签:
ibatis Mybatis 官网:http://blog.mybatis.org/
ibatis的自动生成插件:ibator
搭建iBatis 开发环境:
1 、导入相关的jar 包,ibatis-2.3.0.677.jar 、mysql-connector-java-5.1.6-bin.jar
2 、编写配置文件:Jdbc 连接的属性文件 ;总配置文件, SqlMapConfig.xml 关于每个实体的映射文件(Map 文件)
Demo :
model层
package com.ccssoft.model;
import java.io.Serializable; import java.util.Date;
@SuppressWarnings("unused") public class User implements Serializable { private Integer userID; private String accountNo; private String password; private String name; private String phone; private String email;
public Integer getUserID() { return userID; } public void setUserID(Integer userID) { this.userID = userID; } public String getAccountNo() { return accountNo; } public void setAccountNo(String accountNo) { this.accountNo = accountNo; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String toString(){ return "phone ="+phone+";"; } }
SqlMap.properties :
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:dream
jdbc.username=dream
jdbc.password=marconi
User_Map.xml:
<?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> <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 --> <typeAlias alias="User" type="com.ccssoft.demo.entity.User" /> <resultMap id="userMap" class="User"> <result property="userID" column="USERID"/> <result property="accountNo" column="ACCOUNTNO"/> <result property="password" column="password"/> <result property="name" column="name"/> <result property="email" column="email"/> <result property="phone" column="phone"/> </resultMap> <!-- 用户添加 -->
<insert id="addUser" parameterClass="User"> insert into t_user( USERID,ACCOUNTNO,password, name,email,phone ) values( id_seq.nextval,#accountNo#,#password#, #name#,#email#,#phone# ) </insert> <!-- 删除用户 --> <delete id="deleteUserById" parameterClass="Integer"> delete from t_user where USERID=#userID# </delete> <!-- 修改用户信息 --> <update id="updateUser" parameterClass="User"> update t_user set name=#name#,email=#email#,phone=#phone#,password=#password# where USERID=#userID# </update>
<!-- 查询 --> <select id="selectAllUser" parameterClass="HashMap" resultClass="User"> select t.USERID,t.ACCOUNTNO,t.password, t.name,t.email,t.phone from (select t.*,ROWNUM RN from t_user t where 1=1 <isNotEmpty property="accountNo"> and ACCOUNTNO like ‘%$accountNo$%‘ </isNotEmpty> <isNotEmpty property="name"> and name like ‘%$name$%‘ </isNotEmpty> ) t where RN>#start# and RN<=#end#
</select>
<select id="selectUserById" parameterClass="Integer" resultClass="User"> select * from t_user where USERID=#userID# </select>
<select id="selectUsersByName" parameterClass="HashMap" resultClass="HashMap">
select t.USERID,t.ACCOUNTNO,t.password, t.name,t.email,t.phone from (select t.*,ROWNUM RN from t_user t) t where t.name like ‘%$name$%‘ and RN>#start# and RN<=#end# </select> <select id="selectUsersByAccountNo" parameterClass="HashMap" resultClass="HashMap"> select t.USERID,t.ACCOUNTNO,t.password, t.name,t.email,t.phone from (select t.*,ROWNUM RN from t_user t) t where RN>#start# and RN<=#end# and t.ACCOUNTNO like ‘%$accountNo$%‘ </select> <select id="selectUsersByName_No" parameterClass="HashMap" resultClass="HashMap"> select t.USERID,t.ACCOUNTNO,t.password, t.name,t.email,t.phone from (select t.*,ROWNUM RN from t_user t) t where RN>#start# and RN<=#end# and (t.name like ‘%$name$%‘ or ACCOUNTNO like ‘%$accountNo$%‘)
</select> <select id="selectUserByNo" parameterClass="String" resultClass="User"> select * from t_user where ACCOUNTNO=#accountNo# </select> <select id="selectPwdByNo" parameterClass="String" resultClass="String"> select password from t_user where ACCOUNTNO=#accountNo# </select>
<select id="selectUserCount" parameterClass="User" resultClass="Integer"> select count(*) from t_user where 1=1 <isNotEmpty property="accountNo"> and ACCOUNTNO like ‘%$accountNo$%‘ </isNotEmpty> <isNotEmpty property="name"> and name like ‘%$name$%‘ </isNotEmpty> </select>
</sqlMap>
说明:
如果xml 中没有ibatis 的提示,则window --> Preference--> XML-->XML Catalog---> 点击add
选择uri URI: 请选择本地文件系统上
iBatisDemo1/WebContent/WEB-INF/lib/sql-map-config-2.dtd 文件;
Key Type: 选择Schema Location;
Key: 需要联网的,不建议使用;
SqlMapConfig.xml :
StudentDao :
StudentDaoImpl :
TestIbatis.java :
标签:
原文地址:http://www.cnblogs.com/jwblogmj/p/4413926.html