Check the completion of additions and deletions to change the use of iBATIS3.0
iBATIS3.0 has also increased some simple annotations, iBATIS3 annotations can only complete a few simple operations, to conduct more complex operations, preferably in the XML file configuration.
In the database (I use mysql) in the establishment of a person table:
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;
In MyEclipse to create a new Java Project, the following structure diagram
The jdbc.properties file is the mapping file to be used, which reads as follows:
driver=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/test username=root password=123456
SqlMapper.xml is iBATIS configuration files, its code is as follows:
<?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>
<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>
The above file sql map file person.xml code is as follows:
<?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>
Note: In iBATIS3 in the namespace (namespace) is a must, if you do not use the annotations, then the name you can define it themselves. Once the use of annotations, where the use of annotations that must be the full name of the class or interface.
Person.java the codes, please refer to my "iBATIS3 learning (1)."
sessionFactory.java and in front of me "iBATIS3 learning (1)", just a note that will:
private String resource="cn/ibatis3/test/SqlMapper.xml";
改为private String resource="cn/ibatis3/test/annotation/SqlMapper.xml";
iBATIS3 annotation can be defined on the interface method can also be defined in the class methods, I have here defined in the interface, the interface PersonMapper.java code is as follows:
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")
void delete(Person person);
@Update( {"update person set name=#{name},birthday=#{birthday},sex=#{sex}",
"where" })
void update(Person person);
}
The above comment SelectProvider use a class SqlProvider.java, its code is as follows:
package cn.ibatis3.test.annotation;
public class SqlProvider {
// 动态的SQL语句,实际上应该使用iBATIS的动态SQL产生方法,这里仅仅是为了使用注解
public String selectAllSql() {
return "SELECT * FROM person p";
}
}
Interface implementation class PersonDao.java code is as follows:
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;
}
}
Well, time will continue to write articles, thank you! Welcome to point out the error, or is not appropriate place.







