mybatis 配置sqlMapConfig.xml

sqlMapConfig.xml


<typeAliases>
<typeAlias alias="student" type="entity.Student"/>
</typeAliases>
//student为别名,type为与数据库相关的实体类
<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());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值