使用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);
}
上面的注解SelectProvider使用了一个类SqlProvider.java,其代码如下:
package cn.ibatis3.test.annotation;
public class SqlProvider {
// 动态的SQL语句,实际上应该使用iBATIS的动态SQL产生方法,这里仅仅是为了使用注解
public String selectAllSql() {
return "SELECT * FROM person p";
}
}
接口的实现类PersonDao.java代码如下:
package cn.ibatis3.test.annotation;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class PersonDao implements PersonMapper {
private SqlSessionFactory sessionFactory = SessionFactory.getInstance()
.getSqlSessionFactory();
public Person selectById(Integer id) {
Person person = new Person();
SqlSession session = null;
try {
session = sessionFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
person = personMapper.selectById(id);
} finally {
session.close();
}
return person;
}
@SuppressWarnings("unchecked")
public List<Person> selectAll() {
List<Person> persons = new ArrayList<Person>();
SqlSession session = null;
try {
session = sessionFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
persons = personMapper.selectAll();
} finally {
session.close();
}
return persons;
}
public void delete(Person person) {
SqlSession session = null;
try {
session = sessionFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
personMapper.delete(person);
session.commit();
} finally {
session.close();
}
}
public void insert(Person person) {
SqlSession session = null;
try {
session = sessionFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
personMapper.insert(person);
session.commit();
} finally {
session.close();
}
}
public void update(Person person) {
SqlSession session = null;
try {
session = sessionFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
personMapper.update(person);
session.commit();
} finally {
session.close();
}
}
@SuppressWarnings("unchecked")
public List<Person> selectPersonsByName(String name) {
List<Person> persons = new ArrayList<Person>();
SqlSession session = null;
try {
session = sessionFactory.openSession();
PersonMapper personMapper = session.getMapper(PersonMapper.class);
persons=personMapper.selectPersonsByName("%" + name + "%");
} finally {
session.close();
}
return persons;
}
}
好了,有时间还会继续写相关文章的,谢谢!欢迎大家指出其中的错误或者是不恰当的地方。
- 大小: 8 KB
分享到:
相关推荐
ibatis3.0的中文版文档,pdf格式,很适合ibatis学习使用
ibatis3.0,jsp,有数据库脚本。
Ibatis3.0 增删改查(三) Ibatis3.0 增删改查(三),包含jar包和建表语句。sql建表语句在WEBROOT下面
由ibatis3.0(mybatis3.0)官网下载示例代码
IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习IBatis3.0学习
ibatis3.0中in的用法ibatis3.0中in的用法ibatis3.0中in的用法ibatis3.0中in的用法
介绍了ibatis3.0的配置技巧,包括比较复杂的sql语句的写法。
Ibatis3.0 增删改查(二) Ibatis3.0 mybatis 增删改查(二),包含jar包和建表语句。sql语句在log文件夹下面
官网下的,但是不兼容spring 3.0.1
该文件是一个完整的Spring3.0+Ibatis+Oracle全注解式开发项目,里面详细的介绍了注解的使用,注解描述很全,下载就可以导入到您的myeclipse中运行,很好的Spring注解开发小项目
ibatis3.0中文文档和英文文档
NULL 博文链接:https://king2009163-163-com.iteye.com/blog/657629
Ibatis3.0 spring2.5 strut2
NULL 博文链接:https://jinyike.iteye.com/blog/715697
折腾了好几天,终于从零开始把spring mvc 3.0 + ibatis 3.0 (基于oracle)的Demo完全跑通了。Demo中有不少注释,使用的jar包基本是最精简的了。留着做个备用,也给大家一个参考!
ibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zipibatis-3-core-3.0.0.242.jar.zip
在用iBatis.net与数据库打交道的过程中,发现iBatis.net拼接字符串不能直接拷贝然后在dbms里执行
全面介绍了关于ibatis操作数据库的各种类型,在程序中编辑SqlMapClient对象的成员函数的参数,从而对数据库进行更新
apache开源项目源码ibatis-3-core-src-3.0.0.227 ibatis框架java源程序 spring,struts,hibernate,ibatis,框架源码 各种ibatis框架应用源码,你会从中得到意想不到的效果! apache开源组织开发的开源项目源码,其...