sqlMapConfig.xml
//student为别名,type为与数据库相关的实体类
<typeAliases>
<typeAlias alias="student" type="entity.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/school?characterEncoding=UTF-8" /> //连接到数据库
<property name="username" value="count" />//用户名
<property name="password" value="password" />//密码
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/StudentDao.xml" />//相关的dao.xml的配置
</mappers>
</configuration>
StudentDao.xml
<mapper namespace="dao.StudentDao">
//路径
<resultMap type="student" id="stuList">
<result column="stuid" property="stuid"/>
<result column="stuname" property="name" />
<result column="sex" property="sex" />
<result column="age" property="age" />
<result column="bj_id" property="bj_id"/>
<result column="birthday" property="birthday" />
</resultMap>//实体类与数据库属性的对应,student为上文的别名
//若两者相同可不写
<select id="getCount" resultType="int">
select count(stuid) from student
</select>//getCount为方法名 resultType为返回值
<select id="search" resultMap="stuList">
select * from student
</select>//若数据库与实体类属性名均相同,那可以写resultType="student",若不相同,则这样写,否则取不到值
<select id="searchByPage"
resultMap="stuList">
select * from student limit #{0} , #{1}
//limit 传参数,#{0}第一个参数,#{1}第二个
</select>
</mapper>
MybatisSqlSession
package dao;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
`public class MybatisSqlSession {
public static SqlSessionFactory getSqlSessionFactory() {
// TODO Auto-generated method stub
String resource = "sqlMapConfig.xml";
InputStream is = MybatisSqlSession.class.getClassLoader()
.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
return factory;
}
/**
* 获取SqlSession
*
* @return SqlSession
*/
public static SqlSession getSqlSession() {
return getSqlSessionFactory().openSession();
}
/**
* 获取SqlSession
*
* @param isAutoCommit
* true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务 false
* 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务
* ,这时就需要我们手动调用sqlSession.commit()提交事务
* @return SqlSession
*/
public static SqlSession getSqlSession(boolean isAutoCommit) {
return getSqlSessionFactory().openSession(isAutoCommit);
}
}
Test
package test;
import java.util.ArrayList;
import java.util.List;
import dao.MybatisSqlSession;
import dao.StudentDao;
import entity.Student;
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
try {
StudentDao dao = MybatisSqlSession.getSqlSession().getMapper(
StudentDao.class);
list = dao.searchByPage(0,5);
for(Student stu:list){
System.out.print(stu.getStuid()+" ");
System.out.print(stu.getName()+" ");
System.out.print(stu.getSex()+" ");
System.out.println(stu.getAge()+" ");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}