基于ibatis-2.3.0.677版本。
1、创建新的项目,并引入jar包
a) ibatis-2.3.0.677.jar
b) mysql驱动
2、在类路径中提供ibatis的配置文件: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>
- <transactionManager type="JDBC" commitRequired="false">
- <dataSource type="SIMPLE">
- <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
- <property name="JDBC.ConnectionURL" value="jdbc:mysql://127.0.0.1/crm"/>
- <property name="JDBC.Username" value="root"/>
- <property name="JDBC.Password" value="mysql"/>
- </dataSource>
- </transactionManager>
- <sqlMap resource="com/bjsxt/crm/model/User.xml"/>
- </sqlMapConfig>
<?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>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://127.0.0.1/crm"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="mysql"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/bjsxt/crm/model/User.xml"/>
</sqlMapConfig>
3、创建实体类:User.java
- package com.app.model;
- public class User {
- private int id;
- private String username;
- private String password;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- }
package com.app.model;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
4、创建针对User对象的CRUD的xml映射配置:User.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>
- <!-- Use type aliases to avoid typing the full classname every time. -->
- <typeAlias alias="User" type="com.app.model.User"/>
- <!-- Select with no parameters using the result map for Account class. -->
- <select id="selectAllUsers" resultClass="User">
- select * from t_user
- </select>
- <select id="selectUser" resultClass="User" parameterClass="int">
- select * from t_user where id=#id#
- </select>
- <insert id="insertUser" parameterClass="User">
- insert into t_user values (
- null,#username#,#password#
- )
- </insert>
- <update id="updateUser" parameterClass="User">
- update t_user set username = #username#,password=#password#
- where id=#id#
- </update>
- <delete id="deleteUser" parameterClass="int">
- delete from t_user where id=#id#
- </delete>
- </sqlMap>
<?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>
<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="User" type="com.app.model.User"/>
<!-- Select with no parameters using the result map for Account class. -->
<select id="selectAllUsers" resultClass="User">
select * from t_user
</select>
<select id="selectUser" resultClass="User" parameterClass="int">
select * from t_user where id=#id#
</select>
<insert id="insertUser" parameterClass="User">
insert into t_user values (
null,#username#,#password#
)
</insert>
<update id="updateUser" parameterClass="User">
update t_user set username = #username#,password=#password#
where id=#id#
</update>
<delete id="deleteUser" parameterClass="int">
delete from t_user where id=#id#
</delete>
</sqlMap>
5、创建测试程序测试:
- package com.app.model;
- import java.io.Reader;
- import java.util.Iterator;
- import java.util.List;
- import com.ibatis.common.resources.Resources;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- /**
- * 最简单的形式!
- * @author Lee
- *
- */
- public class UserTest {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception{
- //从配置文件中得到SqlMapClient对象
- Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
- SqlMapClient sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
- reader.close();
- //创建用户数据
- for(int i=0; i<10; i++){
- User user = new User();
- user.setUsername("用户"+i);
- user.setPassword("密码"+i);
- sqlMapper.insert("insertUser", user);
- }
- //查询用户数据
- List users = sqlMapper.queryForList("selectAllUsers");
- for (Iterator iter = users.iterator(); iter.hasNext();) {
- User user = (User) iter.next();
- System.out.println(user.getUsername());
- }
- //查询特定用户的数据
- User user = (User)sqlMapper.queryForObject("selectUser", 1);
- System.out.println("用户【id="+1+"】的名称是:"+user.getUsername());
- //更新用户的信息
- user = new User();
- user.setId(3);
- user.setUsername("更改之后的用户名称");
- user.setPassword("密码被更改");
- sqlMapper.update("updateUser", user);
- //删除用户的信息
- sqlMapper.delete("deleteUser", 6);
- }
- }