环境及软件:
MyEclipse 8,5 & mysql5.1 & jdk 1.6
jar包:
mybatis-3.0.3.jar(可在网上下载http://code.google.com/p/mybatis/)
mysql-connector-java-5.1.6-bin.jar
配置文件:
db.properties:见ibatis简单应用
Configuration.xml:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"/> <typeAliases> <typeAlias type="Test" alias="Test"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="UNPOOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <!-- sql映射文件 --> <mapper resource="Test.xml" /> </mappers> </configuration>
JavaBean:
JavaBean相应的sql映射文件:
Test.xml<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ontheway.mybatis"> <select id="selectAll" resultType="Test"> select * from test </select> <select parameterType="int" id="selectTestById" resultType="Test"> select * from test where id=#{id} </select> <insert id="add" parameterType="test"> insert into test(id,name) values(#{id},#{name}) </insert> <update id="update" parameterType="Test"> update test set name=#{name} where id=#{id} </update> <select id="fuzzyRetrieve" parameterType="String" resultType="Test"> select * from test where name like #{name} </select> <delete id="delete" parameterType="int"> delete from test where id=#{id} </delete> </mapper>
工具类:import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionFactoryUtil {
private static SqlSessionFactory sqlSessionFactory = null;
static {
String resource = "Configuration.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
public static void closeSession(SqlSession sqlSession) {
if(sqlSession != null) {
sqlSession.close();
}
}
}
接口:import java.util.List;
public interface TestDao {
public void add(Test test);
public void delete(int id);
public void update(Test test);
public List<Test>query();
public List<Test> query(String name);
public Test query(int id);
}
接口实现类:
import java.util.List;
import org.apache.ibatis.session.SqlSession;
public class ITestDaoImpl implements TestDao {
public void add(Test test){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
try{
session.insert("com.ontheway.mybatis.add",test);
session.commit();
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
SqlSessionFactoryUtil.closeSession(session);
}
}
public void delete(int id){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
try{
session.delete("com.ontheway.mybatis.delete",id);
session.commit();
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
SqlSessionFactoryUtil.closeSession(session);
}
}
@SuppressWarnings("unchecked")
public List<Test> query(){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
List<Test>list = null;
try{
list = session.selectList("com.ontheway.mybatis.selectAll");
}catch(Exception e){
e.printStackTrace();
}finally{
SqlSessionFactoryUtil.closeSession(session);
}
return list;
}
@SuppressWarnings("unchecked")
public List<Test> query(String name){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
List<Test>list = null;
try{
list = session.selectList("com.ontheway.mybatis.fuzzyRetrieve", name);
}catch(Exception e){
e.printStackTrace();
}finally{
SqlSessionFactoryUtil.closeSession(session);
}
return list;
}
public Test query(int id){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
Test test = null;
try{
test = (Test)session.selectOne("com.ontheway.mybatis.selectTestById", id);
}catch(Exception e){
e.printStackTrace();
}finally{
SqlSessionFactoryUtil.closeSession(session);
}
return test;
}
public void update(Test test){
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
try{
session.update("com.ontheway.mybatis.update", test);
session.commit();
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
SqlSessionFactoryUtil.closeSession(session);
}
}
现在的MyBatis即就是以前的ibatis,并且现在的写法和hibernate很相似的
sql语句还是自己写
没有类映射文件
这里只是列举了xml文件方式,还可以使用注解方式,只是如果sql比较复杂的话,建议使用配置文件方式