mybatis的一个简单的入门实例
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>
<!--为Student实体类定义别名-->
<typeAliases>
<typeAlias alias="student" type="com.xdl.myibatis.Student"/>
</typeAliases>
<!--数据源的配置-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--映射文件-->
<mappers>
<mapper resource="com/xdl/myibatis/mapper/Student.xml"/>
</mappers>
</configuration>
映射文件student.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.xdl.myibatis.mapper.StudentMapper">
<resultMap type="student" id="findAll">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="birthday" column="birthday"/>
<result property="className" column="className"/>
</resultMap>
<select id="findStudent" resultMap="findAll">
<![CDATA[
select
id,name,age,sex,birthday,className
from
student
]]>
</select>
<select id="getStudentById" parameterType="int"
resultType="student">
<![CDATA[
select
id,name,age,sex,birthday,className
from
student
where
id=#{id}
]]>
</select>
<select id="getCount" resultType="int">
<![CDATA[
select
count(*)
from
student
]]>
</select>
</mapper>
ibatis中这里应该是dao层接口和实现类,但是在mybatis中没有实现类,只有一个mapper接口 StudentMapper.java
public interface StudentMapper {
public Student getStudentById(int id);
public int getCount();
public List<Student> findStudent();
}
Student类
public class Student{
private int id;
private String name;
private int age;
private String sex;
private String birthday;
private String className;
此处的set,get方法省略
}
测试类TestStudent.java
package com.xdl.myibatis;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.xdl.myibatis.mapper.StudentMapper;
import junit.framework.TestCase;
public class TestStudent extends TestCase {
@Test
public void testFindStuByID() throws IOException {
String resources = "myibatis-config.xml";
Reader reader = Resources.getResourceAsReader(resources);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sessionFactory = builder.build(reader);
SqlSession session = sessionFactory.openSession();
// 通过xml映射文件来获取信息
// Student student = (Student) session.selectOne("getStudentById", 5);
// 通过注解的形式获取信息
//Student student = (Student) session.selectOne("com.xdl.myibatis.mapper.StudentMapper.getStudentById", 5);
System.out.println(student);
int count = (Integer) session.selectOne("getCount");
System.out.println(count);
session.commit();
session.close();
}
@Test
public void testGetCount() throws IOException {
String resources = "myibatis-config.xml";
Reader reader = Resources.getResourceAsReader(resources);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sessionFactory = builder.build(reader);
SqlSession session = sessionFactory.openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.getStudentById(new Integer(2));
System.out.println(student);
int count = mapper.getCount();
System.out.println(count);
List<Student> students = mapper.findStudent();
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
student = it.next();
System.out.println("编号:" + student.getId() + " \t 姓名:"
+ student.getName() + "\t 年龄:" + student.getAge()
+ "\t 性别:" + student.getSex() + "\t 生日:"
+ student.getBirthday() + "\t 班级:"
+ student.getClassName());
}
} catch (Exception e) {
} finally {
session.close();
}
}
}