使用mybatis首先要导入两个包到lib文件下mybatis-3.2.7.jar,ojdbc6.jar,本次用的数据库是Oracle,一个为mybatis使用所用包,一个为jdbc连接数据库所用的包。
在src文件下面我们需要定义mybatic的配置文件mybatis-config.xml
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
本文出自:http://www.solgle.com/news/358.html
下面new了一个包usebatis,创建了几个java类文件。MyBatisUtil 主要用于获得SqlSessionFactory
package usebatis;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private final static SqlSessionFactory sqlSessionFactory;
static {
Reader reader = null;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
} catch (Exception e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
package usebatis;
import beans.Student;
创建一个接口java文件StudentMapper,以及配置文件StudentMapper.xml
public interface StudentMapper {
public void insertStudent(Student student);
public Student getStudent(String sno);
}
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into student(sno,sname,ssex,sbirthday,sclass) values (seq_mysource.nextval,#{sname},#{ssex},#{sbirthday},#{sclass})
select * from student where sno=#{sno}
最后是数据操作调用
package usebatis;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import beans.Student;
public class ADOStudent {
static SqlSessionFactory sqlSessionFactory = null;
static {
sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
}
public static void studentAdd() {
System.out.println("load xml beginng");
SqlSession sqlSession = sqlSessionFactory.openSession();//打开数据库会话
System.out.println("load xml end");
try {
//由会话实例化mapper接口
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student();
//sno由数据库序列生成
//student.setSno("301");
student.setSname("10101");
student.setSsex("女");
student.setSclass("20220");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date) sdf.parse("1990-08-08");
student.setSbirthday(date);
studentMapper.insertStudent(student);
sqlSession.commit();// 这里一定要提交,不然数据进不去数据库中
System.out.println("get sno:"+student.getSno());//数据库sno序列号返回
System.out.println("insert sucess");
}catch(Exception e){
sqlSession.rollback();
e.printStackTrace();
} finally {
sqlSession.close();
}
}
public static void getStudent() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
StudentMapper userMapper = sqlSession.getMapper(StudentMapper.class);
Student student = userMapper.getStudent("301");
System.out.println("sno: " + student.getSno() + "|sname: "
+ student.getSname());
}catch(Exception e){
e.printStackTrace();
}
finally {
sqlSession.close();
}
}
public static void main(String args[]) {
studentAdd();
getStudent();
}
}