ibatis入门尝试3-namespace命名空间 parameterMap Like模糊查询 批量及事物处理

1 namespace 命名空间

为了测试命名空间 在创建表 u1(id name email) u2(id name email)
分别创建 实体Bean和映射文件
User.java
package com.bean;

public class User {
	private Integer id;
	private String name;
	private String email;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	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;
	}
}


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 namespace="user">
<typeAlias alias="userVO" type="com.bean.User"/>
<insert id="createUser" parameterClass="userVO">
insert into u1(name,email)
values(#name#,#email#)
</insert>
</sqlMap>



User2.java
package com.bean;

public class User2 {
	private Integer id;
	private String name;
	private String email;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	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;
	}
}


User2.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="user2">
<typeAlias alias="user2VO" type="com.bean.User2"/>
<insert id="createUser" parameterClass="user2VO">
insert into u2(name,email)
values(#name#,#email#)
</insert>
</sqlMap>


ibatis 配置文件
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
	<settings
		cacheModelsEnabled="true"
		enhancementEnabled="true"
		lazyLoadingEnabled="true"
		maxRequests="32"
		maxSessions="10"
		maxTransactions="5"
		useStatementNamespaces="false"
	/>
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
			<property name="JDBC.Driver"
				value="com.mysql.jdbc.Driver" />
			<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/ibatis_db?characterEncoding=UTF-8" />
			<property name="JDBC.Username" value="root" />
			<property name="JDBC.Password" value="" />
		</dataSource>
	 </transactionManager>
	<sqlMap resource="com/bean/User.xml" />
 
	<sqlMap resource="com/bean/User2.xml"/>	

</sqlMapConfig>

测试方法
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
User u =new User();
u.setName("aa");
u.setEmail("[email protected]");
sqlMap.insert("createUser", u);

如果此时执行 会报异常 原因很明显
There is already a statement named createUser in this SqlMap.
就是因为在加载的实体映射文件中 都含有 id = "createUser"
这里去掉已经 <sqlMap resource="com/bean/User2.xml"/>就可以
这也就是为什么我们要引入namespace的原因
我们的映射文件中已经设置了namespace
<sqlMap namespace="user">
实际ibatis的默认是不开启的namespace的
此时我们将 配置文件中的useStatementNamespaces="false" 改为true
我们在测试一下,不过此时的调用方式 需要 namespace.id_name
sqlMap.insert("user.createUser", u);
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
User u =new User();
u.setName("aa");
u.setEmail("[email protected]");
sqlMap.insert("user.createUser", u);


这里需要注意  typeAlias 标签不受命名空间影响 只对应当前的实体映射页

2 parameterMap传入参数映射
之前我们已经用到过rseultMap映射,现在我们来看一下 parameterMap
我们修改User.xml
	<parameterMap class="userVO" id="userVOpmp">
	<!-- 
	这里可以对java的类进行映射 也可以映射jdbcType 
	如果jdbcType 要注意是Date(只保存日期) 区别 TimeSTAMP(日期+时间)
	-->
		<parameter property="name" jdbcType="varchar"/>
		<parameter property="email" jdbcType="varchar"/>
	</parameterMap>
	<insert id="createUser" parameterMap="userVOpmp">
	insert into u1(name,email)
	values(?,?)
	</insert>

Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
User u =new User();
u.setName("www.0431.la");
u.setEmail("[email protected]");
sqlMap.insert("user.createUser",u);

需要注意
类型必须一致 保证语句的参数顺序正确

3 Like 模糊查询
这个比价简单 和正常查询几乎没有什么变化 主不过标识符由#换成$
<select id="selectLikeName" parameterClass="java.lang.String" resultClass="userVO">
select * from u1 where name like '%$value$%'
</select>

Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
List list = sqlMap.queryForList("user.selectLikeName", "a");
System.out.println(list.size());


4 批量及事物处理
这里的设置不需要更改xml
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
//开启事物	
sqlMap.startTransaction();
//开启批次操作
sqlMap.startBatch();
User user = new User();
user.setName("aaaaa");
User2 user2 = new User2();
user2.setName("bbbbb");
sqlMap.insert("user.createUser",user);
sqlMap.insert("user2.createUser",user2);
//执行批次操作
sqlMap.executeBatch();
//提交事物
sqlMap.commitTransaction();

你可能感兴趣的:(java,sql,xml,bean,ibatis)