使用mybatis首先要导入两个包到lib文件下mybatis-3.2.7.jar,ojdbc6.jar,本次用的数据库是Oracle,一个为mybatis使用所用包,一个为jdbc连接数据库所用的包。
在src文件下面我们需要定义mybatic的配置文件mybatis-config.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>
<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<typeAlias alias="Student" type="beans.Student"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:guard"/>
<property name="username" value="mysource"/>
<property name="password" value="111111"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- mapper路径指定 -->
<mapper resource="usebatis/StudentMapper.xml" />
</mappers>
</configuration>
本文出自: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);
}
<?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="usebatis.StudentMapper">
<!-- 这里namespace必须是StudentMapper接口的路径” -->
<insert id="insertStudent" parameterType="Student">
insert into student(sno,sname,ssex,sbirthday,sclass) values (seq_mysource.nextval,#{sname},#{ssex},#{sbirthday},#{sclass})
<!-- <selectKey resultType="java.lang.String" keyProperty="sno" order="AFTER">
select seq_mysource.currval as sno FROM dual
</selectKey> -->
<!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 -->
</insert>
<!-- 这里的id必须和StudentMapper接口中的接口方法名相同 -->
<select id="getStudent" resultType="Student" parameterType="java.lang.String">
select * from student where sno=#{sno}
</select>
</mapper>
最后是数据操作调用
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();
}
}