日期:2014-05-16  浏览次数:20478 次

iBATIS3.0学习(二)使用iBATIS3.0注解完成对数据库的简单操作

?使用iBATIS3.0注解完成对数据库的简单操作

??? iBATIS3.0也增加了一些简单的注解,iBATIS3的注解只能完成一些简单操作,要进行更复杂的操作,最好是在XML文件中配置。

??? 在数据库(本人使用的mysql)中建立一个person表:

DROP TABLE IF EXISTS `test`.`person`;
CREATE TABLE  `test`.`person` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(20) default NULL,
  `sex` varchar(8) default NULL,
  `birthday` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

?

??? 在MyEclipse新建一个Java Project,结构如下图



?

??? 在jdbc.properties文件是映射文件要使用的,其内容如下:

?

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/test
username=root
password=123456

?SqlMapper.xml是iBATIS的配置文件,其代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
	<properties resource="jdbc.properties"/>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="${driver}"/>
				<property name="url" value="${url}"/>
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="cn/ibatis3/test/annotation/person.xml"/>
	</mappers>
</configuration>

?上面文件中的sql映射文件person.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="cn.ibatis3.test.annotation.PersonMapper">

</mapper>

???? 注意:在iBATIS3中,命名空间(namespace)是必须的,如果不使用注解的话,名字可以自己定义。一旦使用了注解,这里必须是那个使用注解类或接口的全名。

???? Person.java的代码请参考我的《iBATIS3学习(一)》。

??? sessionFactory.java和我前面的《iBATIS3学习(一)》一样,只是注意将:

private String resource="cn/ibatis3/test/SqlMapper.xml";
改为private String resource="cn/ibatis3/test/annotation/SqlMapper.xml";

?

??? iBATIS3的注解可以定义在接口方法上的,也可以定义在类方法上,我这里定义在接口上,接口PersonMapper.java代码如下:

package cn.ibatis3.test.annotation;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

@CacheNamespace(readWrite = true)
public interface PersonMapper {
	@Select("select * from person where id = #{id}")
	@Options(useCache = true, flushCache = false)
	Person selectById(Integer id);

	@SelectProvider(type=SqlProvider.class ,method="selectAllSql")
	List<Person> selectAll();

	@Select("select * from person where name like #{name}")
	List<Person> selectPersonsByName(String name);

	@Insert( { "insert into person(name,birthday,sex)",
			"values(#{name},#{birthday},#{sex})" })
	void insert(Person person);

	@Delete("delete from person where id=#{id}")
	void delete(Person person);

	@Update( {"update person set name=#{name},birthday=#{birthday},sex=#{sex}",
			"where id=#{id}" })
	void update(Person person);

}

?上面的注解SelectP