如下6个文件:
User.java
UserTest.java
SqlMapConfig.xml
User.xml
log4j.properties
t_user.sql
1.首先看看目录结构:
2.t_user.sql
drop table if exists t_user;
CREATE TABLE t_user (
id int ( 11 ) NOT NULL auto_increment,
name varchar ( 50 ) default NULL ,
sex int ( 11 ) default NULL ,
PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
/* 数据表 `t_user` 的数据 */
insert into t_user values
( 1 , 'jinzy' , 1 ),( 2 , 'jinzy' , 2 ),( 3 , '3' , 3 ),( 4 , '4' , 4 ),( 5 , '5' , 5 );
3.log4j.properties
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.logger.com.codefutures=DEBUG
4.User.java
package com.jinzy;
public class User {
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
private String sex;
/*public User(String name, String sex) {
this.name = name;
this.sex = sex;
}*/
}
5.UserTest.java
package com.jinzy;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class UserTest {
/**
* @param args
* @throws SQLException
* @throws IOException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Reader reader = Resources
.getResourceAsReader("com/jinzy/maps/sqlMapConfig.xml");
SqlMapClient sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
List users = sqlMapper.queryForList("selectAllUsers");
for (int i = 0; i < users.size(); i++) {
Map<String, Object> ma = (HashMap<String, Object>) users.get(i);
System.out.print(ma.get("id"));
System.out.print("-");
System.out.println(ma.get("name"));
}
User user = new User();
user.setId(3);
user.setName("002");
sqlMapper.update("updateUser", user);
}
}
6.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:3306/test"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="111111"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/jinzy/maps/User.xml"/>
</sqlMapConfig>
7.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>
<typeAlias alias="user" type="com.jinzy.User"/>
<select id="selectAllUsers" resultClass="java.util.HashMap">
select * from t_user
</select>
<select id="selectUser" resultClass="User" parameterClass="int">
select * from t_user where id=#id#
</select>
<update id="updateUser" parameterClass="User">
update t_user set name = #name#,sex=#sex#
where id=#id#
</update>
<select id="searchProductList" resultClass="user" parameterClass="java.util.ArrayList">
select * from t_user
<dynamic prepend="WHERE">
<iterate open="(" close=")" conjunction="OR">
name=#keywordList[]#
</iterate>
</dynamic>
</select>
<select id="searchProductList1" resultClass="user" parameterClass="java.util.ArrayList">
select * from t_user
<dynamic prepend="WHERE">
<iterate open="(" close=")" conjunction="OR">
name=#keywordList[].name# AND
sex=#keywordList[].sex#
</iterate>
</dynamic>
</select>
</sqlMap>