对mysql进行操作,创建数据库表
CREATE TABLE EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
PRIMARY KEY (id)
);
Employee对象
package com.zbm.pojo;
public class Employee {
private int id;
private String first_name;
private String last_name;
private int salary;
public Employee() {
}
public Employee(String fname, String lname, int salary) {
this.first_name = fname;
this.last_name = lname;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
数据库连接的SqlMapConfig.xml
<?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>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost:3306/health"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/zbm/pojo/Employee.xml"/>
</sqlMapConfig>
配置文件:Employee.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 namespace="employee">
<typeAlias alias="employee" type="com.zbm.pojo.Employee"/>
<insert id="insertEmployee" parameterClass="employee">
<selectKey keyProperty="id" resultClass="int">
select last_insert_id() as id
</selectKey>
insert into employee(first_name, last_name, salary) value
(#first_name#,#last_name#,#salary#)
</insert>
<select id="queryForAll" resultClass="Employee">
select * from employee
</select>
<update id="updateData" parameterClass="Employee">
update employee set first_name=#first_name#
where id=#id#
</update>
<delete id="deleteData" parameterClass="int">
delete from employee where id = #id#
</delete>
</sqlMap>
对数据库进行操作
package com.zbm.client;
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.zbm.pojo.Employee;
public class IbatisDao {
public static void main(String[] args) {
Reader rd = null;
try {
rd = Resources.getResourceAsReader("SqlMapConfig.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);
// insertEmployee(smc);
// updateFirstName(smc);
queayAll(smc);
// deleteData(smc);
}
public static void insertEmployee(SqlMapClient smc) {
try {
Employee employee = new Employee("Json", "Bright", 7000);
smc.insert("employee.insertEmployee", employee);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void queayAll(SqlMapClient smc) {
try {
@SuppressWarnings("unchecked")
List<Employee> emps = (List<Employee>)smc.queryForList("employee.queryForAll");
for(Employee emp : emps) {
System.out.println("firstName : " + emp.getFirst_name() + " , "
+ "lastName : " + emp.getLast_name() + " , "
+ "salary : " + emp.getSalary());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void updateFirstName(SqlMapClient smc) {
Employee rec = new Employee();
rec.setId(2);
rec.setFirst_name("File");
try {
smc.update("employee.updateData", rec);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void deleteData(SqlMapClient smc) {
int id = 3;
try {
int status = smc.delete("employee.deleteData", id);
System.out.println("status : " + status);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}