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 :
- <?xmlversion="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>
- <!-- 引用JDBC属性的配置文件 -->
- <propertiesresource="com/iflytek/entity/SqlMap.properties"/>
- <!-- 使用JDBC的事务管理 -->
- <transactionManagertype="JDBC">
- <!-- 数据源 -->
- <dataSourcetype="SIMPLE">
- <propertyname="JDBC.Driver"value="${driver}"/>
- <propertyname="JDBC.ConnectionURL"value="${url}"/>
- <propertyname="JDBC.Username"value="${username}"/>
- <propertyname="JDBC.Password"value="${password}"/>
- </dataSource>
- </transactionManager>
- <!-- 这里可以写多个实体的映射文件 -->
- <sqlMapresource="com/iflytek/entity/Student.xml"/>
- </sqlMapConfig>
StudentDao :
- package com.iflytek.dao;
- import java.util.List;
- import com.iflytek.entity.Student;
- /**
- * @author xudongwang 2011-12-31
- *
- * Email:xdwangiflytek@gmail.com
- *
- */
- publicinterface StudentDao {
- /**
- * 添加学生信息
- *
- * @param student
- * 学生实体
- * @return 返回是否添加成功
- */
- publicboolean addStudent(Student student);
- /**
- * 根据学生id删除学生信息
- *
- * @param id
- * 学生id
- * @return 删除是否成功
- */
- publicboolean deleteStudentById(int id);
- /**
- * 更新学生信息
- *
- * @param student
- * 学生实体
- * @return 更新是否成功
- */
- publicboolean updateStudent(Student student);
- /**
- * 查询全部学生信息
- *
- * @return 返回学生列表
- */
- public List<Student> selectAllStudent();
- /**
- * 根据学生姓名模糊查询学生信息
- *
- * @param name
- * 学生姓名
- * @return 学生信息列表
- */
- public List<Student> selectStudentByName(String name);
- /**
- * 根据学生id查询学生信息
- *
- * @param id
- * 学生id
- * @return 学生对象
- */
- public Student selectStudentById(int id);
- }
StudentDaoImpl :
- package com.iflytek.daoimpl;
- 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;
- import com.iflytek.dao.StudentDao;
- import com.iflytek.entity.Student;
- /**
- * @author xudongwang 2011-12-31
- *
- * Email:xdwangiflytek@gmail.com
- *
- */
- publicclass StudentDaoImpl implements StudentDao {
- privatestatic SqlMapClient sqlMapClient = null;
- // 读取配置文件
- static {
- try {
- Reader reader = Resources
- .getResourceAsReader("com/iflytek/entity/SqlMapConfig.xml");
- sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- publicboolean addStudent(Student student) {
- Object object = null;
- boolean flag = false;
- try {
- object = sqlMapClient.insert("addStudent", student);
- System.out.println("添加学生信息的返回值:" + object);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- if (object != null) {
- flag = true;
- }
- return flag;
- }
- publicboolean deleteStudentById(int id) {
- boolean flag = false;
- Object object = null;
- try {
- object = sqlMapClient.delete("deleteStudentById", id);
- System.out.println("删除学生信息的返回值:" + object + ",这里返回的是影响的行数");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- if (object != null) {
- flag = true;
- }
- return flag;
- }
- publicboolean updateStudent(Student student) {
- boolean flag = false;
- Object object = false;
- try {
- object = sqlMapClient.update("updateStudent", student);
- System.out.println("更新学生信息的返回值:" + object + ",返回影响的行数");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- if (object != null) {
- flag = true;
- }
- return flag;
- }
- public List<Student> selectAllStudent() {
- List<Student> students = null;
- try {
- students = sqlMapClient.queryForList("selectAllStudent");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return students;
- }
- public List<Student> selectStudentByName(String name) {
- List<Student> students = null;
- try {
- students = sqlMapClient.queryForList("selectStudentByName",name);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return students;
- }
- public Student selectStudentById(int id) {
- Student student = null;
- try {
- student = (Student) sqlMapClient.queryForObject(
- "selectStudentById", id);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return student;
- }
- }
TestIbatis.java :
- package com.iflytek.test;
- import java.sql.Date;
- import java.util.List;
- import com.iflytek.daoimpl.StudentDaoImpl;
- import com.iflytek.entity.Student;
- /**
- * @author xudongwang 2011-12-31
- *
- * Email:xdwangiflytek@gmail.com
- *
- */
- publicclass TestIbatis {
- publicstaticvoid main(String[] args) {
- StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
- System.out.println("测试插入");
- Student addStudent = new Student();
- addStudent.setName("李四");
- addStudent.setBirth(Date.valueOf("2011-09-02"));
- addStudent.setScore(88);
- System.out.println(studentDaoImpl.addStudent(addStudent));
- System.out.println("测试根据id查询");
- System.out.println(studentDaoImpl.selectStudentById(1));
- System.out.println("测试模糊查询");
- List<Student> mohuLists = studentDaoImpl.selectStudentByName("李");
- for (Student student : mohuLists) {
- System.out.println(student);
- }
- System.out.println("测试查询所有");
- List<Student> students = studentDaoImpl.selectAllStudent();
- for (Student student : students) {
- System.out.println(student);
- }
- System.out.println("根据id删除学生信息");
- System.out.println(studentDaoImpl.deleteStudentById(1));
- System.out.println("测试更新学生信息");
- Student updateStudent = new Student();
- updateStudent.setId(1);
- updateStudent.setName("李四1");
- updateStudent.setBirth(Date.valueOf("2011-08-07"));
- updateStudent.setScore(21);
- System.out.println(studentDaoImpl.updateStudent(updateStudent));
- }